1. 开发工具准备
eclipse + jdk1.7
spring-framework-4.3.9.RELEASE
2. 新建Dynamic Web Project项目,命名为springmvc.
3. 导入依赖包
spring的依赖包
jstl标签的依赖包
commmons-loggling包
。这里只需要引入压缩包中的红框的jar包,javacdoc和sources的包不需要引入。
完整的lib包如下:
4. 完成后的项目结构如下:
5. 在web.xml中配置前端控制器
<?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"> <display-name>srpingmvc</display-name> <!-- 配置前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 加载配置文件(配置处理器映射器、处理器适配器、视图解析器、Handler处理器) --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
6. 配置springmvc.xml
配置处理器映射器、处理器适配器、视图解析器、Handler处理器
在src下新建config文件夹,并在其中新建springmvc.xml文件。
编辑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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd "> <!-- 配置处理器映射器 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 配置处理器适配器 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> <!-- 配置handler处理器 --> <bean name="/getList.action" class="com.king.controller.UserController"></bean> </beans>
7. 创建User对象模型
package com.king.pojo; public class User { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public User(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public User() { super(); } }
8. 编写UserController
新建类 UserController,实现Controller的接口,放到包com.king.controller下。
这里直接创造数据作为演示用,返回的ModelAndView是list.
package com.king.controller; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import com.king.pojo.User; public class UserController implements Controller{ @Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { System.out.println("start UserController"); List<User> list = new ArrayList<>(Arrays.asList( new User(1,"zhangsan",20), new User(1,"zhang2",22), new User(1,"zhang3",23), new User(1,"zhang4s",24) )); ModelAndView mv = new ModelAndView(); mv.addObject("list", list); mv.setViewName("page/list.jsp"); return mv; } }
9. 编写jsp页面用来展示list数据,注意添加taglib引用。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <table> <c:forEach var="user" items="${list}" varStatus="status"> <tr> <td>${user.id}</td><td>${user.name}</td><td>${user.age}</td> </tr> </c:forEach> </table> </body> </html>
10. 启动tomcat, 在浏览器地址栏输入localhost/springmvc/getList.action,显示如下。
至此,一个简单的springmvc测试完成。
11. 最后附上完整的项目结构如下: