SpringMVC一路總結(二)

     冰凍三尺非一日之寒。對技術的學習尤爲得遵循這麼一個理。在《SpringMVC一路總結(一)》中,清楚的總結了SpringMVC的入門案例,對於這類入門demo,理清套路,整理思緒是最爲重要的。可以從案例中瞭解到SpringMVC在框架技術中的特色和應用,就是第一部分的主要做用。css

      從上部分能夠看出,每一個Controller中只能寫一個方法,這個Controller是實現Controller中的方法handleRequest。然而對於這種形式的分佈和配置,一旦項目中的功能增多,很難準確的定位方法的請求,並且維護也很麻煩,所以,這樣的實際應用特色並不明顯。今天的這部分就是主要解決這樣的問題的。本博文爲本人學習的總結,請尊重勞動成果。歡迎轉載,請保留博文出處:http://www.cnblogs.com/itred ;本人郵箱: it_red@sina.com 。html

     那麼,問題來了!如何在一個Controller中寫入多個方法呢?java

     其實在SpringMVC中已經寫好了,只須要繼承其中的MultiActionController,這個的具體類在org.springframework.web.servlet.mvc.multiaction目錄下。在spring的JAR包中,能夠清楚的找到這個類。web

   

   仍是從案例上來講吧,要實現這麼一個多方法的Controller,基本原理和第一部分的案例相同。只是須要簡單修改一些配置文件和從新寫Controller就好了。spring

    SpringMVC-servlet.xml的配置爲:瀏覽器

<bean id="paramMethodResolver"
  class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
       <property name="paramName" value="action"></property>
    </bean>
    <bean name="/test" class="com.red.controller.MultiController">
       <property name="methodNameResolver">
           <ref bean="paramMethodResolver" />
       </property>
    </bean>

 

  上面的bean 中id爲paramMethodResolver 就是配置一個Controller類中實現多個方法的重點。mvc

     在Controller中繼承MultiActionController的源碼以下:app

package com.red.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class MultiController extends MultiActionController {
         public ModelAndView First(HttpServletRequest request,
                            HttpServletResponse response) {
                   String method = "First";
                   System.out.println("using First method::" + method);
                   return new ModelAndView("/test", "param", method);
         }
         public ModelAndView Second(HttpServletRequest request,
                            HttpServletResponse response) {
                   String method = "Second";
                   System.out.println("using Second method::" + method);
                   return new ModelAndView("/test", "param", method);
         }
}

 

    瀏覽器中輸入地址:http://localhost:8080/20150507_SpringMVC01/test?action=First框架

   實現效果以下:學習

    

    瀏覽器中輸入地址:http://localhost:8080/20150507_SpringMVC01/test?action=Second

    實現效果以下:

    

 控制檯中輸出的結果:

     

  其實在開發過程當中遇到最多的問題,每每不是這些技術性問題。須要注意一點細節的地方,不少人在剛接觸SpringMVC的時候,圖片,js以及css類的文件基本都會出問題,在頁面上不能正常的在瀏覽器中顯示理想的效果。然而,其實是由於在配置文件中少了一些配置,或者配置的時候沒有配置正確。

即,須要在SpringMVC-servlet.xml中加入如下源碼:

<mvc:resources location="/img/" mapping="/img/**" />

 

Location中是文件的實際存放路徑,mapping是指的是映射圖片文件 夾。對於其餘的如css和js類的文件用一樣的方法就能夠。

  實際效果以下:

     

 

===================================================================

      在這部分以及上一部分都是經過xml的形式來配置實現的,然而xml並非實現SpingMVC的最佳方式,其實註解纔是其特色。若是利用註解的形式來實現就能夠清楚的看到SpringMVC框架的優勢,簡單,並且還適用。關於註解的形式會在後續的總結中給出案例。

      Demo下載

 

                   感謝奮鬥中的本身!               


 

 郵箱:it_red@sina.com
 我的博客: http://itred.cnblogs.com
 
相關文章
相關標籤/搜索