スプリングMVCフレーム構築プロセス

6349 ワード

1、Dynamic Web Projectを作成する
2、スプリングとスプリングmvcの導入に必要なファイル
3、web.xmlファイルの設定
3.1 springコンテキストコンテナを傍受する
3.2 springのxmlファイルをspringのコンテキストコンテナにロードする(spring-context.xml)
3.3 spring MVCを配置するDisppatch Servlet
3.4 spring MVCを読み込むxmlからspringの文脈容器(spring MVC-context.xml)
3.5 Displatch Servletを配置するために必要なurl(HTTPを固定したフォーマットは*.doのようです)
4、springを配置するxmlファイル
主にリンクデータベースなどの情報を設定します.
5、スプリングMVCのxmlファイルを配置する
5.1 springのグローバルプロファイルを読み込みます.
5.2 指定されたパケットのスキャンのすべてのクラスは注釈が有効です.
5.3 SprigMVCのビューレンダリングを設定します.
6、Controller(TestController.java)を書きます.
6.1 @コントローラ
6.2 @Request Mapping()
7、jspファイルを書く
実行フロー個人まとめ:要求を受けたら、sprigMVCプロファイルで指定されたカバンの種類をスキャンし、controllerの注釈@Request Mapping(「test」)に従って対応するjspファイルを見つけます.
web.xmlファイル
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
	<display-name></display-name>
	
	<!--1.    spring      -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 2.   spring xml spring       -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-context.xml</param-value>
	</context-param>

	<!-- 3.   spring MVC DispatcherServlet -->
	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!--4.    spring MVC xml spring       -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/classes/springMVC-context.xml</param-value>
		</init-param>
		<!--      servlet -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<!--5.   DispatcherServlet       url -->
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

</web-app>
spring-context.xml(これは簡単な例で、データベース接続などの操作に関係していないので、このファイルは何も構成しなくてもいいです.)
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
                http://www.springframework.org/schema/context  
                 http://www.springframework.org/schema/context/spring-context-3.2.xsd  
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">  
    <!-- Root Context: defines shared resources visible to all other web components -->  
  
       
</beans>  
スプリングMVC-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
				http://www.springframework.org/schema/aop
				http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!--   Spring        -->
	<beans:import resource="spring-context.xml" />
	
	<!-- SpringMVC   -->
	
	<!--   component-scan  Spring  org.swinglife.controller      , Spring        -->
	<context:component-scan base-package="com.liuyunlong.controller"></context:component-scan>
	
	<!--   SpringMVC      ,      :/    .jsp        /page/<method   >.jsp  -->
	<beans:bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/page/" p:suffix=".jsp">
		</beans:bean>


</beans:beans>
TestController.java
package com.liuyunlong.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
	@RequestMapping("test")
	public String test(){
		return "test";
	}
}
jspファイル
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'test.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    Welcome to springMVC <br>
  </body>
</html>