beauty_beast

          上善若水 厚德載物

          這兩天在學(xué)習(xí)SpringMVC遇到兩個(gè)比較郁悶的問題,估計(jì)新學(xué)者很容易遇到,和大家分享一下,避免出現(xiàn)類似的問題。
          1、 No request handling method with name 'insert' in class? "ClassName",頁(yè)面顯示為404錯(cuò)誤
          這個(gè)問題出現(xiàn)在使用多操作控制器情況下,相關(guān)的操作方法中對(duì)應(yīng)的方法參數(shù)前兩位必須是request,response對(duì)象,必須要有,否則會(huì)報(bào)如上異常。
          2、這個(gè)問題困惑了我半天,在網(wǎng)上也有類似的問題,但沒有正確解決方法,異常如下:
          javax.servlet.ServletException: ModelAndView [ModelAndView: materialized View is [null]
          這個(gè)問題可能出現(xiàn)的場(chǎng)景很多,我所描述的只是其中之一,沒有相關(guān)解決方法,只有查看相關(guān)源代碼,開源就是有這個(gè)好處。
          異常拋出代碼為:
          ??????? at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:924)
          查看了相關(guān)源代碼,一層一層看下去
          首先在ModelAndView 類實(shí)例是在DispatcherServlet類中的doDispatch方法中創(chuàng)建的,
          再跟蹤doDispatch方法中相關(guān)代碼行

          HandlerAdapter?ha? = ?getHandlerAdapter(mappedHandler.getHandler());
          mv?
          = ?ha.handle(processedRequest,?response,?mappedHandler.getHandler());

          ha是一個(gè)接口實(shí)現(xiàn)類,在該場(chǎng)景下,對(duì)應(yīng)的接口實(shí)現(xiàn)類為:
          org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter

          SimpleControllerHandlerAdapter類中對(duì)應(yīng)的實(shí)現(xiàn)代碼為:

          ((Controller)?handler).handleRequest(request,?response)

          調(diào)用的是對(duì)應(yīng)的Controller接口中方法,當(dāng)前Controller對(duì)應(yīng)的接口實(shí)現(xiàn)類為我們配置的自定義控制類,一般繼承于org.springframework.web.servlet.mvc.SimpleFormController;一層一層再跟蹤發(fā)現(xiàn):
          SimpleFormController繼層于同包AbstractFormController類,而
          AbstractFormController繼承于同包AbstractController類,對(duì)應(yīng)的
          handleRequest(request,response)在AbstractController類中實(shí)現(xiàn),最終調(diào)用代碼如下:

          return ?handleRequestInternal(request,?response)

          handleRequest方法為一個(gè)抽象方法,在AbstractFormController類中實(shí)現(xiàn),終于找到原因了,呵呵

          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);
          ????????}

          ????}

          原因?qū)嶋H很簡(jiǎn)單,就因?yàn)槲以谝峤坏谋韱沃袥]有采用post方法,呵呵
          而isFormSubmission(request)就是根據(jù)此項(xiàng)判斷,所以其實(shí)際執(zhí)行的代碼為:
          return showNewForm(request, response);
          而我在對(duì)應(yīng)的配置屬性中沒有配置對(duì)應(yīng)屬性 formView值,因?yàn)槲冶緛砭筒皇且宫F(xiàn)一個(gè)新表單。
          故最后返回的ModelAndView為空。

          問題都解決了,只是沒想到對(duì)提交表單這么嚴(yán)格,其他web框架是沒有這種限制,不過也沒多大關(guān)系,在實(shí)際開發(fā)中我們大都是采用post方式提交表單的。

          Feedback

          # re: Spring框架學(xué)習(xí)二篇外----在WEBMVC學(xué)習(xí)遇到的問題  回復(fù)  更多評(píng)論   

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

          # re: Spring框架學(xué)習(xí)二篇外----在WEBMVC學(xué)習(xí)遇到的問題  回復(fù)  更多評(píng)論   

          2006-05-26 15:17 by 柳隨風(fēng)
          呵呵,我明白你的意思,我做的測(cè)試頁(yè)面是提交數(shù)據(jù)的頁(yè)面,不是展現(xiàn)一個(gè)新表單,我實(shí)際上只是想測(cè)試數(shù)據(jù)綁定功能。
          謝謝你的指導(dǎo).

          # re: Spring框架學(xué)習(xí)二篇外----在WEBMVC學(xué)習(xí)遇到的問題  回復(fù)  更多評(píng)論   

          2006-05-26 20:07 by 潛心
          當(dāng)然了
          spring對(duì)表單提交的方法是非常嚴(yán)格的,
          建議把spring的API仔細(xì)研究下,里面講的很清楚
          主站蜘蛛池模板: 图片| 土默特左旗| 红河县| 延边| 长治县| 泾川县| 荥阳市| 亚东县| 资兴市| 方山县| 维西| 田林县| 昌黎县| 高密市| 新津县| 衡水市| 洞头县| 浦城县| 汨罗市| 昭平县| 中方县| 永川市| 桂阳县| 依兰县| 神池县| 杭锦后旗| 出国| 垫江县| 凌源市| 旬邑县| 扶沟县| 辛集市| 德钦县| 金门县| 德令哈市| 林口县| 凯里市| 河津市| 偃师市| 泾源县| 辽阳县|