SpringMVC配置入門
我的開發環境
開發工具: springsource-tool-suite-2.9.0
JDK版本: 1.6.0_29
tomcat版本:apache-tomcat-7.0.26
本文地址:http://www.cnblogs.com/sunang/p/3419544.html 轉載請注明出處^_^
本文要注意的點已经用 標注,請大家要特別注意。
go!
step1.在你的開發環境新建一個工程,引入jar包
Maven代碼:
<!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.4.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上下文配置文件
在step2中,我們配置了DispatcherServlet上下文配置文件的路徑為conf/spring/spring-servlet.xml,所以在src/main/resources/conf/spring目錄下新建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="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <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文件
在step3中,我們配置了包掃描路徑為"www.asuan.com",所以在src/main/java目錄下新建包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 { @RequestMapping("/helloWorld")//url字段名 public String helloWorld() { return "helloWorld";//jsp文件名 } }
視圖解析路徑設置為WEB-INF/jsp,所以在WEB-INF/jsp目錄下新建helloWorld.jsp文件(文件名為controller方法中返回的字符串),代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>Hello World!</h2> </body> </html>
step5.運行與調試
將工程部署到tomcat并運行,在瀏覽器輸入:http://localhost:8080/你設置的工程名/helloWorld.htm
運行結果:
complete!
posted on 2013-11-11 00:11 bangdikka 阅读(2387) 评论(10) 编辑 收藏 举报