开发者

Problem in Spring enabled controller page rendering in jsp

开发者 https://www.devze.com 2023-01-29 16:19 出处:网络
When i do this. @RequestMapping(\"/folder/{name}.jsp\") public ModelAndView catchAll(@PathVariable String viewName) {

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 开发者_JS百科that how can i program in spring


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"));
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消