SpringMVC入門
來自: http://my.oschina.net/u/2245781/blog/608216
一、建立web項目SpringMVC并導入spring包
WebContent/WEB-INF/web.xm
<?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.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" 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>
二、控制器
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 下的任一方法都需要帶上這個命名空間。
(1)/mvc.action或/mvc執行的是 execute() 方法,這時根據springmvc-servlet.xml里配置,控制器將派發/WEB-INF/jsp/default_page.jsp頁面。execute() 方法的 @RequestMapping 注解缺省 value 值,在這種情況下,當訪問命名空間時默認執行的是這個方法。方法級別上的 @RequestMapping 標注是必須的,否則方法無法被正確訪問。
(2)/mvc/hello.action或/mvc/hello.action執行的是hello() 方法,這時控制器將派發/WEB-INF/jsp/hello.jsp頁面。類級別上的 @RequestMapping 標注不是必須的,在不寫的情況下,方法上定義的 URL 都是絕對地址,否則,方法上定義的 URL 都是相對于它所在的 Controller 的。
三、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.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> 恭喜你!測試成功 </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>
</ 三、運行結果


