1、創建web項目SpringMVC並導入spring包html
WebContent/WEB-INF/web.xmjava
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>spring mvc</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--configure the setting of springmvcDispatcherServlet and configure the mapping --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!-- <load-on-startup>1</load-on-startup> --> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
src/springmvc-servlet.xmlweb
<?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" 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/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- scan the package and the sub package --> <context:component-scan base-package="test.SpringMVC"/> <!-- don't handle the static resource --> <mvc:default-servlet-handler /> <!-- if you use annotation you must configure following setting --> <mvc:annotation-driven /> <!-- configure the InternalResourceViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 前綴 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 後綴 --> <property name="suffix" value=".jsp" /> </bean> </beans>
2、控制器spring
package test.SpringMVC; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/mvc") public class mvcController { @RequestMapping public String execute(){ return "default_page"; } @RequestMapping("/hello") public String hello(){ return "hello"; } }
@RequestMapping 有兩種標註方式,一種是標註在類級別上,一種是標註在方法級別上。
標註在方法上時,value 表示訪問該方法的 URL 地址。標註在類上時,value 至關於一個命名空間,即訪問該 Controller 下的任一方法都需要帶上這個命名空間。spring-mvc
(1)/mvc.action或/mvc執行的是 execute() 方法,這時根據springmvc-servlet.xml裏配置,控制器將派發/WEB-INF/jsp/default_page.jsp頁面。execute() 方法的 @RequestMapping 註解缺省 value 值,在這種狀況下,當訪問命名空間時默認執行的是這個方法。方法級別上的 @RequestMapping 標註是必須的,不然方法沒法被正確訪問。mvc
(2)/mvc/hello.action或/mvc/hello.action執行的是hello() 方法,這時控制器將派發/WEB-INF/jsp/hello.jsp頁面。類級別上的 @RequestMapping 標註不是必須的,在不寫的狀況下,方法上定義的 URL 都是絕對地址,不然,方法上定義的 URL 都是相對於它所在的 Controller 的。app
3、jsp頁面jsp
WebContent/WEB-INF/jsp/default_page.jsp測試
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> 恭喜你!Spring MVC default page測試成功! </body> </html>
WebContent/WEB-INF/jsp/hello.jspui
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> 恭喜你!測試成功 </body> </html>
WebContent/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> <ul> <li><a href="mvc/hello.action">Spring MVC HelloWorld!</a></li> <li><a href="mvc">Spring MVC 默認頁面 !</a></li> </ul> </body> </
3、運行結果
參考
http://jinnianshilongnian.iteye.com/blog/1631021
http://www.cnblogs.com/sunniest/p/4555801.html
http://www.cnblogs.com/Ming8006/p/6346712.html?utm_source=itdadao&utm_medium=referral