相应的HandlerMapping对Web请求进行分析,然后调用匹配结果对应的Controller实现,具体到当前的场景就是我将要实现的
UserController。UserController处理完毕就视图名称userList和模型数据一同返回,然后DispatcherServlet则借助于相应的
ViewResolver,根据返回的视图名选择相应的视图(userList.jsp)并显示,这就是整个流程。下面是实现这个Web处理流程的
详细步骤,我是根据书上的例子改编成一个比较简单的例子哦!
(1)配置web.xml
需要将org.springframework.web.servlet.DispatcherServlet和org.springframework.web.context.ContextLoaderListener
通过<servlet>和<listener>元素添加到web.xml部署描述符文件中,另外处于其他目的,我们还可以添加相应的Filter和以及
ServletContextListener以处理字符编码和Log4j初始化配置等内容,这些完全根据当前的应用程序的需求情况来决定。
ContextLoaderListener的职责在于,它为整个的Web应用程序加载顶层的WebApplicationContext。该顶层的WebApplicationContext
主要用于提供应用所使用的中间层服务。我们做使用的数据源定义、数据访问点对象(DAO)定义、服务对象(Service)等,
都在该WebApplicationContext中注册。ContextLoaderListener加载的WebApplicationContext的默认配置文件路径为/WEB-INF/applicationContext.xml
DispatcherServlet是基于Spring MVC框架Web应用程序的Front Controller,它将负责几乎所有对应当前Web应用程序的Web请求的处理
它也使用了一个外部化的配置文件,用来配置SpringMVC框架在处理Web请求过程中所涉及的各个组件,包括Handlermapping定义
Controller定义、ViewResolver定义等。该外部化配置文件存在的默认路径也是/WEB-INF/,名称需要参照web.xml中定义的
DispatcherServlet的<servlet-name>来决定。
web.xml代码如下:
<display-name>SpringMVC_1</display-name>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<!-- <context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param> -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webName.root</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<servlet-name>controller</servlet-name>
</filter-mapping>
<listener>
<!-- ContextLoaderListener加载顶层的WebApplicationContext -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- <listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener> -->
<!-- web请求将被发送到DispatcherServlet进行处理 -->
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>userList.jsp</welcome-file>
</welcome-file-list>
在web.xml中将所有以.do结尾的请求模式都映射给了DispatcherServlet来处理,接下的事情就由DispatcherServlet接受了。
(2)WebApplicationContext文件的添加
<?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" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- BeanNameUrlHandlerMapping 强制我们的Handler的bean定义名称,必须去匹配视图中的链接路径
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
根据URL与controller的bean定义的名称进行匹配
<bean name="/userList.do" class="com.springMVC.controller.UserController">
<property name="userService" ref="userService"></property>
<property name="viewName" value="userList"></property>
</bean>
-->
<!-- SimpleUrlHandlerMapping -->
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!-- SpringMVC可用的HandlerMapping实现全都实现Ordered接口 -->
<property name="order" value="1"></property>
<property name="mappings">
<props>
<prop key="userList.do">userController</prop>
<prop key="userList.do">zhsi</prop>
</props>
</property>
</bean>
<bean name="zhsi" class="com.springMVC.controller.UserController">
<property name="userService" ref="userService"></property>
<property name="viewName" value="userList"></property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
注意这里的BeanNameUrlHandlerMapping和SimpleUrlHandlerMapping的区别,我是两个是实现过,原理很简单,这里就不再详述了
(3对象定义:我创建了一个用户对象User
public class User {
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public User(String name, String sex) {
super();
this.name = name;
this.sex = sex;
}
(4)定义相应的业务接口:我的service类只是简单的提供了获取用户对象的功能
public interface IUserService {
List<User>getUser();
}
(5)接口实现类
public class UserviceImpl implements IUserService{
@Override
public List<User> getUser() {
User u1=new User("zhsi","woman");
User u2=new User("cw","man");
List<User>userList=new ArrayList<User>();
userList.add(u1);
userList.add(u2);
return userList;
}
}
(6)实现对应的Controller并添加到配置文件
针对当前的用户请求,需要实现一个Controller来处理,通常情况下会扩展SpringMVC的org.springframework.web.servlet.mvc.AbstractController
来实现具体的Controller。代码如下:
public class UserController extends AbstractController{
private IUserService userService;
private String viewName;
public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
public String getViewName() {
return viewName;
}
public void setViewName(String viewName) {
this.viewName = viewName;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
List<User>userList=getUserService().getUser();
ModelAndView mav=new ModelAndView(getViewName());
mav.addObject("users", userList);
return mav;
}
}
mav.addObject("users", userList);中第一个参数是key,第二个参数是模型数据。
users 对应<c:forEach items="${users}" var="user">这里面的users
(7)实现相应的视图
在STS/WEB-INF/jsp/目录下创建一个userList.jsp文件代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table width="517" border="1" align="center" cellpadding="0"
cellspacing="0" bordercolor="#33FFFF">
<caption>用户</caption>
<!-- 定义表格的标题 -->
<tr bgcolor="#999900">
<td width="250">
<div align="center">User Name</div> <!-- <div></div>主要是用来设置涵盖一个区块为主 -->
</td>
<td>
<div align="center">User Sex</div>
</td>
</tr>
<c:forEach items="${users}" var="user">
<tr>
<td>
<div align="center">${user.name}</div>
</td>
<td>
<div align="center">${user.sex}</div>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
接下来就可以将项目部署到tomcat服务器上了,在部署到服务器上的时候一直出现404错误,可能是我的访问路径不正确,
后来访问成功的访问路径是:http://localhost:8080/SpringMVC_1/userList.do 记得后面是以do结尾的,可能之前我在
这里犯了错误。
至此就完成了一个简答的SpringMVC项目