beauty_beast

          上善若水 厚德載物

          這兩天在學習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方式提交表單的。

          Feedback

          # re: Spring框架學習二篇外----在WEBMVC學習遇到的問題  回復  更多評論   

          2006-05-26 14:25 by null
          為啥“沒有配置對應屬性 formView值”呢?當你新建一個東西時,不需要“展現一個新表單”嗎?
          一個SimpleFormController可以處理一種表單的新建、編輯、修改,并且提供數據驗證和綁定的功能,我覺得這是SpringMVC最獨到的地方了;處理表單以外的其他GET請求時,我都用MultiActionController。

          # re: Spring框架學習二篇外----在WEBMVC學習遇到的問題  回復  更多評論   

          2006-05-26 15:17 by 柳隨風
          呵呵,我明白你的意思,我做的測試頁面是提交數據的頁面,不是展現一個新表單,我實際上只是想測試數據綁定功能。
          謝謝你的指導.

          # re: Spring框架學習二篇外----在WEBMVC學習遇到的問題  回復  更多評論   

          2006-05-26 20:07 by 潛心
          當然了
          spring對表單提交的方法是非常嚴格的,
          建議把spring的API仔細研究下,里面講的很清楚
          主站蜘蛛池模板: 吴江市| 易门县| 布尔津县| 富民县| 佳木斯市| 城固县| 武义县| 沙坪坝区| 印江| 明水县| 平昌县| 称多县| 灵丘县| 司法| 寿宁县| 虹口区| 乌兰察布市| 乌鲁木齐县| 香港 | 新沂市| 宝丰县| 麟游县| 正镶白旗| 扶余县| 鹰潭市| 石楼县| 磐安县| 武清区| 固原市| 安康市| 广宁县| 翁牛特旗| 乌什县| 舞钢市| 辽中县| 黎平县| 九江县| 阳山县| 左云县| 罗源县| 临沧市|