springMVC01、02
基本概念
三层架构
MVC模型
SpringMVC
组件
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 乱码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数: 指定springmvc配置文件路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 设置生命周期: 服务器启动创建Servlet对象 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<!-- /* 拦截所有,包括静态资源 .PNG .CSS .JS .JSP ...
/ 拦截所有,不包括 .JSP 一般常用于互联网应用
*.do 一般在xx管理系统使用
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 扫描控制器 -->
<context:component-scan base-package="com.zzxx"/>
<!-- 同时注册处理器适配器和处理器映射器-->
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
//解除/方式对文件的过滤
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
@Controller
// 这个类中所有的处理器方法, 都会在路径上加统一的上级目录
@RequestMapping(path="/spring")
public class HelloController {
// <servlet-name>
// params = "username" : 表示请求中必须包含username参数
@RequestMapping(value={"/hello", "/hehe"}, method = {RequestMethod.POST, RequestMethod.GET}
, params = {"username=lisi", "age<100"},
headers = "user-agent")
public String hello() {
System.out.println("hello 控制器方法执行了!");
return "success";
}
请求响应流程
获得请求参数
简单类型
<!-- 发送请求, 参数为简单类型 id=1&username=zhangsan -->
<a href="params/simple?id=1&username=zhangsan">接收简单类型的参数</a><br>
@RequestMapping("/simple")
// id=1&username=zhangsan
// 将请求参数的key, 作为控制器的方法参数 即可
public String getSimpleParam(int id, String username) {
System.out.println("接收简单参数的控制器方法执行...");
System.out.println(id + ", " +username);
return "success";
}
实体类对象
public class Account {
private int id;
private String name;
private double money;
private User user;
<!-- 发送请求, 参数为对象类型[参数可以封装为一个完整的对象] -->
<form action="params/bean" method="post">
账号id: <input type="text" name="id"/> <br>
账号姓名: <input type="text" name="name"/> <br>
账号金额: <input type="text" name="money"/> <br>
<%-- 想要将以下两个参数值 封装到上面 account对象中的user属性里 --%>
用户id: <input type="text" name="user.id"/> <br>
用户姓名: <input type="text" name="user.username"/> <br>
<input type="submit" value="提交">
</form>
@RequestMapping("/bean")
// 表单: id=1&name=zhangsan&money=2000.0
// 将要封装的对象作为 控制器的方法参数
// 注意: 参数名key 和对象的属性名要一致
public String getBeanParam(Account account) {
System.out.println("接收对象类型的参数的控制器方法执行...");
System.out.println(account);
return "success";
数组类型
<form action="params/array" method="post">
<input type="checkbox" name="ids" value="1"/>
<input type="checkbox" name="ids" value="2"/>
<input type="checkbox" name="ids" value="3"/>
<input type="checkbox" name="ids" value="4"/>
<input type="checkbox" name="ids" value="5"/><br>
<input type="submit" value="提交">
</form>
@RequestMapping("/array")
// 数组/集合 int[] ids List<Integer> ids;
// ids=1&ids=2&ids=3&ids=4
// request.getParameterValues(ids);
public String getArrayParam(int[] ids) {
System.out.println(Arrays.toString(ids));
return "success";
}
List集合
<form action="params/list" method="post">
<input type="checkbox" name="ids" value="1"/>
<input type="checkbox" name="ids" value="2"/>
<input type="checkbox" name="ids" value="3"/>
<input type="checkbox" name="ids" value="4"/>
<input type="checkbox" name="ids" value="5"/><br>
<input type="submit" value="提交">
</form>
<form action="params/userList" method="post">
用户1id: <input type="text" name="userList[0].id"/> <br>
用户1姓名: <input type="text" name="userList[0].username"/> <br>
用户2id: <input type="text" name="userList[1].id"/> <br>
用户2姓名: <input type="text" name="userList[1].username"/> <br>
用户3id: <input type="text" name="userList[2].id"/> <br>
用户3姓名: <input type="text" name="userList[2].username"/> <br>
<input type="submit" value="提交">
</form>
public class QueryVo {
private List<Integer> ids;
private List<User> userList;
@RequestMapping("/list")
// 集合 List<Integer> ids;
// ids=1&ids=2&ids=3&ids=4
// request.getParameterValues(ids);
public String getListParam(QueryVo qv) {
System.out.println(qv.getIds());
return "success";
}
@RequestMapping("/userList")
// 集合 List<User> userList
// key
public String getUserListParam(QueryVo qv) {
System.out.println(qv.getUserList());
return "success";
}
Map
public class QueryVo {
private Map<String, User> userMap;
<form action="params/userMap" method="post">
one用户 id: <input type="text" name="userMap['one'].id"/> <br>
one用户 姓名: <input type="text" name="userMap['one'].username"/> <br>
two用户id: <input type="text" name="userMap['two'].id"/> <br>
two用户姓名: <input type="text" name="userMap['two'].username"/> <br>
<input type="submit" value="提交">
</form>
@RequestMapping("/userMap")
// {one=User1, two=User2}
public String getUserMapParam(QueryVo qv) {
System.out.println(qv.getUserMap());
return "success";
}
特殊
Date——需要自定义类型转换器
<!-- 注册 转换器服务工厂 -->
<bean id="cs2" class="org.springframework.context.support.ConversionServiceFactoryBean">
<!-- 注入自定义转换器对象 -->
<property name="converters">
<set>
<bean class="com.zzxx.converter.StringDateConverter"/>
</set>
</property>
</bean>
<form action="params/date">
生日: <input type="date" name="birthday"><br>
<input type="submit" value="提交">
</form>
@RequestMapping("/date")
// birthday=2020-10-07
// String -> Date Converter
public String getDateParam(Date birthday) {
System.out.println(birthday);
return "success";
}
Model——是 spring 提供的⼀个接⼝,该接⼝有⼀个实现类 ExtendedModelMap, 该类继承了 ModelMap,而 ModelMap 就是 LinkedHashMap 子类
@RequestMapping("/findAll")
public String findAll(Model model){
List<Account> list= accountService.findAllAccount();
model.addAttribute("list",list);
return "success";
想要使用传统的
@RequestMapping("/servletAPI")
// 想要使用 HttpServletRequest对象 和 HttpServletResponse对象 HttpSession对象
public String servletAPI(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
System.out.println(request);
System.out.println(response);
System.out.println(session);
return "success";
}
常用注解
PathVariable
restful风格URL——轻量,易于扩展
只有一个统一的上级目录,根据method属性中不同的提交方式进行区分;提交方式相同,根据参数区分
{id} 就是 url 占位符
@Controller
@RequestMapping("/path")
public class PathVariableController {
@RequestMapping(method = RequestMethod.GET)
public String findAll(){
System.out.println("findAll 方法执行了");
return "success";
}
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public String findById(@PathVariable("id") int id){
System.out.println("findById 方法执行了");
System.out.println(id);
return "success";
}
}
<a href="path/100">pathVariable 注解</a>
浏览器get方式提交:http://localhost:8080/my01_war/path/100
SessionAttribute
存入session域中,不加标签直接用model.addAttribute只有一次转发能获取到
@SessionAttributes({“username”, “password”,“age”})
@Controller
@RequestMapping("/session")
@SessionAttributes({"username", "password","age"})
public class SessionAttributeController {
/**
* 把数据存⼊ SessionAttribute
* Model 是 spring 提供的⼀个接⼝,该接⼝有⼀个实现类 ExtendedModelMap
* 该类继承了 ModelMap,⽽ ModelMap 就是 LinkedHashMap ⼦类
*/
@RequestMapping("/add")
public String testPut(Model model) {
model.addAttribute("username", "张三");
model.addAttribute("password", "123456");
model.addAttribute("age", 31);
System.out.println("存入了数据");
return "success";
}
@RequestMapping("/get")
public String testGet(ModelMap model) {
System.out.println("获取了数据:" + model.get("username") + ";" +
model.get("password") + ";" + model.get("age"));
return "success";
}
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<body>
${username}${password}${age}
</body>
或着不用标签参数改为HttpSession也能存入session域
@RequestMapping("/add")
public String testPut(HttpSession session) {
session.setAttribute("username", "张三");
session.setAttribute("password", "123456");
session.setAttribute("age", 31);
System.out.println("存入了数据");
return "success";
}
@RequestMapping("/get")
public String testGet(HttpSession session) {
System.out.println("获取了数据:" + session.getAttribute("username") + ";" +
session.getAttribute("password") + ";" + session.getAttribute("age"));
return "success";
}
响应数据
返回值分类
String类型返回值
// 1.String 类型返回值
@RequestMapping("/testString")
// Model 是 spring 提供的⼀个接⼝,该接⼝有⼀个实现类 ExtendedModelMap,该类继承了 ModelMap,⽽ ModelMap 就是 LinkedHashMap ⼦类
public String testStringReturn(Model model, ModelMap modelMap) {
System.out.println("testStringReturn 执行了!");
// 通常在请求转发之前, 会传递数据, 需要用到request域
model.addAttribute("msg", "hello");
modelMap.addAttribute("username", "zhangsan");
// 等同于 请求转发, 会使用视图解析器来解析视图路径
return "success";
}
void没有返回值
@RequestMapping("/testVoid")
public void testVoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("testVoid 执行了!");
// 使用request response 来页面跳转
// 转发
// request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request, response);
// 重定向
// response.sendRedirect(request.getContextPath() + "/error.jsp");
// 直接使用response来写出页面
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("你好!");
ModelAndView 返回值
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
System.out.println("testModelAndView 执行了!");
ModelAndView mav = new ModelAndView();
// 1.给mav 添加数据模型, 等同于model.addAttribute()
mav.addObject("msg", "modelAndView");
// 2.设置转发视图页面路径 -- 会经过视图解析器的处理
mav.setViewName("success");
return mav;
转发和重定向
总结:只有请求转发(逻辑视图名方式)和 ModelAndView会经过视图解析器
使用关键字方式的请求和转发:
@RequestMapping("/testStringRedirect")
public String testStringRedirect() {
System.out.println("testStringRedirect 执行了!");
// 重定向, 使用关键字 redirect, 不会进入视图解析器添加前缀和后缀
// response.sendRedirect(request.getContextPaht() + "/error.jsp")
return "redirect:/error.jsp";
}
@RequestMapping("/testStringForward")
public String testStringForward() {
System.out.println("testStringForward 执行了!");
// 转发, 使用关键字 forward, 不会进入视图解析器添加前缀和后缀
return "forward:/WEB-INF/pages/success.jsp";
}
@ResponseBody 响应 json 数据
@RequestMapping("/testJson")
@ResponseBody // 将返回值转换成json格式字符串写出
public User testJson(User user) {
System.out.println("testJson 执行了!");
user.setId(10);
// 将user对象转换成json写出给客户端
// jackson jsonlib fastjson gson
return user;
}
<head>
<script src="js/jquery-1.11.0.min.js"></script>
<script>
$(function() {
$("#btn1").click(function() {
$.ajax({
url: "user/testJson",
data: {id: 1, username:"张三"},
success: function(user) {
alert(user.id);
alert(user.username);
},
dataType: "json",
type: "get",
async: true
});});});
</script>
</head>
<button id="btn1" >发送ajax请求</button>
文件上传
传统文件上传(无spring)
@RequestMapping("/fileupload")
public String fileUpload(HttpServletRequest request) throws FileUploadException, IOException {
// 1.获得磁盘文件项工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2.获得文件上传的核心类对象
ServletFileUpload upload = new ServletFileUpload(factory);
// 3.解析请求, 获得表单项
List<FileItem> list = upload.parseRequest(request);
// 4.判断表单项是否是文件
for (FileItem item : list) {
if (item.isFormField()) {
// 普通表单项, 获得参数操作即可
} else {
// 5.文件项, 上传文件
// a.获得要上传的文件名
String filename = item.getName();
// a.1 文件名的处理
String uuid = UUID.randomUUID().toString();
uuid = uuid.replace("-", "");
filename = uuid + "_" + filename;
// b.定义上传文件的服务器目标目录
String path = request.getServletContext().getRealPath("/uploads");
// c.上传
InputStream in = item.getInputStream();
OutputStream out = new FileOutputStream(path + "/" + filename);
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
}
}
return "success";
}
SprignMVC传统文件上传
上传的⽂件和访问的应⽤存在于同⼀台服务器上
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
//解除/方式对文件的过滤
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
<!-- 配置文件解析器, id固定 multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大上限 -->
<property name="maxUploadSize" value="5242880"/>
</bean>
@RequestMapping("/fileupload2")
public String fileUpload2(MultipartFile upload, HttpServletRequest request) throws FileUploadException, IOException {
System.out.println("fileUpload2 的文件上传");
// 5.文件项, 上传文件
// a.获得要上传的文件名
String filename = upload.getOriginalFilename();
// a.1 文件名的处理
String uuid = UUID.randomUUID().toString();
uuid = uuid.replace("-", "");
filename = uuid + "_" + filename;
// b.定义上传文件的服务器目标目录
String path = request.getServletContext().getRealPath("/uploads/");
// c.文件复制
upload.transferTo(new File(path, filename));
return "success";
}
<FORM action="fileupload2" method="post" enctype="multipart/form-data" >
用户名:<input type="text" name="username"/><br>
选择文件: <input type="file" name="upload"/><br>
<input type="submit" value="提交"/>
</FORM>
跨服务器上传
两个tomcat服务器, tomcat 配置⽂件 webx.xml 中加入允许读写操作。
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!-- 此jar包是sun公司提供的 -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
/**
* 跨服务器上传文件
* 异常: 403代码, 文件服务器没有写入权限
* 400代码, URI/URL不支持中文 URIEncoder
* @param upload
* @return
*/
@RequestMapping("/fileupload3")
public String fileUpload3(MultipartFile upload) throws FileUploadException, IOException {
String file_server = "http://localhost:9090/springmvc02_fileserver_war/uploads/";
System.out.println("fileUpload3 的文件上传");
// 5.文件项, 上传文件
// a.获得要上传的文件名
String filename = upload.getOriginalFilename();
// a.1 文件名的处理
String uuid = UUID.randomUUID().toString();
uuid = uuid.replace("-", "");
filename = uuid + "_" + filename;
// filename如果是中文, 要处理过, 编码一下
filename = URLEncoder.encode(filename, "UTF-8");
// b.创建一个跨服务的Client对象
Client client = Client.create();
// c.获得上传文件的资源对象
WebResource resource = client.resource(file_server + filename);
// d.文件上传
resource.put(upload.getBytes());
return "success";
}
另一个project的webapp下准备uploads文件夹,其他全空
异常处理器
自定义异常类
public class CustomerException extends Exception {
public CustomerException() {
}
public CustomerException(String message) {
super(message);
}
自定义异常处理器
public class MyExceptionHandler implements HandlerExceptionResolver {
/**
* @param handler 抛出异常的处理器对象
* @param ex 抛出的异常(所有的)
*/
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) {
System.out.println(handler);
// 只想处理 CustomerException
if (ex instanceof CustomerException) {
ModelAndView mav = new ModelAndView();
mav.addObject("msg", ex.getMessage());
mav.setViewName("error");
return mav;
}
return null;
}
加入spring容器
<bean class="com.zzxx.utils.MyExceptionHandler"/>
@Controller
public class UserController {
@RequestMapping("/testException")
public String testException(Integer id) throws CustomerException {
if (id < 10) {
throw new CustomerException("该商品已下架!");
} else if (id < 20) {
throw new NullPointerException("这是代码出错了!");
}
return "success";
}
拦截器
AOP 思想的具体应⽤, ⽤于对处理器进行预处理和
后处理例。如系统需要权限拦截
拦截器和过滤器区别:
- 过滤器是 servlet 规范中的⼀部分,任何 java web 工程都可以使用。拦截器是 SpringMVC 框架自己的,只有使用了 SpringMVC 框架的工程才能用。
- 过滤器在 url-pattern 中配置了/*之后,可以对所有要访问的资源拦截。
拦截器它是只会拦截访问的控制器方法,如果访问的是 JSP、HTML、CSS、Image 或者 JS是不会进行拦截的。
要想自定义拦截器, 要求必须实现:HandlerInterceptor 接口。
public class MyInterceptor1 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("1的preHandle执行了");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("1的postHandle执行了");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("1的afterCompletion执行了");
}
配置拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>需要拦截的URL
<mvc:exclude-mapping path="/user/login"/>需要排除的URL
<bean id="MyInterceptor1" class="com.zzxx.interceptor.MyInterceptor1"/>
</mvc:interceptor>
</mvc:interceptors>
拦截器的放行:如果有下⼀个拦截器就执⾏下⼀个,如果该拦截器处于拦截器链的最后⼀个,则执行控制器中的⽅法。
验证用户是否登录案例
自定义拦截器
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 判断用户是否登录
HttpSession session = request.getSession();
Object user = session.getAttribute("user");
if (user == null) {
// 如果没有登录, 不放行, 跳转到登录页面
response.sendRedirect(request.getContextPath() + "/login.jsp");
return false;
}
// 如果登录了, 就放行
return true;
}
注册拦截器
<!-- 注册拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 要拦截的路径
/* 拦截根目录下所有的路径资源
/** 拦截根目录以及所有子目录中的路径资源
-->
<mvc:mapping path="/**"/>
<!-- 不进行拦截的路径 -->
<mvc:exclude-mapping path="/user/login"/>
<bean class="com.zzxx.web.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
控制器
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/login")
public String login(HttpSession session) {
System.out.println("登录成功!");
session.setAttribute("user", "zhangsan");
return "success";
}
@RequestMapping("/add")
public String add() {
System.out.println("添加记录成功!");
return "success";
}
直接访问/user/add受到拦截,无法添加记录
先访问/user/login注册后,可成功