Posted on 2006-05-25 18:10
柳隨風 閱讀(3673)
評論(3) 編輯 收藏 所屬分類:
開源框架
這兩天在學習SpringMVC遇到兩個比較郁悶的問題,估計新學者很容易遇到,和大家分享一下,避免出現類似的問題。
1、 No request handling method with name 'insert' in class? "ClassName",頁面顯示為404錯誤
這個問題出現在使用多操作控制器情況下,相關的操作方法中對應的方法參數前兩位必須是request,response對象,必須要有,否則會報如上異常。
2、這個問題困惑了我半天,在網上也有類似的問題,但沒有正確解決方法,異常如下:
javax.servlet.ServletException: ModelAndView [ModelAndView: materialized View is [null]
這個問題可能出現的場景很多,我所描述的只是其中之一,沒有相關解決方法,只有查看相關源代碼,開源就是有這個好處。
異常拋出代碼為:
??????? at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:924)
查看了相關源代碼,一層一層看下去
首先在ModelAndView 類實例是在DispatcherServlet類中的doDispatch方法中創建的,
再跟蹤doDispatch方法中相關代碼行
HandlerAdapter?ha?
=
?getHandlerAdapter(mappedHandler.getHandler());
mv?
=
?ha.handle(processedRequest,?response,?mappedHandler.getHandler());
ha是一個接口實現類,在該場景下,對應的接口實現類為:
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
SimpleControllerHandlerAdapter類中對應的實現代碼為:
((Controller)?handler).handleRequest(request,?response)
調用的是對應的Controller接口中方法,當前Controller對應的接口實現類為我們配置的自定義控制類,一般繼承于org.springframework.web.servlet.mvc.SimpleFormController;一層一層再跟蹤發現:
SimpleFormController繼層于同包AbstractFormController類,而
AbstractFormController繼承于同包AbstractController類,對應的
handleRequest(request,response)在AbstractController類中實現,最終調用代碼如下:
return
?handleRequestInternal(request,?response)
handleRequest方法為一個抽象方法,在AbstractFormController類中實現,終于找到原因了,呵呵
protected
?
final
?ModelAndView?handleRequestInternal(HttpServletRequest?request,?HttpServletResponse?response)

????????????
throws
?Exception?
{

????????
//
?Form?submission?or?new?form?to?show?
????????
if
?(isFormSubmission(request))?
{

????????????
//
?Form?submission:?in?session-form?mode,?we?need?to?find
????????????
//
?the?form?object?in?the?HTTP?session.
????????????
if
?(isSessionForm())?
{
????????????????HttpSession?session?
=
?request.getSession(
false
);

????????????????
if
?(session?
==
?
null
?
||
?session.getAttribute(getFormSessionAttributeName(request))?
==
?
null
)?
{
????????????????????
//
?Cannot?submit?a?session?form?if?no?form?object?is?in?the?session.
????????????????????
return
?handleInvalidSubmit(request,?response);
????????????????}
????????????}
????????????
//
?Found?form?object?in?HTTP?session:?fetch?form?object,
????????????
//
?bind,?validate,?process?submission.
????????????Object?command?
=
?getCommand(request);
????????????ServletRequestDataBinder?binder?
=
?bindAndValidate(request,?command);
????????????
return
?processFormSubmission(request,?response,?command,?binder.getErrors());
????????}
????????
else
?
{
????????????
//
?New?form?to?show:?render?form?view.
????????????
return
?showNewForm(request,?response);
????????}
????}
原因實際很簡單,就因為我在要提交的表單中沒有采用post方法,呵呵
而isFormSubmission(request)就是根據此項判斷,所以其實際執行的代碼為:
return showNewForm(request, response);
而我在對應的配置屬性中沒有配置對應屬性 formView值,因為我本來就不是要展現一個新表單。
故最后返回的ModelAndView為空。
問題都解決了,只是沒想到對提交表單這么嚴格,其他web框架是沒有這種限制,不過也沒多大關系,在實際開發中我們大都是采用post方式提交表單的。