简要思路:
在web.xml中配置好SpringMVC入口,让SpringMVC中的DispatcherServlet拦截所有请求。
接着在index.jsp页面中点开超链接,发起一个welcome请求(程序员开发)。
DispatcherServlet拦截到这个welcome请求,通过匹配@RequestMapping发给相应的处理器SpringMVCHandler(程序员开发)。SpringMVCHandler返回结果给DispatcherServlet。
DispatcherServlet让视图处理器(在spring.xml配置)处理后跳转到success.jsp页面。
【目录】
一、前期准备
二、配置SpringMVC
一、前期准备
1.新建Web工程:HelloSpringMVC。

2.准备好index.jsp和success.jsp。
在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>
<!--
项目中可以同时兼容 SpringMVC和Servlet
如果SpringMVC拦截welcome请求,SpringMVC就会找包含welcome的@RequestMapping映射进行处理
如果没被SpringMVC拦截,就会交给普通Servlet处理,普通Servlet就会交给url-parttern或者/@WebServlet处理
-->
<a href="welcome">hello SpringMVC</a>
</body>
</html>
在WebContent文件夹下面新建文件夹views。在文件夹views内success.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>
success!
</body>
</html>
二、配置SpringMVC
1.导入常用jar包。

2.在src目录下面新建配置文件springmvc.xml。
导入常用约束(参看Eclipse安装STS插件使用更便捷的导入方式。)。

3.配置SpringMVC的入口,让SpringMVC介入程序。
使用普通的Servlet运行流程是这样的:请求-<url-pattern>-交给对应的Servlet去处理。而现在想用SpringMVC代替普通的Servlet,就需要在web.xml中配置一个SpringMVC自带的Servlet(前端控制器DispatcherServlet),以通知程序。
通过以下配置,拦截所有请求,交给SpringMVC处理:
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定springmvc配置文件的路径。
如果要省略,必须放到 默认路径:/WEB-INF/xx-servlet.xml(xx是Servlet的名字)
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- tomcat启动项目时第一位加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 指定哪些请求要进入这个servlet -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!-- /:拦截一切请求,注意不是 /*
/user:拦截以
/user开头的请求
/user/abc.do :只拦截该请求
.action:只拦截 .action结尾的请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
如果安装了STS插件可以按“alt+/”快速生成DispatcherServlet的相关配置。
另外,在配置拦截器的时候,需要对<url-pattern>标签加深理解。例如:
<url-pattern>/</url-pattern>
拦截的时候会匹配到/login这样的路径型的url,不会匹配到模式为*.jsp这样的后缀型url
推荐博文:springmvc dispatcherServlet url-pattern的设置
springmvc.xml 中 设置/和/*区别
4.在src文件夹下面新建com.test.handler包,包内新建SpringMVCHandler.java。

SpringMVCHandler.java代码如下:
package com.test.handler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//这里采用注解方式配置
@Controller
//@RequestMapping(value="handler")在类上面可以添加映射,这样拦截的地址就是handler/welcome
public class SpringMVCHandler {
//通过@RequestMapping来指定控制器可以处理哪些请求
@RequestMapping("welcome") //映射名welcome
public String welcome() {
return "success" ;// 会通过springmvc配置文件视图解析器加入前缀后缀 - views/success.jsp
}
}
关于@RequestMapping更多的用法可以看这里超详细 Spring @RequestMapping 注解使用技巧。
5.在springmvc.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:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 扫描 有注解的包 -->
<context:component-scan base-package="com.test.handler"></context:component-scan>
<!--配置视图解析器(InternalResourceViewResolver) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
PS:常见的视图和解析器有:InternalResourceView、InternalResourceViewResolver。springMVC解析JSP时会默认使用InternalResourceView, 如果发现JSP中包含了JSTL语言相关的内容,则自动转为JSTL View(JSTL可以实现国际化操作,有兴趣的同学看JSTL 对国际化支持总结)。
进行测试。


至此,完成第一个SpringMVC测试项目。
网友评论