統合springmvc

パッケージのコントローラを構築し、TestController.javaを書きます

パッケージコントローラ。

輸入org.springframework.stereotype.Controller;
輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.bind.annotation.ResponseBody。

@Controller 
@RequestMapping( "CON" パブリック クラスTestController { 

    パブリックTestController(){ 
        System.out.printlnは( "123123123" )。
    }
     
    @RequestMapping( "試験" 
    @ResponseBody 
    パブリック文字列testmvc(){ 
        System.out.printlnは( "testmvc" )。
        リターン「testmvc」
    } 
    
}

書き込みコンストラクタは便利かどうかを確認するためにスキャンします

 

springmvc.xml

<?xmlのバージョン= "1.0"エンコード= "UTF-8"> 
<豆のxmlns = "http://www.springframework.org/schema/beans" 
    のxmlns:XSI = "http://www.w3.org / 2001 / XMLスキーマ・インスタンス"のxmlns:P = "http://www.springframework.org/schema/p" 
    のxmlns:コンテキスト= "http://www.springframework.org/schema/context" 
    のxmlns:MVC =" http://www.springframework.org/schema/mvc "  XSI:のschemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans- 4.0.xsd  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4。0.xsd「>  <! -コンフィギュレーション・コントローラのスキャン- >
    
    <コンテキスト:コンポーネントベース・パッケージスキャン= "コントローラ" />
 < -ドライブ構成注釈! - >  <MVC:注釈ドリブン/> 
 <! -構成図リゾルバ- > <! - <ビーンクラス= "org.springframework.web.servlet.view.InternalResourceViewResolver"> 接頭辞<プロパティ名= "接頭辞"値 = "/ WEB-INF / JSP /" /> 接尾語<プロパティ名= "サフィックス"値 =」。JSP 「/> </豆> - > </豆>

ジャンプアクションでパケットコントローラ、プレフィックスとサフィックスをスキャンするように設定することがspringmvc.xml必要がJSP接頭辞と接尾辞であります

 

web.xmlにspringmvc設定を追加

<xmlのバージョン= "1.0"エンコード= "UTF-8"?> 
の<web-appのxmlns:XSI = "http://www.w3.org/2001/XMLSchema-instance"のxmlns = "のhttp:// javaのsun.comの/ XML / NS / JavaEEの"XSI:のschemaLocation =" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5 .xsd」バージョン= "2.5"> 
  <表示名> SMH </表示名> 
  <歓迎-ファイル・リスト> 
    <歓迎-ファイル> index.htmlを</歓迎ファイル> 
    <歓迎ファイル> index.htmを</歓迎ファイル> 
    <歓迎-ファイル> index.jspを</歓迎ファイル> 
    <歓迎-ファイル> デフォルトの.html </歓迎ファイル> 
    <歓迎-ファイル> デフォルトの.htm </歓迎ファイル> 
    <歓迎、ファイル> デフォルトの.jsp </歓迎ファイル> 
  </歓迎ファイルリスト> 
    <! -配置春- >
    <コンテキスト-param>の
        の<param-name>のcontextConfigLocation </ PARAM名> の<param-value>のクラスパス:applicationContext.xmlを</ PARAM値> 
    </コンテキスト-param>の
    <! -配置监听器加载春- > 
    <聞き手> 
        <listener- クラス > org.springframework.web.context.ContextLoaderListener </ listener- クラス > 
    </リスナー> 
    <! -配置过滤器、解决ポスト的乱码问题- > 
    <フィルター> 
        <フィルター-名前>エンコード</フィルタ名> <フィルター- クラス > org.springframework.web.filter.CharacterEncodingFilter </フィルター- クラス > 
        <INIT-PARAM> 
        </ INIT-PARAM>
        

 
            の<param-name>のエンコーディング</ PARAM名>
            <PARAM値> UTF-8 </ PARAM値> 
    </フィルタ> 
    <フィルタマッピング> 
        <フィルタ名>エンコード</フィルタ名> 
        の<url-pattern> *。アクション</ URLパターン> 
    < /フィルタマッピング> 
    
    <フィルター> 
      <フィルタ名> openSessionInView </フィルタ名> 
      <フィルター- クラス > org.springframework.orm.hibernate4.support.OpenSessionInViewFilter </フィルター- クラス > 
  </フィルタ> 
    <フィルタマッピング> 
      <フィルタ名> openSessionInView </フィルタ名> 
      の<url-pattern> / * </のurl-pattern> 
  </のfilter-mapping>

    <! -配置SpringMVC - > 
    の<servlet> 
        <サーブレット名>ブート-CRM </サーブレット名>
        <サーブレットクラス> org.springframework.web.servlet.DispatcherServlet </サーブレットクラス> 
        の<init-param>要素
            の<param-name>のcontextConfigLocation </ PARAM名>     の<param-value>はCLASSPATH:springmvc.xml </ PARAM -value> 
        </ INIT-param>の
        <! - springmvcが活性化されると、整数パラメータでなければならない配置- > 
        <! -容器がspringMVCを開始するために活性化されるように0以上であれば- > 
        <! -そして、初めて要求を開始するには来て、0未満- > 
        の<load-on-startup> 1 </のload-on-startup>。
    </サーブレット> 
    <サーブレットを-mapping> 
        の<servlet-name>のブート-CRM </サーブレット名> 
        <! -すべての要求がspringMVCを入力する- > 
        。<URLパターン>アクション* </ URLパターン>
    </のservlet-mapping>
        
 
</ web-app>の

 

アクセスパス:HTTP:// localhostを:8080 / art_test / CON / test.action(プロジェクト名を追加することを忘れないでください)

 

 

おすすめ

転載: www.cnblogs.com/withbear/p/11850106.html