2011-02-11 49 views
3

我有一個控制器的方法來實例化一個日曆:如何防止在@ModelAttribute方法

public FormValidationResult submitFormAndSendEmail(@Valid ContactForm form, BindingResult result, 
    @HttpSessionParam(value = "lastTimeContactFormSent", required = false) Calendar lastTimeContactFormSent) 

正如你可以看到我已經創建了一個@HttpSessionParam註釋,這將需要一個變量從HttpSession中並將其放入指定的方法參數中。

但是......

的說法正在被解決之前,我收到一封InstantiationExceptionConstructorAccessorImpl因爲日曆無法通過默認的構造函數來實例化。

使用給定的堆棧跟蹤,我看到「resolveModelAttribute」中的HandlerMethodInvoker導致實例化。

我該如何預防?我不想實例化,我想使用我自己的WebArgumentResolver來填充方法參數。

任何線索?

更多信息: 堆棧跟蹤由Spring(3.0.4):

java.lang.InstantiationException 
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126) 
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104) 
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:772) 
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:356) 
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171) 
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427) 
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415) 

我沒有看到調試器擊中webargumentresolver。

的論點是在我的應用程序上下文(XML)的定義如下:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    <property name="customArgumentResolver" ref="sessionParamResolver"/> 
</bean> 

<!-- annotation to resolve httpSession attributes --> 
<bean id="sessionParamResolver" class="nl.usgpeople.vakgilde.spring.mvc.extensions.SessionParamArgumentResolver"/> 

回答

1

看起來axtavt是正確的,部分地。 Web參數沒有得到解決。但是,它已在上下文xml中正確定義。那麼爲什麼它沒有工作?

application-context.xml包含一個mvc-context.xml(使用import標籤)。在mvc-context.xml中,我定義了bean等。

在導入標記上移動mvc-context.xml之外的bean定義,使Spring'通知'bean並按適當的順序解析。

看起來更進一步,它看起來像只要我的sessionParamResolver豆被定義以上標籤,它的工作原理。當把我的bean放在這個標籤下時,它不起作用。

查看Spring documentation,在第15.12章配置Spring MVC,據說它會定義一個AnnotationMethodHandlerAdapter。所以我想事先定義它確保它也使用你的customArgumentResolvers。

+0

順便說一句,通過這樣做,你將覆蓋默認的AnnotationMethodHandlerAdapter。這是春天的一個錯誤,請看這裏:http://forum.springsource.org/showthread.php?p = 272494,您需要取出mvc:annotation-driven標籤以獲得完全控制權。 – 2011-02-11 12:05:07

1

它看起來像你的說法解析器沒有被解僱或者沒有解決的說法。確保:

  • 您的解析器正確地傳遞給AnnotationHandlerMethodAdapter
  • 如果您的解析器被觸發,它正確解析參數
+0

我已經定義了在我的應用程序上下文中,我已經更新了我的原始問題以及它的配置方式。 – 2011-02-11 11:20:45