写点什么

springboot 中如何使用拦截器,Javaweb 资料视频

  • 2021 年 11 月 10 日
  • 本文字数:2458 字

    阅读完需:约 8 分钟

/**


  • 在整个请求结束之后被调用,也就是在 DispatcherServlet 渲染了对应的视图之后执行

  • (主要是用于进行资源清理工作)


*/


@Override


public void afterCompletion(HttpServletRequest request, HttpServletResponse response,


Object object, Exception ex)


throws Exception {


// TODO Auto-generated method stub


}


public void returnErrorResponse(HttpServletResponse response, IMoocJSONResult result)


throws IOException, UnsupportedEncodingException {


OutputStream out=null;


try{


response.setCharact


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


erEncoding("utf-8");


response.setContentType("text/json");


out = response.getOutputStream();


out.write(JsonUtils.objectToJson(result).getBytes("utf-8"));


out.flush();


} finally{


if(out!=null){


out.close();


}


}


}


}


package com.imooc.controller.interceptor;


import java.io.IOException;


import java.io.OutputStream;


import java.io.UnsupportedEncodingException;


import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;


import org.slf4j.Logger;


import org.slf4j.LoggerFactory;


import org.springframework.web.servlet.HandlerInterceptor;


import org.springframework.web.servlet.ModelAndView;


import com.imooc.pojo.IMoocJSONResult;


import com.imooc.utils.JsonUtils;


public class TwoInterceptor implements HandlerInterceptor {


final static Logger log = LoggerFactory.getLogger(TwoInterceptor.class);


/**


  • 在请求处理之前进行调用(Controller 方法调用之前)


*/


@Override


public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {


if (true) {


returnErrorResponse(response, IMoocJSONResult.errorMsg("被 two 拦截..."));


}


System.out.println("被 two 拦截...");


return false;


}


/**


  • 请求处理之后进行调用,但是在视图被渲染之前(Controller 方法调用之后)


*/


@Override


public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView mv)


throws Exception {


// TODO Auto-generated method stub


}


/**


  • 在整个请求结束之后被调用,也就是在 DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)


*/


@Override


public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception ex)


throws Exception {


// TODO Auto-generated method stub


}


public void returnErrorResponse(HttpServletResponse response, IMoocJSONResult result) throws IOException, UnsupportedEncodingException {


OutputStream out=null;


try{


response.setCharacterEncoding("utf-8");


response.setContentType("text/json");


out = response.getOutputStream();


out.write(JsonUtils.objectToJson(result).getBytes("utf-8"));


out.flush();


} finally{


if(out!=null){


out.close();


}


}


}


}


Controller


package com.imooc.controller.interceptor;


import java.util.ArrayList;


import java.util.Date;


import java.util.List;


import org.springframework.stereotype.Controller;


import org.springframework.ui.ModelMap;


import org.springframework.web.bind.annotation.PostMapping;


import org.springframework.web.bind.annotation.RequestMapping;


import com.imooc.pojo.User;


@Controller


@RequestMapping("one")


public class OneController {


@RequestMapping("/index")


public String index(ModelMap map) {


map.addAttribute("name", "imooc22");


return "thymeleaf/index";


}


@RequestMapping("center")


public String center() {


return "thymeleaf/center/center";


}


@RequestMapping("test")


public String test(ModelMap map) {


User user = new User();


user.setAge(18);


user.setName("manager");


user.setPassword("123456");


user.setBirthday(new Date());


map.addAttribute("user", user);


User u1 = new User();


u1.setAge(19);


u1.setName("imooc");


u1.setPassword("123456");


u1.setBirthday(new Date());


User u2 = new User();


u2.setAge(17);


u2.setName("LeeCX");


u2.setPassword("123456");


u2.setBirthday(new Date());


List<User> userList = new ArrayList<>();


userList.add(user);


userList.add(u1);


userList.add(u2);


map.addAttribute("userList", userList);


return "thymeleaf/test";


}


@PostMapping("postform")


public String postform(User user) {


System.out.println(user.getName());


return "redirect:/th/test";


}


}


package com.imooc.controller.interceptor;


import java.util.ArrayList;


import java.util.Date;


import java.util.List;


import org.springframework.stereotype.Controller;


import org.springframework.ui.ModelMap;


import org.springframework.web.bind.annotation.PostMapping;


import org.springframework.web.bind.annotation.RequestMapping;


import com.imooc.pojo.User;


@Controller


@RequestMapping("two")


public class TwoController {


@RequestMapping("/index")


public String index(ModelMap map) {


map.addAttribute("name", "itzixi22");


return "thymeleaf/index";


}


@RequestMapping("center")


public String center() {


return "thymeleaf/center/center";


}


@RequestMapping("test")


public String test(ModelMap map) {


User user = new User();


user.setAge(18);


user.setName("manager");


user.setPassword("123456");


user.setBirthday(new Date());


map.addAttribute("user", user);


User u1 = new User();


u1.setAge(19);


u1.setName("itzixi");


u1.setPassword("123456");


u1.setBirthday(new Date());


User u2 = new User();


u2.setAge(17);


u2.setName("LeeCX");

评论

发布
暂无评论
springboot中如何使用拦截器,Javaweb资料视频