【发布时间】:2013-08-23 02:14:21
【问题描述】:
我在我的控制器中定义了@ModelAttribute,它需要根据请求的方法输出来执行。因此,当我尝试从 JSP 访问我的 ModelAttribute 时,它会产生先前的结果。例如下面:
class MyController{
@modelAttribute("Address")
protected getAddress(HttpRequest req){
HttpSession sess = req.getSession();
return sess.getAttribute("Address");// For example now Address is "Test Address"
}
@RequestMapping("sample.do", method=RequestMethod.GET)
public Model requestMethod(......)
{
// after execution of this method
sess.setAttribute("Address","Changed Address");
return model;// request directed to my JSP.
}
}
当我在我的 JSP 中使用 ${Address} 时,它显示“测试地址”,我需要在我的 JSP 中“更改地址”。但是我的 ModelAttribute 是在加载 jsp 之后执行的。是否可以使用@ModelAttribute 使这成为可能,如果可以,那么如何。? .除了@ModelAttribute,还有其他方法可以实现吗?
【问题讨论】:
-
能否提供实际代码。您上面的代码无法编译。
-
如果要将新地址存储在模型中,那么...存储在模型中:
model.addAttribute("Address", "Changed Address"); -
model.addAttribute() 正在工作,但是这个 ModelAttribute 方法被许多控制器在更大的应用程序中使用。我需要它在一个地方并且必须在所有 JSP 中使用。除了 ModelAttribute 有没有其他方法
标签: java spring jsp annotations modelattribute