MDS
MDS

Reputation: 153

Unable to pass attribute from controller to view

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>
...

Upvotes: 1

Views: 1403

Answers (2)

Javi
Javi

Reputation: 19789

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.

Upvotes: 1

Mark Pope
Mark Pope

Reputation: 11274

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;

}

Upvotes: 0

Related Questions