SpringMVCエントリとインターセプター

   SSM最後のフレームspringmvcは、実際には、始めるために特に簡単です。しかし、私は、ビデオの深いソース昨日、ほとんどGGを参照してください。実際には、過去に学んだ多くのことは、後でもう一度それを読んで、今の本を持っている、と言っていないことを学ぶ、忘れます。いくつか多かれ少なかれ、右の私が使用して、堪能言うことができないときに注意してください。

  今週は予定今日、私は実際に完全なセットは、SSMプロジェクトである方法によって、明日を構築するために出て終了し、SSMプロジェクトをspringmvcします。水曜日レビューMavenの、木曜日レビューのRedis、金曜日は、SSM + Redisの統合を開始しました。来週SSHに従事するために、そして最終的にSpringBootを学びます。主なもの今月は学習フレームワークです。

  実際のコンテナクラスはspringmvc XmlWebApplicationContextです。私たちは、自動的にシンプルなために設定された新しいプロジェクトspringmvc機能のアイデアを使用する場合は、web.xmlファイルでは、非常に巧妙な、我々は一般的に、フィルタCharacterEncoderingFilter文字セット、起動パラメータコンテキスト-PARAM、春のコンテナを起動するために、春のコンテナに必要それはspringmvcコンテナを含む大規模なコンテナです。

  web.xmlの次のように構成されました

    

<?xmlのバージョン= "1.0"エンコード= "UTF-8" ?> 
< ウェブアプリのxmlns = "http://xmlns.jcp.org/xml/ns/javaee" 
         のxmlns:XSI = "のhttp://www.w3 .ORG / 2001 / XMLスキーマ・インスタンス" 
         のxsi:schemaLocationの=" http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd " 
         バージョン=" 3.1" > 
    < 文脈PARAM > 
        < PARAM名> contextConfigLocation </ PARAM名> 
        < PARAM値> /WEB-INF/applicationContext.xml <
    > 
    < 聞き手> 
        < リスナークラス> org.springframework.web.context.ContextLoaderListener </ リスナクラス> 
    </ リスナー> 
    < サーブレット> 
        < サーブレット名>ディスパッチャ</ サーブレット名> 
        < サーブレットクラス>組織。 springframework.web.servlet.DispatcherServlet </ サーブレットクラス> 
        < のload-on-startup > 1 </ のload-on-startup > 
        < INIT-PARAM > 
            <PARAM名> contextConfigLocation </ PARAM名> 
            < PARAM値>クラスパス:ディスパッチャ-servlet.xml </ PARAM値> 
        </ INIT-PARAM > 
    </ サーブレット> 
    < サーブレットマッピング> 
        < サーブレット名>ディスパッチャ</ サーブレット-name > 
        < のurl-pattern > * .FORM </ のurl-pattern > 
    </ のservlet-mapping > 
    < フィルタ> 
        < フィルタ名>エンコーダ</フィルタ名> 
        < フィルタクラス> org.springframework.web.filter.CharacterEncodingFilter </ フィルタクラス> 
        < INIT-PARAM > 
            < PARAM名>エンコード</ PARAM名> 
            < PARAM値> UTF-8 < / PARAM値> 
        </ INIT-PARAM > 
    </ フィルタ> 
    < フィルタマッピング> 
        < フィルタ名>エンコーダ</ フィルタ名> 
        <url-pattern > / *</ のurl-pattern > 
    </ のfilter-mapping > 
</ ウェブアプリ>
コードの表示

  次のように構成されたディスパッチャ-servlet.xml、即ちspringmvc構成、主な機能であります

  • コントローラ上のサン記述注釈、コンテナspringmvcに追加します
  • <MVC:annotataionドリブン/>デフォルトのHandlerMapping、HandlerAdatorを有効にします、
  • <MVC:インターセプタ>設定インターセプター
  • ビューリゾルバの設定は、この機能は、より一般的に使用されます
<?= "1.0"エンコード= "UTF-8" XMLバージョン?> 
< のxmlns = "http://www.springframework.org/schema/beans" 
       のxmlns:XSI = "http://www.w3.org/2001 / XMLスキーマ・インスタンス" 
       の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.xsd
 http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd」> 
    < 文脈:コンポーネント・スキャンベースパッケージ= "com.houjun.controller" > </ コンテキスト:コンポーネント・スキャン> 
    < MVC:注釈駆動型> </ MVC:注釈駆動型> 
</ >
コードの表示

  applicationContext.xmlを構成は、これは春のコンテナであり、私はノートを可能にするだけでここにいる、あまりにも多くのことを行うことができます

<?= "1.0"エンコード= "UTF-8" XMLバージョン?> 
< のxmlns = "http://www.springframework.org/schema/beans" 
       のxmlns:XSI = "http://www.w3.org/2001 / XMLスキーマ・インスタンス" 
       のxmlns:コンテキスト= "http://www.springframework.org/schema/context" 
       XSI:のschemaLocation =" http://www.springframework.org/schema/beansのhttp://www.springframework。 ORG /スキーマ/豆/春-beans.xsd
       http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd」> 
        < 文脈:コンポーネント・スキャンベースパッケージ= "com.houjun.pojo" > </ コンテキスト:コンポーネントスキャン> 
</ 豆類>
コードの表示

  シンプルなコントローラとして、

パッケージcom.houjun.controller。

輸入com.houjun.pojo.Student;
輸入com.houjun.pojo.User;
輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.context.ApplicationContext;
輸入org.springframework.context.support.ClassPathXmlApplicationContext;
輸入org.springframework.stereotype.Controller;
輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.context.ConfigurableWebApplicationContext;
輸入org.springframework.web.context.WebApplicationContext;

インポートのjavax.servlet.http.HttpServletRequest;

/ **
 * @Author:HouJun
 * @Date:2019年11月8日午後3時21分
 * @description:com.houjun.controller
 * @version :1.0
  * /
@コントローラ
@RequestMapping( "/学生" のパブリック クラスStudentController {
    @Autowired
    民間の学生学生。
    @Autowired
    民間のApplicationContextのApplicationContext。
    @Autowired
   プライベートConfigurableWebApplicationContext configurableWebApplicationContext。
    (@RequestMapping "/ハロー" 公衆{文字列こんにちは()
        System.out.println(「自動ジェクト」+ 学生)。
        System.out.println( "クラス名" + applicationContext.getClass()のgetName());
        文字列[] beanDefinitionNames = applicationContext.getBeanDefinitionNames()。
        {(beanDefinitionNamesストリングbeanDefinitionName)
            System.out.println(beanDefinitionName)。
        }
        System.out.println(「------------------------------------------- -------------------------------------------」);
        System.out.println( "クラス名" + configurableWebApplicationContext.getClass()のgetName());
        文字列[] beanDefinitionNames1 = configurableWebApplicationContext.getBeanDefinitionNames()。
        (文字列S:beanDefinitionNames1){
            System.out.println(S);
        }
        戻る「/success.jspを」
    }
}
コードの表示

  インターフェイスHandlerInterceptorAdapterを実装するために、次のように、切片を順次配置対象のXMLファイルにするために、インターセプタが複数存在してもよいニーズインターセプタインターセプタクラス

パッケージcom.houjun.inerceptor。

輸入org.springframework.web.servlet.ModelAndView。
輸入org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

インポートのjavax.servlet.http.HttpServletRequest;
インポートのjavax.servlet.http.HttpServletResponse;

/ **
 * @Author:HouJun
 * @Date:2019年11月11日午前9時58分
 * @description:com.houjun.inerceptor
 * @version :1.0
  * /

パブリック クラス UserInterceptorは延びHandlerInterceptorAdapterを{
    @オーバーライド
    パブリック ブール preHandleは(HttpServletRequestのリクエスト、HttpServletResponseの応答、オブジェクト・ハンドラ)がスロー{例外
        System.out.println(「カスタムインターセプタ1、コントローラ入る前に」);
         戻り値 をtrueにします
    }

    @オーバーライド
    公共 ボイド postHandle(HttpServletRequestのリクエスト、HttpServletResponseの応答、オブジェクト・ハンドラのModelAndViewのModelAndView)がスロー{例外
        System.out.println(「カスタムインターセプタ1の後、コントローラ、ビューに入る前」);
    }

    @オーバーライド
    公共 ボイド afterCompletionは、(HttpServletRequestのリクエスト、HttpServletResponseの応答、オブジェクト・ハンドラは、例外EX)はスロー{例外
        System.out.println(「カスタムインターセプタ1の後、コントローラ、ビューを入力した後」)。
    }
}
コードの表示

  ディレクトリ構造

 

 

  

>ライブラリが問題とTomcatコンテナの様々なを持っているかもしれませんが、ゆっくりと、少しずつ、これらを構成するために知っている - これらのものを使用すると、コンフィギュレーションプロジェクトStuctureがいることをWebプロジェクト、およびノー​​トを開始することができますの意味。

おすすめ

転載: www.cnblogs.com/houj/p/11833878.html