我在我的控制器中定義了@ModelAttribute,它需要根據請求的方法輸出執行。所以當我試圖從JSP訪問我的ModelAttribute時,它卻產生了以前的結果。例如下面:@Model在顯示JSP之前執行屬性執行
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中使用$ {}的地址,它會顯示「測試地址」,我需要在我的JSP「更改地址」。但是我的ModelAttribute在jsp加載後執行。是否有可能使用@ModelAttribute來做到這一點,如果是這樣,那麼怎麼做? 。除了@ModelAttribute之外,還有其他方法可以實現嗎?
你能提供實際的代碼嗎?你上面的代碼不會編譯。 –
如果您想要將新地址存儲在模型中,那麼...將其存儲在模型中:'model.addAttribute(「Address」,「Changed Address」);' –
model.addAttribute()正在工作,但此ModelAttribute方法被許多控制器用於更大的應用程序。我需要它在單個地方,並且必須在所有JSP中使用。除了ModelAttribute以外,還有其他的方式嗎? – DeepVeen