⭐表示重要。

第一章:SpringMVC 配置文件的默认位置

1.1 配置要求

  • 配置文件存放目录:/WEB-INF 目录。
  • 文件名格式:[servlet-name]-servlet.xml。
    • servlet-name 部分是在 web.xml 中配置 DispatcherServlet 时,servlet-name 标签的值。
  • 省略原来的 init-param。

1.2 为什么不建议

  • 除 web.xml 是 Tomcat 要求放在 WEB-INF 下,其他配置文件习惯上是放在类路径下。

第二章:请求映射的其它方式

2.1 根据请求参数情况映射

  • 使用 @RequestMapping 注解的 params 参数实现,表达式语法参见下面的例子: | 需求 | 映射方式 | | —- | —- | | 请求参数中必须包含userName | @RequestMapping(value= “/xxx”, params=”userName”) | | 请求参数中不能包含userName | @RequestMapping(value = “/xxx”, params=”!userName”) | | 请求参数中必须包含userName,且值必须为Tom2015 | @RequestMapping(value = “/xxx”, params=”userName=Tom2015”) | | 请求参数中必须包含userName ,但值不能为Tom2015 | @RequestMapping(value = “/xxx”, params=”userName=!Tom2015”) | | 请求参数中必须包含userName,且值为Tom2015, 同时必须包含userPwd但值不限 | @RequestMapping(value= “/xxx”, params={“userName=Tom2015”,”userPwd”} ) |

2.2 根据请求消息头内容映射

  • 使用 @RequestMapping 注解的 headers 参数实现,表达式语法参见下面的例子: | 需求 | 映射方式 | | —- | —- | | 根据 Accept-Language:zh-CN,zh;q=0.8 映射 | @RequestMapping( value=”/xxx”, headers= “Accept-Language=zh-CN,en;q=0.8” ) |

2.3 Ant 风格通配符(⭐)

  • 英文问号(?):匹配一个字符
  • 一个星号(*):匹配路径中的一层
  • 两个连续星号(**):匹配路径中的多层

第三章:@ModelAttribute 注解

  • handler 类中,选定一个方法标记 @ModelAttribute 注解。
    • 效果1:在每个 handler 方法前执行。
    • 效果2:可以将某些数据提前存入请求域。
  • 一般用于更新的场景,比如:数据库表中 10 个字段,而页面更新的时候只传递了更新的 6 个字段,但是 SpringMVC 会将页面更新的字段封装到实体类对象中,导致实体类对象中有 4 个字段是 null ,那么如果直接更新,会导致原来的默认值会被设置为 null。可以用 Mybatis 的动态 SQL 解决。

  • 示例:

  1. package com.github.fairy.era.mvc.handler;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.http.HttpHeaders;
  6. import org.springframework.http.HttpStatus;
  7. import org.springframework.http.ResponseEntity;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.util.MultiValueMap;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.ModelAttribute;
  12. import javax.servlet.ServletContext;
  13. import javax.servlet.http.HttpServletResponse;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. @Controller
  18. public class ModelAttrHandler {
  19. @ModelAttribute
  20. public void doSthBefore(Model model) {
  21. model.addAttribute("initAttr", "initValue");
  22. }
  23. @RequestMapping("/test/model/attr/one")
  24. public String testModelAttrOne(Model model) {
  25. Object modelAttribute = model.getAttribute("initAttr");
  26. System.out.println("modelAttribute = " + modelAttribute);
  27. return "target";
  28. }
  29. @RequestMapping("/test/model/attr/two")
  30. public String testModelAttrTwo(Model model) {
  31. Object modelAttribute = model.getAttribute("initAttr");
  32. System.out.println("modelAttribute = " + modelAttribute);
  33. return "target";
  34. }
  35. @RequestMapping("/test/model/attr/three")
  36. public String testModelAttrThree(Model model) {
  37. Object modelAttribute = model.getAttribute("initAttr");
  38. System.out.println("modelAttribute = " + modelAttribute);
  39. return "target";
  40. }
  41. }