aditya
aditya

Reputation: 495

Problem in Spring enabled controller page rendering in jsp

When i do this.

@RequestMapping("/folder/{name}.jsp")
public ModelAndView catchAll(@PathVariable String viewName) {
    return new ModelAndView("legacy/" + viewName);
}

it give an error on the browser that

legacy/123.jsp is not available

How can I resolve this issue

if i have a list of id,

i want that the browser show like this /legacy/1 /legacy/2 etc so for that how can i program in spring

Upvotes: 0

Views: 615

Answers (2)

Samo
Samo

Reputation: 8240

You're telling Spring to render a jsp page that doesn't exist. If the name of your jsp is "legacy.jsp", that's the view you render, and you're just passing a different object to the view based on the parameter. If you want legacy/123 to take you to "legacy.jsp" which will show a Legacy object with the id of 123, then you do it like this:

@RequestMapping("/legacy/{id}")
public ModelAndView catchAll(@PathVariable("id") String id) {
  Legacy legacy = // Get the object from your database using the id
  return new ModelAndView("legacy", "legacy", legacy); // insert the legacy object into your view, it will be accessed by the name "legacy"
}

So this way, you enter the url "/legacy/123" in your browser (or click a link to it) and it just renders the legacy.jsp with the object you fetched using the id 123.

Upvotes: 0

Ralph
Ralph

Reputation: 120811

You need to put an 123.jsp in the legacy subdirectory of the directory where you put your JSPs.

But I guess that is not what you want: I asssume you want to build somekind of forwarding. In this case you have to return an redirect view.

@RequestMapping("/folder/{name}.jsp")
public ModelAndView catchAll(@PathVariable String viewName) {
    return new ModelAndView(new RedirectView("legacy/" + viewName + ".jsp"));
}

Upvotes: 2

Related Questions