SpringMVC是一种使用[模型-视图-控制器]来设计并创建Web应用程序的开发模式
模型(Model):表示应用程序核心,比如在数据库中的数据记录列表
视图(View):表示显示数据(数据库记录),比如Java Server Pages(Java服务器页面)
控制器(Controller):处理输入(写入数据库的记录),接收请求数据并转发视图
前端控制器(DispatcherServlet):也称总控制器,将请求转发到具体的控制类
映射处理器(HandlerMapping):负责映射中央处理器转发给Controller时的映射策略
控制器(Controller):具体处理请求的控制器
ModelAndView:服务层返回的数据和视图层的封装类
视图解析器(ViewResolver):解析具体的视图
拦截器(Interceptors):负责拦截定义的请求并进行处理
1 . 发送请求(Send Message)
2 . 前端控制器(DispatcherServlet):将请求发送给具体的控制类
3 . 映射控制器(HandlerMapping):判断将请求发送给哪个类的控制器
4 . 控制器(Controller):根据映射控制器的判断结果执行后续操作
按照控制器名称映射查找:BeanNameUrlHandlerMapping
按照简单Url映射查找:SimpleUrlHandlerMapping
按照控制类的类名控制器映射查找:ControllerClassNameHandlerMapping(访问时首字母需要小写)
5 . 视图和模型层(ModelAndView)
6 . 视图解析器(ViewResolver)
7 . 视图层(View):JSP
8 . 完成响应
1 . 配置JAR包:将jar包拷贝到工程目录下的WEB-INF文件夹中的lib文件夹中(Eclipse)
2 . 配置前端控制器DispatcherServlet:WEB.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>springmvc001display-name>
<welcome-file-list>
<welcome-file>login.jspwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
web-app>
3 . 配置Spring核心文件:创建SpringMVC核心控制配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xmlns:tx="http://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/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<bean id="loginController" name="/login.do" class="com.pmzeroa.controller.LoginController">bean>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="login1.do">loginControllerprop>
props>
property>
bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/">property>
<property name="suffix" value=".jsp">property>
bean>
beans>
4 . 配置控制器:编写控制器类(使用注解)
登陆页面演示代码:
package com.pmzeroa.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class LoginController extends AbstractController{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
String userName = request.getParameter("userName");//获取账号
String userPw = request.getParameter("userPw");//获取密码
System.out.println("-----我是SpringMVC的控制器-----");
System.out.println("账号:" + userName);
System.out.println("密码:" + userPw);
ModelAndView mav = new ModelAndView("index");
//模拟调用Model
if("admin".equals(userName) && "123".equals(userPw)){
mav.addObject("message", "用户登录成功");
} else {
mav.addObject("message", "账号或密码错误");
}
return mav;
}
}
5 . 编写JSP页面并显示数据
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>springmvc002display-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>dispatcherservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>dispatcherservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
web-app>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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-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/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<mvc:annotation-driven/>
<context:component-scan base-package="com.pmzeroa.servlet"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/">property>
<property name="suffix" value=".jsp">property>
bean>
beans>
package com.pmzeroa.servlet;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
//用来标注SpringMVC的控制器类
@Controller
@RequestMapping(value="sys/")//定义命名空间
public class RegController {
//用户访问控制器类的方法的注解
@RequestMapping(value="reg.do", method=RequestMethod.GET)
public String reg() {
System.out.println("-----用户注册1-----");
return "index";
}
@RequestMapping(value="reg.do", method=RequestMethod.POST)
public String reg1() {
System.out.println("-----用户注册2-----");
return "index";
}
@RequestMapping(value="add.do")
public String add() {
System.out.println("-----添加信息-----");
return "index";
}
}
package com.pmzeroa.servlet;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//用来标注SpringMVC的控制器类
@Controller
@RequestMapping(value="sys/")//定义命名空间
public class RegController {
//用户访问控制器类的方法的注解
@RequestMapping(value="reg.do")
public String reg() {
System.out.println("-----用户注册-----");
return "index";
}
/** * 通过HttpServletRequest获取页面数据 * @return */
@RequestMapping("reg1.do")
public String reg1(HttpServletRequest request) {
String userId = request.getParameter("userId");
String userPw = request.getParameter("userPw");
System.out.println("账号:" + userId);
System.out.println("密码:" + userPw);
return "index";
}
}
/** * 获取姓名数据 * @param request * @return */
@RequestMapping("reg2.do")
public String reg2(String userName) {
System.out.println("姓名:" + userName);
return "index";
}
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>springmvc002display-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>dispatcherservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>dispatcherservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
/** * 获取多个数据,包含时间属性数据(需要添加时间属性编辑器) * @param request * @return */
@RequestMapping("reg3.do")
public String reg3(Integer userId, String userName, Integer userPw, Date userBrithday) {
System.out.println("账号:" + userId);
System.out.println("姓名:" + userName);
System.out.println("密码:" + userPw);
System.out.println("出生日期:" + userBrithday);
return "index";
}
/** * 添加时间属性编辑器 */
@InitBinder
public void InitBinder(ServletRequestDataBinder bin) {
bin.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>PMZeroAtitle>
head>
<body>
<form action="sys/reg4.do" method="post">
<h1>用户注册h1>
账号:<input type="text" name="userId"/><br/>
密码:<input type="password" name="userPw"/><br/>
姓名:<input type="text" name="userName"/><br/>
出生日期:<input type="text" name="userBrithday"/><br/>
兴趣爱好:
<input type="checkbox" name="xqs" value="绘画"/>绘画
<input type="checkbox" name="xqs" value="游戏"/>游戏
<input type="checkbox" name="xqs" value="网球"/>网球
<input type="checkbox" name="xqs" value="钢琴"/>钢琴
<input type="checkbox" name="xqs" value="动漫"/>动漫
<input type="submit" value="提交"/>
form>
body>
html>
/** * 获取复选框内的数据 * @param request * @return */
@RequestMapping("reg4.do")
public String reg4(String[] xqs) {
for(String xq:xqs) {
System.out.println("兴趣:" + xq);
}
return "index";
}
import java.util.Arrays;
import java.util.Date;
public class UserInfo {
private Integer userId;
private String userPw;
private String userName;
private Integer userAge;
private Date userBrithday;
private String[] xqs;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserPw() {
return userPw;
}
public void setUserPw(String userPw) {
this.userPw = userPw;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getUserAge() {
return userAge;
}
public void setUserAge(Integer userAge) {
this.userAge = userAge;
}
public Date getUserBrithday() {
return userBrithday;
}
public void setUserBrithday(Date userBrithday) {
this.userBrithday = userBrithday;
}
public String[] getXqs() {
return xqs;
}
public void setXqs(String[] xqs) {
this.xqs = xqs;
}
@Override
public String toString() {
return "UserInfo [userId=" + userId + ", userPw=" + userPw + ", userName=" + userName + ", userAge=" + userAge
+ ", userBrithday=" + userBrithday + ", xqs=" + Arrays.toString(xqs) + "]";
}
}
/** * 通过对象获取页面参数 * @param request * @return */
@RequestMapping("reg5.do")
public String reg5(UserInfo user) {
System.out.println(user);
return "index";
}
package com.pmzeroa.domain;
public class UserInfo {
private Integer userId;
private String userName;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
package com.pmzeroa.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.pmzeroa.domain.UserInfo;
@Controller
public class UserInfoController {
@RequestMapping("/query1.do")
public String queryUser1(HttpServletRequest request) {
//返回单一数据
String name = "明日香";
request.setAttribute("name", name);
return "index";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
<hr/>
1.HttpServletRequest 返回数据:<br/>
<form action="query1.do" method="post">
<input type="submit" value="查询"/>
form>
<br/>
name:${name }
<hr/>
body>
html>
import com.pmzeroa.domain.UserInfo;
@Controller
public class UserInfoController {
@RequestMapping("/query1.do")
public String queryUser1(HttpServletRequest request) {
//返回单一数据
String name = "明日香";
request.setAttribute("name", name);
//返回对象
UserInfo user = new UserInfo();
user.setUserId(002);
user.setUserName("明日香");
request.setAttribute("user", user);
return "index";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
<hr/>
1.HttpServletRequest 返回数据:<br/>
<form action="query1.do" method="post">
<input type="submit" value="查询"/>
form>
<br/>
name:${name }
<br/>
userId:${user.userId }
userName:${user.userName }
<hr/>
body>
html>
@RequestMapping("/query2.do")
public ModelAndView queryUser2() {
UserInfo user = new UserInfo();
user.setUserId(1001);
user.setUserName("淀真嗣");
Map<String, Object> map = new HashMap<String, Object>();
map.put("user2", user);
return new ModelAndView("index", map);
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
2.ModelAndView 返回数据:<br/>
<form action="query2.do" method="post">
<input type="submit" value="查询"/>
form>
<br/>
userId:${user2.userId }
userName:${user2.userName }
body>
html>
@RequestMapping("/query3.do")
public String queryUser3(Map<String, Object> map) {
UserInfo user = new UserInfo();
user.setUserId(1000);
user.setUserName("绫波丽");
map.put("user3", user);
return "index";
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
3.Map<String, Object> map 返回数据:<br/>
<form action="query3.do" method="post">
<input type="submit" value="查询"/>
form>
<br/>
userId:${user3.userId }
userName:${user3.userName }
body>
html>
Model相当于ModelAndView中的Model
@RequestMapping("/query4.do")
public String queryUser4(Model model) {
UserInfo user = new UserInfo();
user.setUserId(1);
user.setUserName("淀真嗣");
model.addAttribute("user4", user);
return "index";
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
4.Model 返回数据:<br/>
<form action="query4.do" method="post">
<input type="submit" value="查询"/>
form>
<br/>
userId:${user4.userId }
userName:${user4.userName }
body>
html>
@RequestMapping("/query5.do")
public String queryUser5(Model model) {
UserInfo user = new UserInfo();
user.setUserId(1);
user.setUserName("淀真嗣");
List<UserInfo> list = new ArrayList<UserInfo>();
list.add(user);
list.add(user);
list.add(user);
model.addAttribute("list", list);
return "index";
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
5.List 返回数据:<br/>
<form action="query5.do" method="post">
<input type="submit" value="查询"/>
form>
<br/>
list:${list }
<hr/>
body>
html>
获取登陆页面文本框的值
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>PMZeroAtitle>
<script type="text/javascript" src="js/jquery-3.3.1.js">script>
<script type="text/javascript"> $().ready(function(){ $("#button1").click(function(){ var userId = $("#userId").val(); if(userId.length !=0){ $.post("ajax1.do", {usrId:userId},function(data){ alert(data); }); } else { alert('请输入账号'); } }); }); script>
head>
<body>
<form action="">
账号:<input type="text" id="userId" name="userId"/>
<input type="button" id="button1" value="验证"/>
form>
body>
html>
在JSP编辑页面中需要对输入的数据进行条件判断:
package com.pmzeroa.controller;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RegController {
@RequestMapping("ajax1.do")
public void ajax1(String usrId, HttpServletResponse response) {
try {
System.out.println(usrId);
//响应输入
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
//进行条件判断
if("admin".equals(usrId)) {
out.println("账号已被注册!");
} else {
out.println("账号可以使用!");
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.pmzeroa.domain;
public class UserInfo {
private Integer userId;
private String userName;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public UserInfo(Integer userId, String userName) {
this.userId = userId;
this.userName = userName;
}
}
@RequestMapping("/ajax2.do")
//通过@ResponseBody将内容或者对象作为HTTP进行响应并返回
public @ResponseBody UserInfo ajax2(String userId) {
UserInfo user = new UserInfo();
if("1001".equals(userId)) {
user.setUserId(1001);
user.setUserName("淀真嗣");
} else {
user.setUserId(1002);
user.setUserName("明日香");
}
return user;
}
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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-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/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<mvc:annotation-driven/>
<context:component-scan base-package="com.pmzeroa.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/">property>
<property name="suffix" value=".jsp">property>
bean>
<bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8value>
list>
property>
bean>
beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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-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/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<mvc:annotation-driven/>
<context:component-scan base-package="com.pmzeroa.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/">property>
<property name="suffix" value=".jsp">property>
bean>
<bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8value>
list>
property>
bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringConverer"/>
<ref bean="jsonConverer"/>
list>
property>
bean>
beans>
废弃的:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
最新使用:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
@RequestMapping("/ajax3.do")//响应地址
public @ReponseBody List<UserInfo> ajax3() {
List<UserInfo> list = new ArrayList<UserInfo>();
UserInfo user1 = new UserInfo():
user1.setUserId(001);
user1.setUserName("淀真嗣");
list.add(user1);
UserInfo user2 = new UserInfo():
user2.setUserId(002);
user2.setUserName("明日香");
list.add(user2);
UserInfo user3 = new UserInfo():
user3.setUserId(003);
user3.setUserName("绫波丽");
list.add(user3);
return list;
}
JSP页面表单是使用二进制进行数据传输,所以如果传输图片格式的文件,需要进行格式配置
配置解析器,并同时可以限制上传文件的大小
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>springmvc005display-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>dispatcherservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>dispatcherservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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-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/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<mvc:annotation-driven/>
<context:component-scan base-package="com.pmzeroa.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/">property>
<property name="suffix" value=".jsp">property>
bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1024000">property>
bean>
beans>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>PMZeroAtitle>
head>
<body>
<form action="upload.do" method="post" enctype="multipart/form-data">
<h1>用户注册h1>
账号:<input type="text" name="userId"/><br/>
姓名:<input type="text" name="userName"/><br/>
头像:<input type="file" name="userImg"/><br/>
年龄:<input type="text" name="userAge"/><br/>
<input type="submit" value="注册"/>
form>
body>
html>
package com.pmzeroa.domain;
public class UserInfo {
private String userId;
private String userName;
private String url;
private Integer userAge;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Integer getUserAge() {
return userAge;
}
public void setUserAge(Integer userAge) {
this.userAge = userAge;
}
@Override
public String toString() {
return "UserInfo [userId=" + userId + ", userName=" + userName + ", url=" + url + ", userAge=" + userAge + "]";
}
}
package com.pmzeroa.controller;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.pmzeroa.domain.UserInfo;
@Controller
public class RegController {
@RequestMapping(value="upload.do",method=RequestMethod.POST)
public String upload(UserInfo user, HttpServletRequest req, Model model) {
try {
System.out.println(user);
//转换MultipartHttpServletRequest
MultipartHttpServletRequest request = (MultipartHttpServletRequest)req;
//获取目标文件
CommonsMultipartFile cmfile = (CommonsMultipartFile)request.getFile("userImg");
//获得文件并转化为byte[]
byte[] byte1 = cmfile.getBytes();
//获取文件名称
String oldfilename = cmfile.getOriginalFilename();
//获取文件格式名称
String suffix = oldfilename.substring(oldfilename.lastIndexOf("."));
//获取当前项目文件路径
String path = req.getSession().getServletContext().getRealPath("/");
//设置输出文件时间格式
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String filename = sdf.format(date);
//输出目标文件到项目路径
String url = path+"upload/"+filename+suffix;
System.out.println(url);
OutputStream os = new FileOutputStream(url);
//进行读写文件操作
os.write(byte1);
//关闭资源
os.flush();
os.close();
} catch (Exception e){
e.printStackTrace();
}
return "reg";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>PMZeroAtitle>
head>
<body>
<form action="upload.do" method="post" enctype="multipart/form-data">
<h1>用户注册h1>
账号:<input type="text" name="userId"/><br/>
姓名:<input type="text" name="userName"/><br/>
头像:<input type="file" name="userImg"/><br/>
年龄:<input type="text" name="userAge"/><br/>
<input type="submit" value="注册"/>
form>
<hr />
${user }
<hr />
下载:
<a href="${user.url }">下载a>
body>
html>
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
@Controller
public class RegController {
@RequestMapping(value = "/upload.do", method = RequestMethod.POST)
public String upload(UserInfo user, HttpServletRequest req, Model model) {
try {
System.out.println(user);
//转换MultipartHttpServletRequset
MultipartHttpServletRequset requset = (MultipartHttpServletRequset)req;
//获取文件:使用CommonsMultiparFile
CommonsMultipartFile cmfile = (CommonsMultipartFile)request.getFile("userImg");
//获取文件后转换为byte[]
byte[] byte1 = cmfile.getBytes();
//获取文件的后缀名称
String oldfilename = cmfile.getOriginalFilename();//文件名称
String suffix = oldfilename.substring(oldfilename.lastIndexOf("."));//后缀名称
//获取当前项目的路径
String path = req.getSession().getServletContext().getRealPath("/");
//设置文件名称:获取当前时间
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String filename = sdf.format(date);
String url = path + "upload/" + filename + suffix;
System.out.println(url);
//设置OutputStream
OutputStream os = new FileOutputStream(url);
os.write(byte1);
os.flush();
os.close();
//将页面上的文件信息返回到Model
user.setUrl("upload/" + filename + suffix);//将文件地址返回
model.addAttribute("user", user);//将当前user返回到页面
} catch (Exception e) {
e.printStackTrace();
}
return "reg";
}
}
Interceptor拦截请求是通过HandlerInterceptor接口来实现的,这个接口中主要包含了3个方法:
<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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>springmvc006display-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>dispatcherservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>dispatcherservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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-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/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<mvc:annotation-driven/>
<context:component-scan base-package="com.pmzeroa.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/">property>
<property name="suffix" value=".jsp">property>
bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<bean class="com.pmzeroa.interceptor.MyInterceptor">bean>
mvc:interceptor>
mvc:interceptors>
beans>
package com.pmzeroa.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("/test.do")
public String test() {
System.out.println("-----我是控制器-----");
return "index";
}
}
package com.pmzeroa.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class MyInterceptor implements HandlerInterceptor {
//afterCompletion
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("-----afterCompletion-----");
}
//postHandle
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("-----postHandle-----");
}
//preHandle
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("-----preHandle-----");
return true;
}
}