Usando ajax, ¿cómo puedo generar una URL que se pueda marcar?

I'm writing a simple web application, using tomcat server, servlets and jsp pages. Ideally, I would like my website to be only one page into which I load content (jsp files) on user actions.

However it is also important that users will be able to "bookmark" certain urls to get straight to the content they need to get to.

For example, lets say I have a page that details all of the registered users of my site and then each user has a profile page. I want that users who navigate to someones profile will see the following url:

http://mywebsite.com/users/some_user_name

and that when they enter this url they will get to the profile of some_user_name.

The problem is that all of the content was generated using ajax, and I manually changed the urls displayed using history.pushState - so that when you enter the example url you simply get 404 page.

A simple solution I had in mind was to have a servlet mapped to the /users/* pattern which responds with the correct jsp page. Is this is a reasonable solution or there are other better and acceptable ways to handle this kind of a problem?

Gracias :)

preguntado el 24 de agosto de 12 a las 00:08

From Wiki itself on drawbacks of AJAX: Dynamic web page updates also make it difficult to bookmark and return to a particular state of the application. Solutions to this problem exist, many of which again use the URL fragment identifier.[8][9] Anyway, it sounds like you are using AJAX just because you can. Please spare the Web yet another obnoxious AJAX webpage if possible and reconsider whether you really need to use AJAX for the whole website. -

1 Respuestas

If you are managing history using pushState then when the application is accessed directly through the url you can take the url fragment and make an ajax call and load the content. In order to achieve this all your urls must be rest compliant so it will be easy to load proper view in your case it can be something like this

http://mywebsite.com/users/profile/some_user_name

So now when the app is loaded you can load perfiles view with user name some_user_name

if you want some other view like edit then it can be

http://mywebsite.com/users/profile/edit/some_user_name

or

http://mywebsite.com/users/profileEdit/some_user_name

you should implement a router which can route these urls properly and also an main method which can fetch data from server and pass it on to view.

if you can use your JSP code as a rest webservice this can be achieved easily and also there are lot of libraries which support things like this. you might want to look into Backbone.js /Knockout.js

aquí is the list of popular frameworks with comparisons.

But the issue you might face is SEO. there are some workarounds for this aswell.

Respondido 24 ago 12, 02:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.