1. springmvc的返回参数和传入参数的方式
1.1 返回参数
@Controller
public class NoteController {
@RequestMapping("/mav")
public ModelAndView doSome(ModelAndView mav) {
mav.addObject("name", "jojo");
mav.setViewName("hello");
return mav;
}
@RequestMapping("/m")
public Model doSome(Model m) {
m.addAttribute("name", "jojo");
return m;
}
@RequestMapping("/v")
public void doSome() {
return;
}
@RequestMapping("/viewStr")
public String doSome1() {
return "hello";
}
@RequestMapping(value = "/str", produces = "text/plain;charset=UTF-8" )
@ResponseBody
public String doSome2() {
return "你好!";
}
@RequestMapping(value = "/obj")
@ResponseBody
public Object doSome3() {
return null;
}
@RequestMapping("/query")
@ResponseBody
public Person doSomE() {
Person person = new Person();
person.setBirthday(new Date());
return person;
}
1.2 传入参数
@RequestMapping("/add")
@ResponseBody
public Object doSome(HttpServletRequest request) {
String idStr = request.getParameter("id");
Integer id = Integer.parseInt(idStr);
return null;
}
@RequestMapping(value = "/add", method = RequestMethod.GET)
@ResponseBody
public Object doSome4(Integer id, String name) {
return null;
}
@RequestMapping("/add")
@ResponseBody
public Object doSome4(Person person) {
return null;
}
@RequestMapping("/add")
@ResponseBody
public Object doSome5(@RequestParam(value = "myId", required = true) Integer id,
@RequestParam(value = "myName", required = false) String name) {
return null;
}
@GetMapping("/do/{id}/{name}")
@ResponseBody
public Object doSome6(@PathVariable(value = "id", required = true) Integer id,
@PathVariable(value = "name", required = false) String name) {
return null;
}
@RequestMapping("/body")
@ResponseBody
public Object doSome7(@RequestBody Person person) {
return null;
}
@RequestMapping("/")
@ResponseBody
public Object doSome8(String dateStr) throws ParseException {
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
Person person = new Person();
person.setBirthday(date);
return null;
}
@RequestMapping("/")
@ResponseBody
public Object doSome8(Person person) {
return null;
}
}
2. springmvc的全局异常处理
@RestControllerAdvice
public class GlobaleExceptionHandler {
@ExceptionHandler(RuntimeException.class)
public JsonResult doHandleRuntimeException(RuntimeException e){
return JsonResult.error(e);
}
}
3. springmvc的拦截器
3.1 定义拦截器
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
3.2 注册拦截器
@Configuration
public class SpringWebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
List<String> exculdePaths = new ArrayList<>();
exculdePaths.add("/rNotice/{title}/{page}/{num}");
registry.addInterceptor(new PrintTimeInterceptor())
.addPathPatterns("/rNotice/**" )
.excludePathPatterns("/rNotice/{title}/{page}/{num}" );
registry.addInterceptor(new PrintTimeInterceptor2())
.addPathPatterns("/rNotice/**");
}
}
3.3 处理器执行链
public class HandlerExecutionChain {
private final Object handler;
private final List<HandlerInterceptor> interceptorList;
private int interceptorIndex;
.....
}
4. springmvc的组件
1. 前端控制器组件(DispatcherServlet)
2. 处理器组件(Controller)
3. 处理器映射器组件(HandlerMapping)
4. 处理器适配器组件(HandlerAdapter)
5. 拦截器组件(HandlerInterceptor)
6. 视图解析器组件(ViewResolver)
7. 视图组件(View)
8. 数据转换组件(DataBinder)
9. 消息转换器组件(HttpMessageConverter)
5. springmvc运行流程
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e716dd50cbb72a4bcc2c02366fa759f1.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/dff78db777e761027004fc46a11be737.png#pic_center)