Re: writing the correct url input

Splash Forums PrettyFaces Users writing the correct url input Re: writing the correct url input

#22835

Sure, you can do that. Typically developers are using database IDs to reference an object from the database in the URL like you did before. This is usually a very nice way to do it because you can simply use the ID from the URL to load the corresponding object from the database.

If you want to have names in the URL this will become a little bit harder. If your model object has something like a name you could use that instead. But you have to make sure that names are unique so that you can always find the correct model object in the database when only having the name and not the ID any more.

If cannot make sure that the name is unique, you should add something like a “URL key” property to your object which is guaranteed to be unique (containing “toyota” for example).

Another option would be to use both the ID and the name of the entity in the URL. Something like:

http://www.someweb.com/store/2/toyota/3/corolla

That would allow you to use the ID to load the correct object from the database and you have a pretty name of the entity in the URL. You would use the ID to load the object from the database and just ignore the name

That’ something Amazon does in a similar way. Look at this URL:

http://www.amazon.com/Whats-New-Java-7-ebook/dp/B005XSS8VC/

The text “Whats-New-Java-7-ebook” is for SEO reasons only. In fact you can also use this URL:

http://www.amazon.com/Bla-Bla-Bla/dp/B005XSS8VC/

So the unique ID for the item is “B005XSS8VC”.

Hope this helps

Christian