コンテンツ
1.1ApplicationContextアプリケーションコンテキストの取得方法
1.2 Springは、アプリケーションコンテキストを取得するためのツールを提供します
1.4ContextLoaderListenerリスナーを構成する
1.5ツールを使用したアプリケーションコンテキストオブジェクトの取得
1.Spring統合Web環境
1.1ApplicationContextアプリケーションコンテキストの取得方法
アプリケーションコンテキストオブジェクトは、新しいClasspathXmlApplicationContext(スプリング構成ファイル)を介して取得されますが、Beanがコンテナーから取得されるたびに、新しいClasspathXmlApplicationContext(スプリング構成ファイル)を書き込む必要があります。これの欠点は、構成ファイルが複数回ロードされることです。アプリケーションコンテキストオブジェクトが繰り返し作成されます。
Webプロジェクトでは、ServletContextListenerを作成してWebアプリケーションの起動を監視できます。Webアプリケーションの起動時にSpring構成ファイルをロードし、アプリケーションコンテキストオブジェクトApplicationContextを作成して、最大のドメインservletContextドメインに格納できます。ドメインからアプリケーションコンテキストApplicationContextオブジェクトをどこからでも取得できること。
1.2 Springは、アプリケーションコンテキストを取得するためのツールを提供します
上記の分析を手動で実装する必要はありません。Springは、上記の関数をカプセル化するリスナーContextLoaderListenerを提供します。リスナーはSpring構成ファイルをロードし、アプリケーションコンテキストオブジェクトを作成して、ServletContextドメインに格納します。クライアントツールWebApplicationContextUtils、ユーザーは、アプリケーションコンテキストオブジェクトを取得します。
したがって、実行する必要があるのは2つだけです
。①web.xmlでContextLoaderListenerリスナーを構成します(スプリングとWebの座標をインポートします)。
②WebApplicationContextUtilsを使用して、アプリケーションコンテキストオブジェクトApplicationContextを取得します。
1.3Spring統合Webの座標をインポートします
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
1.4ContextLoaderListenerリスナーを構成する
<!--全局参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的监听器-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
1.5ツールを使用したアプリケーションコンテキストオブジェクトの取得
//java
ApplicationContext applicationContext =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
Object obj = applicationContext.getBean("id");
1.6知識の要点
Spring統合Web環境の手順
①ContextLoaderListenerリスナーを構成する②WebApplicationContextUtils
を使用してアプリケーションコンテキストを取得する
2.SpringMVCの概要
2.1SpringMVCの概要
SpringMVCは、MVC設計モデルを実装するJavaベースのリクエスト駆動型の軽量Webフレームワーク
であり、SpringFrameWorkのフォローアップ製品であり、SpringWebFlowに統合されています。
SpringMVCは最も主流のMVCフレームワークの1つになり、Spring3.0のリリースにより、Struts2を全面的に上回り、最高のMVCフレームワークになりました。一連のアノテーションを使用して、単純なJavaクラスを、インターフェースを実装せずに要求を処理するためのコントローラーにします。また、RESTfulプログラミングスタイルのリクエストもサポートしています。
2.2SpringMVCクイックスタート
要件:クライアントが要求を開始し、サーバーが要求を受信し、ロジックを実行して、ビュージャンプを実行します。
開発手順:
①SpringMVC関連の座標をインポートする
②SpringMVCコアコントローラーを構成するDispathcerServlet③
コントローラークラスを作成してページを表示
④注釈を使用してコントローラークラスのビジネスメソッドのマッピングアドレスを
構成する⑤SpringMVCコアファイルを構成するspring-mvc.xml法律
クライアントがリクエストを開始するテスト
コード表示:
1.SpringMVC関連の座標をインポートします
<!--Spring坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--SpringMVC坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
2.web.xmlでSpringMVCのコアコントローラーを構成します
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.コントローラーとビジネスメソッドを作成する
public class QuickController {
public String quickMethod(){
System.out.println("quickMethod running.....");
return "index";
}
}
4.ビューページindex.jspを作成します
<html>
<body>
<h2>Hello SpringMVC!</h2>
</body>
</html>
5.構成の注釈
@Controller
public class QuickController {
@RequestMapping("/quick")
public String quickMethod(){
System.out.println("quickMethod running.....");
return "index";
}
}
6.spring-mvc.xmlを作成します
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!--配置注解扫描--> <context:component-scan base-package="com.itheima"/>
</beans>
7.テストアドレスにアクセスします
http://localhost:8080/project/quick
2.3SpringMVCプロセス図
2.4知識の要点
SpringMVCの開発手順
3.SpringMVCコンポーネントの解像度
3.1SpringMVCの実行プロセス
3.2SpringMVCコンポーネント分析
3.3SpringMVCアノテーション分析
-
クラス、リクエストURLの第1レベルのアクセスディレクトリ。ここに書かれていない場合は、アプリケーションのルートディレクトリに相当します
-
メソッドに関しては、リクエストURLの第2レベルのアクセスディレクトリが、クラスで@ReqquestMappingとマークされた第1レベルのディレクトリと組み合わされて、アクセス仮想パスを形成します。属性:
-
value :要求されたURLを指定するために使用されます。パス属性と同じ効果があります
-
method :リクエストのメソッドを指定するために使用されます
-
params :リクエストパラメータを制限する条件を指定するために使用されます。簡単な表現に対応しています。リクエストパラメータのキーと値は、設定と完全に同じである必要があります例: params = {"accountName"} 、リクエストパラメータにaccountName params = {"moeny!100"}が必要であることを示し、リクエストパラメータの金額を100にすることはできないことを示します
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
3.4SpringMVCのXML構成解析
ビューリゾルバー
org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.I
nternalResourceViewResolver
REDIRECT_URL_PREFIX = "redirect:" --重定向前缀
FORWARD_URL_PREFIX = "forward:" --转发前缀(默认值)
prefix = ""; --视图名称前缀
suffix = ""; --视图名称后缀
<!--配置内部资源视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property>
</bean>