import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.imooc.pojo.IMoocJSONResult;
import com.imooc.utils.JsonUtils;
public class OneInterceptor implements HandlerInterceptor {
/**
- 在请求处理之前进行调用(Controller方法调用之前)
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object object) throws Exception {
System.out.println(“被one拦截,放行…”);
return true;
/*if (true) {
returnErrorResponse(response, IMoocJSONResult.errorMsg(“被one拦截…”));
}
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();
}
}
}
}
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 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;
总结
面试前的“练手”还是很重要的,所以开始面试之前一定要准备好啊,不然也是耽搁面试官和自己的时间。
我自己是刷了不少面试题的,所以在面试过程中才能够做到心中有数,基本上会清楚面试过程中会问到哪些知识点,高频题又有哪些,所以刷题是面试前期准备过程中非常重要的一点。
面试题及解析总结
大厂面试场景
知识点总结
List;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
总结
面试前的“练手”还是很重要的,所以开始面试之前一定要准备好啊,不然也是耽搁面试官和自己的时间。
我自己是刷了不少面试题的,所以在面试过程中才能够做到心中有数,基本上会清楚面试过程中会问到哪些知识点,高频题又有哪些,所以刷题是面试前期准备过程中非常重要的一点。
面试题及解析总结
[外链图片转存中…(img-pvA2JMH4-1718719601859)]
大厂面试场景
[外链图片转存中…(img-2otNT03F-1718719601860)]
知识点总结
[外链图片转存中…(img-yRDIxVuW-1718719601862)]