SpringMVC配置入門

Let's go!
step1.在開發環境新建一個Web工程,引入jar包,點擊圖片附件可查看所需的jar包,這裡給出Maven的引入代碼,在pom.xml文件中加入:
<!-- Spring MVC -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>3.2.5.RELEASE</version>
</dependency>
<!-- JSP -->
<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>jstl-api</artifactId>
    <version>1.2</version>
</dependency>

step2.編輯web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <!-- 配置SpringMVC前端Servlet控制器DispatcherServlet -->
    <servlet>
	<servlet-name>dispatcherServlet</servlet-name>
	<servlet-class>
             org.springframework.web.servlet.DispatcherServlet
         </servlet-class>
	<!-- 配置初始的DispatcherServlet上下文配置文件加載路徑 -->
	<init-param>
	    <param-name>contextConfigLocation</param-name>
	    <param-value>
                 classpath:conf/spring/spring-servlet.xml
             </param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
    </servlet>
         <!-- 讓DispatcherServlet處理所有以".htm"結尾的URL -->
    <servlet-mapping>
	<servlet-name>dispatcherServlet</servlet-name>
	<url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <!-- 配置DispatcherServlet上下文配置文件加載路徑 -->
    <context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
             classpath:conf/spring/spring-servlet.xml
         </param-value>
    </context-param>
    <!-- 配置上下文載入器 -->
    <listener>
	<listener-class>
             org.springframework.web.context.ContextLoaderListener
         </listener-class>
    </listener>
</web-app>

step3.編輯DispatcherServlet上下文配置文件
在web.xml中,我們配置了路徑conf/spring/以及文件名spring-servlet.xml.所以在工程的src/main/resources/conf/spring路徑下(如沒有該文件可創建一個)新建xml文件,命名為spring-servlet.xml.編輯代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置SpringMVC配置文件所需的xml文件解析模板 -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:cxf="http://cxf.apache.org/core"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="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://cxf.apache.org/core http://cxf.apache.org/schemas/core.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
   	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 配置控制器要掃描的包路徑 -->
    <context:component-scan base-package="www.asuan.com" />
    <!-- 配置視圖解析器 -->
    <bean id="viewResolver"		class="org.springframework.web.servlet.view.InternalResourceV         iewResolver">
	<property name="viewClass"			value="org.springframework.web.servlet.view.JstlView" />
	<!-- 解析器解析/WEB-INF/jsp/路徑下,以.jsp結尾的視圖文件 -->
	<property name="prefix" value="/WEB-INF/jsp/" />
	<property name="suffix" value=".jsp" />
    </bean>
</beans>

step4.編寫controller文件和jsp文件
在第三步中,我們配置了掃描包路徑www.asuan.com,所以新建一個包,命名為www.asuan.com.controller,在包下新建文件HelloWorldController.java,代碼如下:
package www.asuan.com.controller;

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

@Controller// 添加此註釋表明該類被控制器掃描
public class HelloWorldController {
    // URL映射路徑,用helloWorld.htm為路徑就可訪問該方法
    @RequestMapping("/helloWorld")
    public String helloWorld() {
        return "helloWorld";// 控制器返回的視圖文件名
    }
}

在WEB-INF/jsp路徑下新建helloWorld.jsp文件,代碼如下:
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

step5.運行調試
把工程部署到tomcat,并運行。
瀏覽器訪問地址:http://localhost:8080/工程名/helloWorld.htm
如果你看到Hello World!就說明小功告成啦!

猜你喜欢

转载自asuan.iteye.com/blog/1973701