When the user of my application creates a new page, she is asked to enter a 'Page Title' among other details. (e.g. she entered: "Page Title by Megan") The application saves the instance into db.
Now my requirement is that when anyone tries to access that page, the URL should be like:
www.websitename.com/pages/page-title-by-megan
I understand that with Spring MVC I can easily get the part "page-title-by-megan" by using something like:
@RequestMapping("/pages/{pageTitle}")
public String getPage(@PathVariable String pageTitle, Model model) {
List<Pages> pages = pageService.getPages(pageTitle);
model.addAttribute("pages", pages);
return "pages";
}
But the problem is
Q1: To retrieve the relevant page, I need to store pageTitle "page-title-by-megan" along with the actual data in the DB? Is that the only trick to do this? Or there's a better way?
Q2: If that's the only way, how to effectively form a string like "page-title-by-megan" from "Page Title by Megan"? Again, is there a better approach?
Kindly guide. I would be very thankful.