开发者

Unable to pass attribute from controller to view

开发者 https://www.devze.com 2023-01-01 22:28 出处:网络
I\'m using Spring MVC 3 for a web app. In the app a URI template is开发者_如何学编程 supposed to be handled by a controller method, which then passes an attribute to a view. Currently, the controller

I'm using Spring MVC 3 for a web app. In the app a URI template is开发者_如何学编程 supposed to be handled by a controller method, which then passes an attribute to a view. Currently, the controller does handle the request, and it even forwards to the correct view. However, it does NOT pass the attribute to the view. Below are the URI template, controller method and relevent jsp tags. Anyone see what is wrong?

URI:

/home/{status}

Controller:

@RequestMapping("/home")
@Controller
public class HomeController {
...
...
@RequestMapping(value="/{status}") 
public String homeStatusView(@PathVariable("status") String status, ModelMap model) {
model.addAttribute("status", status);
return "home";
}
}

JSP:

...
<c:if test="${not empty status}">
<span class="status">Your status is available...</span>
</c:if>
...


I use it in the same way as yours though I use Model class instead of ModelMap, though I think it works as well with ModelMap. On the other hand when you write:

<span class="status">Your status is available...</span>

did you mean something like this?:

<span class="${status}">Your status is available...</span>

if you don't use ${} it won't be printed.


This is a slightly different approach but should work:

@RequestMapping(value="/{status}") 
public ModelAndView homeStatusView(@PathVariable("status") String status) {

  ModelAndView mav = new ModelAndView("home");
  mav.addAttribute("status", status);
  return mav;

}
0

精彩评论

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