jackson
开发步骤:
1.引入jackson的相关依赖
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.8.9version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.8.9version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>2.8.9version>
dependency>
2.@ResponseBody
package com.baizhi.controller;
import com.baizhi.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/json")
public class JsonController {
@RequestMapping("/tf")
@ResponseBody //自动转换为json串
public User transfer() throws Exception{
User user = new User("1", "小白", "12345", 18);
return user;
}
@RequestMapping("/tf1") //List集合
@ResponseBody //自动把返回的结果转换为json串 jackson
public List<User> transfer1() throws Exception{
List<User> users= new ArrayList<User>();
User user = new User("1", "小白", "12345", 18);
User user1 = new User("2", "小黑", "12345", 18);
User user2 = new User("3", "小红", "12345", 18);
users.add(user);
users.add(user1);
users.add(user2);
return users;
}
}
i.引入相关依赖
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.6version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.1version>
dependency>
ii.编码
package com.baizhi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.File;
@Controller
@RequestMapping("/upload")
public class UploadController{
@RequestMapping("/u") //接收上传文件
public String upload(MultipartFile upload, HttpSession session) throws Exception{
ServletContext servletContext = session.getServletContext();
//获取目标文件夹的路径
String realPath = servletContext.getRealPath("/upload");
//获取上传文件的名字
//创建与上传文件 同名的文件 此时文件为空
File file = new File(realPath + "/" + upload.getOriginalFilename());
//把上传文件转入空的文件
upload.transferTo(file);
return "uploadOk";
}
}
iii.配置文件上传视图解析器
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
bean>
手工设置文件上传大小:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10240000">property>
bean>
编码:
@RequestMapping("/down") //返回值 下载的文件封装到该对象中 filename接收文件名
//5SSM整合.wmv
public ResponseEntity<byte[]> download(String filename,HttpSession session) throws Exception{
//获取目标upload的路径
ServletContext servletContext = session.getServletContext();
String realPath = servletContext.getRealPath("/upload");
//file没有则创建 有则代表此文件 5SSM整合.wmv
File file = new File(realPath + "/" + filename);
//把文件转换为字节数组
byte[] bytes = FileUtils.readFileToByteArray(file);
//设置下载时的请求头
HttpHeaders httpHeaders = new HttpHeaders();
//解决下载完成后中文乱码问题 下载时的编码
String downname = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
//下载文件之后的打开方式
httpHeaders.setContentDispositionFormData("attachment",downname );
//设置下载的方式 //采用流的方式进行配置
httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//告知SpringMVC框架状态已经准备好
return new ResponseEntity<byte[]>(bytes,httpHeaders, HttpStatus.CREATED);
}
作用:统一管理异常 提高用户体验度
开发步骤:
1.impelements HandlerExceptionResolver 接口
package com.baizhi.exception;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GlobalException implements HandlerExceptionResolver {
/*
* ModelAndView 既可以替换request作用域传递数据 又可以跳转页面
*
* */
@Override //捕捉到所有的异常
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
String message=null;
//判断异常是什么类型
if (e instanceof LoginException){
message = e.getMessage();
}else if (e instanceof RegisterException){
message=e.getMessage();
}
ModelAndView modelAndView = new ModelAndView();
//把对应的异常信息 放置在modelAndView中
modelAndView.addObject("message",message);
//跳转页面
modelAndView.setViewName("exception");
return modelAndView;
}
}
2.在SpringMVC配置文件中配置全局处理异常类的信息
<!--配置全局异常处理的信息-->
<bean id="e" class="com.baizhi.exception.GlobalException">
</bean>
3.准备对应的jsp
开发步骤:
1.implements HandlerInterceptor
2.配置拦截器的相关内容
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/ic/**"/>
<mvc:exclude-mapping path="/ic/B.do"/>
<bean id="interceptor" class="com.baizhi.interceptor.MyInterceptor">bean>
mvc:interceptor>
mvc:interceptors>