2012-01-20 34 views
2

我在提交我的形式收到以下錯誤發現:Spring MVC的: - 會話屬性需要閉會期間

org.springframework.web.HttpSessionRequiredException: Session attribute 'rulesForm' required - not found in session 
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseSessionRequiredException(AnnotationMethodHandlerAdapter.java:722) 

我的JSP包含以下內容:

<form:form id="rulesForm" modelAttribute="rulesForm" action="save.do"> 
... 
</form> 

我的控制器包含以下:

@Controller 
@RequestMapping("/rules") 
@SessionAttributes({"rulesForm", "deskForm"}) 
public class RulesController { 
. 
. 
. 
@RequestMapping(value = "/save.do") 
public ModelAndView saveRuleAttributesAndRules(@Valid 
    @ModelAttribute("rulesForm") 
    RulesFormDTO rulesForm, BindingResult bindingResult, HttpSession session, Principal principal) { 

看來,如果我讓我的瀏覽器打開一段時間,我的表單顯示g然後我嘗試執行提交一段時間後出現此錯誤。

真的,我想在這種情況下發生的事情是爲了創建新的「rulesForm」對象......我該如何實現這一目標?

感謝

回答

5

由於上@SessionAttribute的Javadoc指示使用註釋意味着你要存儲在會話,這意味着你需要首先添加到模型中指定的模型屬性。 Spring MVC不會創建它們。

換句話說,當您添加@ModelAttribute(「rulesForm」)控制器方法參數時,您告訴Spring MVC在模型中查找它,或者如果未找到,則創建一個新實例。但是,如果您還添加@SessionAttributes,則Spring MVC不會嘗試創建新實例,並且會期望該對象位於模型中或會話中。您可以使用@ModelAttribute方法最初添加對象。

+1

添加@ModelAttribute方法並不是我想要的。我希望將表單中發佈的數據映射到RulesForm對象。如果我的表單被提交,它不會執行綁定? – DJ180