We have a Spring MVC webapp and our product URL structure needs to be SEO friendly. Such as www.mydomain.com/{productname}. Every time we add a product to inventory, we want to have URL to display those product details.
We could not figure our how to generate dynamic URLs with Spring Controller Resource path.
Can someone please help us.
Would appreciate your help.
Thanks
Raj
2
Answers
A Spring URL structure like http://www.mydomain.com/{productname} would seem to imply that ‘productname’ is always unique. If so, each product could have a property called ‘name’ or ‘productname’; this property could be used to retrieve the relevant product in your controller method. This will only work if every ‘productname’ is unique.
What you are looking for is URI Template Patterns. In Spring MVC,
@PathVariable
annotation is used to bind an argument to the value of URI template variable.Note that the service call returns
List<Product>
instead of oneProduct
, since ideally, product name is not unique to one item. If you want the URI to identify exactly one product, make sure that product name is unique to one product only.