• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Spring MVC PathVariable for products and categories implementation

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Guys ! I develop eCommerce platform but i don't know how to implement the scenario below. I want to have friendly URLs (for SEO stuff..). I have the correct slug in the database for categories and products for example:

www.domain.com/category-tools/

wwww.domain.com/category-laptops/ - which are categories

but i and products which are:

www.domain.com/iphone6s-32gb

www.domain.com/macbook-pro-ssd

How to implement this logic in to the handler below ? How to know when it is product or category ? Do i need to make two separate queries (one for products and one for categories) to the database ?

 
Saloon Keeper
Posts: 14711
331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Slugs should only be an after-thought. Don't use them to identify a resource. First design your URLs so they unambiguously locate a resource:

https://example.com/shop/categories/laptops

https://example.com/shop/products/macbook-pro-ssd

If you still need the slug, for instance when you're using integers for ids rather than unique names, you add them to the URL *after* the URL contains enough information to identify the resource:

https://example.com/shop/categories/5/laptops

https://example.com/shop/products/24/macbook-pro-ssd


In the above code, the index() action will map the {id} part of the URL to the id method parameter, and ignore everything that comes afterwards.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic