1.SpringMVC文件上传
1.1 多文件上传
- 多文件上传 与 单文件上传 类似
- 步骤:
- 表单:
<form method="post" enctype="multipart/form-data"> <!--提供多个input,允许选择多个上传文件--> <input type="file" name="images" /> <input type="file" name="images" /> </form>
- 控制器:方法参数
public String upload(List<MultipartFile> images) { }
1.2 JSON
- 语法:
//对象 var obj = { key:value, .... } //数组 var arr = [元素,....]
- springmvc底层使用 jackson Java对象和JSON数据转换
- 转换核心注解
//请求:JSON数据 --> Java对象 @RequestBody ,修饰在方法参数中 //响应:Java对象 --> JSON 数据 @ResponseBody,1)修饰方法前面当前方法转换成JSON;2)修饰类前面,类中所有方法返回JSON。
- 数据限定注解
//忽略 @JsonIgnore //日期转换 @JSONFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
2. RESTFul
2.1 什么是RESTFul
- RESTFul编写程序一种风格,不是协议。
- 对HTTP协议诠释(落地方案),请求方式的落地
http协议共7种,常见4种,最长使用2种 get post put delete
- RESTFul对服务器程序操作,进行约定。
- 每一个操作组成:请求路径 + 请求方式 。 (一个路径,因为请求方式的不同,可以完成不同操作)
- 数据传输方式:JSON数据
// 传统开发路径 查询:http://localhost:8080/user/selectAll.action 添加:http://localhost:8080/user/addUser.action 修改:http://localhost:8080/user/updateUser.action 删除:http://localhost:8080/user/deleteUser.action // RESTFul风格路径 查询:get http://localhost:8080/user/ 详情:get http://localhost:8080/user/123 添加:post http://localhost:8080/user/ 修改:put http://localhost:8080/user/ 删除:delete http://localhost:8080/user/123
2.2 使用
2.2.1 步骤
- 步骤1:创建新项目 (day15_mvc_restful)
- 步骤2:导入jar包:spring 、spring mvc、jackson、mybatis
- 步骤3:配置类,
- spring mvc 配置
- 启动配置类,前端控制器没有扩展名,由
*.action
改成/
- 步骤4:编写controller,完成增删改查
- 4.1 类级别:
@Controller 、@ResponseBody //--> 合成注入 @RestController
- 4.2 方法级别:@RequestMapping(value=“路径”, method=RequestMethod.GET /POST/PUT/DELETE)
//传统写法 @RequestMapping(value="路径", method=RequestMethod.GET /POST/PUT/DELETE) //-->各种请求方式的简化写法 @GetMapping @PostMapping @PutMapping @DeleteMapping @PathVariable //用于获得路径参数
- 4.3 方法返回值
//根据约束,基于RESTFul风格,方法的的返回值,建议使用 ResponseEntity 类型 // ResponseEntity 用于封装返回值信息,含状态码 // 返回200状态码,ResponseEntity.ok("添加成功"); // 其他状态码,new ResponseEntity<>("添加成功", HttpStatus.UNAUTHORIZED)
2.2.2 实现
- 配置类
- 控制器
package com.czxy.rest.controller; import com.czxy.rest.domain.User; import io.swagger.models.Response; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * @author manor * */ @RestController @RequestMapping("/user") public class UserController { /** * 查询所有 * @return */ //等效 @RequestMapping(method = RequestMethod.GET) @GetMapping public ResponseEntity<List<User>> list() { List<User> list = new ArrayList<>(); list.add(new User(1,"jack","1234", new Date())); list.add(new User(2,"肉丝","6666", new Date())); list.add(new User(3,"tom","loverose", new Date())); return ResponseEntity.ok(list); } /** * 添加用户 * @param user * @return */ @PostMapping(produces = "application/json;charset=UTF-8") public ResponseEntity add(@RequestBody User user) { System.out.println(user); return ResponseEntity.ok("添加成功"); } /** * * @param user * @return */ @PutMapping public ResponseEntity<String> update(@RequestBody User user) { System.out.println(user); return ResponseEntity.ok("update success"); } /** * 删除 * @param userId * @return */ @DeleteMapping("/{id}") public ResponseEntity<String> delete(@PathVariable("id") String userId ) { System.out.println(userId); return ResponseEntity.ok("delete success"); } }
3. spring mvc 拦截器
3.1 概述
- spring mvc拦截器相当于 java web filter
- 在处理器执行过程中,进行拦截或处理
preHandler 拦截器处理前方法,如果返回true继续执行,如果返回false,表示被拦截。 postHandler 拦截器处理方式,controller执行完成后,将执行处理方法。 afterCompletion 完成方法,视图渲染完成触发。
3.2 入门案例
- 环境:项目、jar包、配置类
- 测试程序:controller、jsp
- 编写拦截器,对流程进行校验
- 编写配置文件
- 环境:项目、jar包、配置类
- 测试程序:controller、jsp
package com.czxy.inter.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @author manor * */ @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/selectAll") public String selectAll() { System.out.println("2 controller-->selectAll"); return "list"; } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 查询结果 <% System.out.println("4 jsp 输出"); %> </body> </html>
- 编写拦截器,对流程进行校验
package com.czxy.inter.interceptor; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author manor * */ @Component public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("1 拦截前"); //放行 return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("3 执行中"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("5 最后完成"); } }
- 编写配置文件,spring mvc 配置类,需要实现 WebMvcConfigurer 接口,重新 addInterceptors方法
package com.czxy.inter.config; import com.czxy.inter.interceptor.MyInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.servlet.config.annotation.*; import org.springframework.web.servlet.view.InternalResourceViewResolver; import javax.annotation.Resource; import java.nio.charset.Charset; /** * @author manor * */ @Configuration //配置类 @ComponentScan(basePackages = {"com.czxy.inter.controller","com.czxy.inter.interceptor"}) @EnableWebMvc public class SpringMVCConfig implements WebMvcConfigurer { @Resource private MyInterceptor myInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { InterceptorRegistration interceptorRegistration1 = registry.addInterceptor(myInterceptor); interceptorRegistration1.addPathPatterns("/**"); } /** * 视图解析器 * @return */ @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); // 前缀 viewResolver.setPrefix("/WEB-INF/pages/"); // 后缀 viewResolver.setSuffix(".jsp"); return viewResolver; } }
3.3 多拦截器
- 执行流程
- 多拦截器拦截的顺序,就是在配置类中配置的顺序。
4. SSM 整合:案例
4.1 需求
- 用户的增删改查
- SSM:spring mvc 、spring、mybatis
- view:jsp (非重点)
4.2 环境搭建
- 项目名:day15_ssm_ums
- 数据库:已有数据
# 创建数据库 CREATE DATABASE ssm_db2; # 使用数据库 USE ssm_db2; # 1.1 创建用户表 CREATE TABLE `user` ( `uid` VARCHAR(32) NOT NULL, `username` VARCHAR(20) DEFAULT NULL, #用户名 `password` VARCHAR(32) DEFAULT NULL, #密码 `name` VARCHAR(20) DEFAULT NULL, #昵称 `email` VARCHAR(30) DEFAULT NULL, #电子邮箱 `telephone` VARCHAR(20) DEFAULT NULL, #电话 `birthday` DATE DEFAULT NULL, #生日 `sex` VARCHAR(10) DEFAULT NULL, #性别 `state` INT(11) DEFAULT 0, #状态:0=未激活,1=已激活 `code` VARCHAR(64) DEFAULT NULL, #激活码 PRIMARY KEY (`uid`) ) ; # 1.2 初始化用户默认数据 INSERT INTO `user` VALUES ('u001','jack','1234','杰克','jack@czxy.com','13612345678','2015-11-04','男',0,NULL); INSERT INTO `user` VALUES ('u002','rose','1234','肉丝','rose@czxy.com','13612345679','2015-11-05','女',0,NULL); INSERT INTO `user` VALUES ('373eb242933b4f5ca3bd43503c34668b','ccc','ccc','aaa','bbb@store.com','15723689921','2015-11-04','男',0,'9782f3e837ff422b9aee8b6381ccf927bdd9d2ced10d48f4ba4b9f187edf7738'),('3ca76a75e4f64db2bacd0974acc7c897','bb','bb','张三','bbb@store.com','15723689921','1990-02-01','男',0,'1258e96181a9457987928954825189000bae305094a042d6bd9d2d35674684e6'),('62145f6e66ea4f5cbe7b6f6b954917d3','cc','cc','张三','bbb@store.com','15723689921','2015-11-03','男',0,'19f100aa81184c03951c4b840a725b6a98097aa1106a4a38ba1c29f1a496c231'),('c95b15a864334adab3d5bb6604c6e1fc','bbb','bbb','老王','bbb@store.com','15712344823','2000-02-01','男',0,'71a3a933353347a4bcacff699e6baa9c950a02f6b84e4f6fb8404ca06febfd6f'),('f55b7d3a352a4f0782c910b2c70f1ea4','aaa','aaa','小王','aaa@store.com','15712344823','2000-02-01','男',1,NULL);
- 导入jar包
- spring、spring mvc
- mybatis、spring整合mybatis
- druid 连接池
- jsp jstl(标准标签库)
4.3 导入配置类
- 创建对应的目录结构
4.3.1 MyBatis配置类
package com.czxy.ssm.config; import com.github.pagehelper.PageHelper; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.springframework.context.annotation.Bean; import tk.mybatis.spring.mapper.MapperScannerConfigurer; import javax.sql.DataSource; import java.util.Properties; public class MyBatisConfiguration { /** * 配置session工厂 * @param dataSource * @return * @throws Exception */ @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{ //1 创建 factoryBean SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); //2 设置数据 // 2.1 数据源 factoryBean.setDataSource(dataSource); // 2.2 驼峰命名 Configuration configuration = new Configuration(); configuration.setMapUnderscoreToCamelCase(true); factoryBean.setConfiguration(configuration); // 2.3 分页插件 Properties props = new Properties(); // 设置方言 props.setProperty("dialect", "mysql"); // 分页的同时进行count查询 props.setProperty("rowBoundsWithCount", "true"); // 分页合理化参数,pageNum<=0 时会查询第一页,pageNum>pages (超过总数时),会查询最后一页 props.setProperty("reasonable", "true"); // PageInterceptor pageInterceptor = new PageInterceptor(); // pageInterceptor.setProperties(props); PageHelper pageHelper = new PageHelper(); pageHelper.setProperties(props); factoryBean.setPlugins(new Interceptor[] {pageHelper}); //3 通过factorybean获得对应 return factoryBean.getObject(); } /** * 映射扫描器 * @return */ @Bean public MapperScannerConfigurer mapperScannerConfigurer(){ //1 创建 MapperScannerConfigurer mapperScanner = new MapperScannerConfigurer(); //2设置包 mapperScanner.setBasePackage("com.czxy.ssm.mapper"); return mapperScanner; } }
4.3.2 Spring配置类
- 数据源配置文件
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm_db2 jdbc.username=root jdbc.password=1234
- 配置类
package com.czxy.ssm.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.PropertySource; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; @ComponentScan(basePackages="com.czxy.ssm.service") @PropertySource("classpath:db.properties") @EnableTransactionManagement public class SpringConfiguration { /** * 获得properties文件中内容,并注入对应变量 */ @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; /** * 配置数据源 * @return */ @Bean public DataSource dataSource(){ DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(driver); druidDataSource.setUrl(url); druidDataSource.setUsername(username); druidDataSource.setPassword(password); return druidDataSource; } /** * 事务管理器 * @param dataSource * @return */ @Bean public DataSourceTransactionManager txManager(DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } }
4.3.3 Spring MVC配置类
package com.czxy.ssm.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @ComponentScan(basePackages="com.czxy.ssm.controller") //public class MvcConfiguration extends WebMvcConfigurationSupport { public class MvcConfiguration implements WebMvcConfigurer { /** * 视图解析器 * @return */ @Bean public InternalResourceViewResolver internalResourceViewResolver(){ InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); //前缀 jsp文件夹 viewResolver.setPrefix("/WEB-INF/pages/"); //后缀 jsp扩展名 viewResolver.setSuffix(".jsp"); return viewResolver; } }
4.3.4 启动配置类
package com.czxy.ssm.config; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.DispatcherServlet; import javax.servlet.FilterRegistration; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; public class WebInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { //1 配置spring工厂 AnnotationConfigWebApplicationContext application = new AnnotationConfigWebApplicationContext(); // 注册所有的配置类 application.register(MyBatisConfiguration.class); application.register(SpringConfiguration.class); application.register(MvcConfiguration.class); //2 post中文乱码 FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encoding", new CharacterEncodingFilter("UTF-8")); encodingFilter.addMappingForUrlPatterns(null, true, "/*"); //3 核心控制器 ServletRegistration.Dynamic mvcServlet = servletContext.addServlet("springmvc", new DispatcherServlet(application)); //mvcServlet.addMapping("*.action"); mvcServlet.addMapping("/"); mvcServlet.setLoadOnStartup(2); //tomcat启动时,执行servlet的初始化方法 } }
4.4 查询所有
4.4.1 需求
完成查询所有功能
4.4.2 步骤
- 步骤1:编写JavaBean(User)
- 提供字段 和 表类对应
- 添加MyBatis相关注解 @Id 等
- 步骤2:编写Mapper(UserMapper)
- 整合通用Mapper
- 步骤3:编写service
- 需要管理事务
- 步骤4:编写controller
- 处理请求路径
- 选择视图页面 list
- 步骤5:list.jsp 页面展示数据(非重点)
4.4.3 实现
- 步骤1:编写JavaBean(User)
- 提供字段 和 表类对应
- 添加MyBatis相关注解 @Id 等
@Entity(name="user") public class User { @Id private String uid; @Column(name="username") private String userName; private String password; private String name; private String email; private String telephone; private Date birthday; private String sex; private Integer state; private String code; // 省略getter和setter }
- 步骤2:编写Mapper(UserMapper)
- 整合通用Mapper
package com.czxy.ssm.mapper; import com.czxy.ssm.domain.User; import tk.mybatis.mapper.common.Mapper; /** * @author manor * */ public interface UserMapper extends Mapper<User> { }
- 步骤3:编写service
- 需要管理事务
package com.czxy.ssm.service; import com.czxy.ssm.domain.User; import java.util.List; /** * @author manor * */ public interface UserService { /** * 查询所有 * @return */ public List<User> selectAll(); }
package com.czxy.ssm.service.impl; import com.czxy.ssm.domain.User; import com.czxy.ssm.mapper.UserMapper; import com.czxy.ssm.service.UserService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; /** * @author manor * */ @Service @Transactional public class UserServiceImpl implements UserService { @Resource private UserMapper userMapper; @Override public List<User> selectAll() { return userMapper.selectAll(); } }
- 步骤4:编写controller
- 处理请求路径
- 选择视图页面 list
package com.czxy.ssm.controller; import com.czxy.ssm.domain.User; import com.czxy.ssm.service.UserService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import java.util.List; /** * @author manor * */ @Controller @RequestMapping("/user") public class UserController { @Resource private UserService userService; @RequestMapping("/selectAll") public String selectAll(Model model) { // 查询所有 List<User> list = userService.selectAll(); // 将查询结果存放request --> 模型 model.addAttribute("list", list); // 设置视图名 return "list"; } }
- 步骤5:list.jsp 页面展示数据(非重点)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> </head> <body> <table border="1" width="800"> <tr> <td>编号</td> <td>登录名</td> <td>昵称</td> <td>email</td> <td>电话</td> <td>操作</td> </tr> <c:forEach items="${list}" var="user"> <tr> <td>${user.uid}</td> <td>${user.userName}</td> <td>${user.name}</td> <td>${user.email}</td> <td>${user.telephone}</td> <td> 修改 删除 </td> </tr> </c:forEach> </table> </body> </html>
4.5 添加
4.5.1 需求
完成用户添加
4.5.2 步骤
- 步骤1:点击添加按钮,显示添加表单
- 步骤2:编写controller,处理添加功能
- 添加成功后,跳转到列表页面
- 添加失败后,转发添加页面,提示错误信息
- 步骤3:编写service,完成添加
4.5.3 实现
- 步骤1:点击添加按钮,显示添加表单
- 1.1 在list.jsp页面中,编写添加按钮
<a href="${pageContext.request.contextPath}/user/addUI.action">添加</a>
- 1.2 编程UserController显示jsp
/** * 显示页面 * @return */ @RequestMapping("/addUI") public String addUI() { return "add"; }
- 1.3 编写表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%-- 错误信息 --%> <font color="red"> ${msg}</font> <form action="${pageContext.request.contextPath}/user/add.action" method="post"> 用户名:<input type="text" name="userName" /> <br/> 密码:<input type="password" name="password" /> <br/> 昵称:<input type="text" name="name" /> <br/> <input type="submit" value="添加用户"/> </form> </body> </html>
- 步骤2:编写controller,处理添加功能
- 添加成功后,跳转到列表页面
- 添加失败后,转发添加页面,提示错误信息
/** * * @param user * @param model * @return */ @RequestMapping("/add") public String add(User user, Model model) { // 添加用户 boolean result = userService.add(user); if(result) { // 添加成功 return "redirect:/user/selectAll.action"; } else { // 添加失败 model.addAttribute("msg", "添加失败"); return "forward:/WEB-INF/pages/add.jsp"; } }
- 步骤3:编写service,完成添加(接口+实现类)
/** * 添加 * @param user * @return */ boolean add(User user);
@Override public boolean add(User user) { // 自动生成id String uuidString = UUID.randomUUID().toString().replace("-",""); user.setUid(uuidString); int insert = userMapper.insert(user); return insert == 1; }