1. 导入Jar包
注意:除了日志相关的包和Spring的基本jar包和aop包外,还需要web和webmvc的jar包。
2. 在 web.xml 中配置 DispatcherServlet
DispatcherServlet是SpringMVC的一个核心控制器,是一个Servlet。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param>
<param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
|
3. 加入 Spring MVC 的配置文件,配置自动扫描包
在src路径下创建配置文件applicationContext-mvc.xml,具体内容如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<context:component-scan base-package="com.shoto.springmvc.handlers"/>
</beans>
|
4. 编写处理请求的处理器,并标识为处理器
下面需要在包路径com.shoto.springmvc.handlers下创建HelloWorld请求处理器类,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.shoto.springmvc.handlers;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;
@Controller public class HelloWorld { @RequestMapping("/helloSpringMVC") public String hellowolrd() { System.out.println("Hello World"); return "success"; } }
|
5. 配置视图解析器
在配置文件applicationContext-mvc.xml文件中进行如下配置:
1 2 3 4 5 6 7 8
|
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean>
|
6. 编写视图
在/WEB-INF/pages/路径下编写视图页面success.jsp:
1 2 3 4 5 6 7 8 9 10 11 12
| <%@ 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>Success</title> </head> <body> <h2>Success page</h2> </body> </html>
|
7. 编写请求视图
1 2 3 4 5 6 7 8 9 10 11 12
| <%@ 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> <h3><a href="helloSpringMVC.action">Hello World</a></h3> </body> </html>
|
注意:href属性的值helloSpringMVC与处理器的处理方法helloworld的@RequestMapping(“/helloSpringMVC”)的值相同,它表明点击超链接会由该方法进行处理。