SpringMVC

[复制链接]
发表于 2023-2-16 01:04:15 | 显示全部楼层 |阅读模式
第一章 初识SpringMVC

1.1 SpringMVC概述


  • SpringMVC是Spring子框架
  • SpringMVC是Spring 为【展现层|表示层|表述层|控制层】提供的基于 MVC 设计理念的优秀的 Web 框架,是目前最主流的MVC 框架。
  • SpringMVC是非侵入式:可以使用注解让普通java对象,作为请求处理器【Controller】
  • SpringMVC是用来代替Servlet
    Servlet作用
    1. 1. 处理请求
    2.   - 将数据共享到域中
    3. 2. 做出响应
    4.   - 跳转页面【视图】
    复制代码

1.2 SpringMVC处理请求原理简图


  • 请求
  • DispatcherServlet【前端控制器】

    • 将请求交给Controller|Handler

  • Controller|Handler【请求处理器】

    • 处理请求
    • 返回数据模型

  • ModelAndView

    • Model:数据模型
    • View:视图对象或视图名

  • DispatcherServlet渲染视图

    • 将数据共享到域中
    • 跳转页面【视图】

  • 响应

第二章 SpringMVC搭建框架

2.1 搭建SpringMVC框架


  • 创建工程【web工程】
  • 导入jar包
    1. <dependency>
    2.     <groupId>org.springframework</groupId>
    3.     <artifactId>spring-webmvc</artifactId>
    4.     <version>5.3.1</version>
    5. </dependency>
    6. <dependency>
    7.     <groupId>org.thymeleaf</groupId>
    8.     <artifactId>thymeleaf-spring5</artifactId>
    9.     <version>3.0.12.RELEASE</version>
    10. </dependency>
    11. <dependency>
    12.     <groupId>javax.servlet</groupId>
    13.     <artifactId>javax.servlet-api</artifactId>
    14.     <version>4.0.1</version>
    15.     <scope>provided</scope>
    16. </dependency>
    复制代码
  • 编写配置文件

    • web.xml注册DispatcherServlet

      • url配置:/
      • init-param:contextConfigLocation,设置springmvc.xml配置文件路径【管理容器对象】
      • :设置DispatcherServlet优先级【启动服务器时,创建当前Servlet对象】

    • springmvc.xml

      • 开启组件扫描
      • 配置视图解析器【解析视图(设置视图前缀&后缀)】


  • 编写请求处理器【Controller|Handler】

    • 使用@Controller注解标识请求处理器
    • 使用@RequestMapping注解标识处理方法【URL】

  • 准备页面进行,测试
第三章 @RequestMapping详解

@RequestMapping注解作用:为指定的类或方法设置相应URL
3.1 @RequestMapping注解位置


  • 书写在类上面

    • 作用:为当前类设置映射URL
    • 注意:不能单独使用,需要与方法上的@RequestMapping配合使用

  • 书写在方法上面

    • 作用:为当前方法设置映射URL
    • 注意:可以单独使用

3.2 @RequestMapping注解属性


  • value属性

    • 类型:String[]
    • 作用:设置URL信息

  • path属性

    • 类型:String[]
    • 作用:与value属性作用一致

  • method属性

    • 类型:RequestMethod[]
      1. public enum RequestMethod {
      2.    GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
      3. }
      复制代码

    • 作用:为当前URL【类或方法】设置请求方式【POST、DELETE、PUT、GET】
    • 注意:

      • 默认情况:所有请求方式均支持
      • 如请求方式不支持,会报如下错误

        • 405【Request method 'GET' not supported】



  • params

    • 类型:String[]
    • 作用:为当前URL设置请求参数
    • 注意:如设置指定请求参数,但URL中未携带指定参数,会报如下错误

      • 400【Parameter conditions "lastName" not met for actual request parameters:】


  • headers

    • 类型:String[]
    • 作用:为当前URL设置请求头信息
    • 注意:如设置指定请求头,但URL中未携带请求头,会报如下错误

      • 404:请求资源未找到


  • 示例代码
    1. @RequestMapping(value = {"/saveEmp","/insertEmp"},
    2.                 method = RequestMethod.GET,
    3.                 params = "lastName=lisi",
    4.                 headers = "User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36")
    5. public String saveEmp(){
    6.     System.out.println("添加员工信息!!!!");
    7.     return SUCCESS;
    8. }
    复制代码
  1. @RequestMapping(method = RequestMethod.POST)
  2. public @interface PostMapping {}
  3. @RequestMapping(method = RequestMethod.GET)
  4. public @interface GetMapping {}
  5. @RequestMapping(method = RequestMethod.PUT)
  6. public @interface PutMapping {}
  7. @RequestMapping(method = RequestMethod.DELETE)
  8. public @interface DeleteMapping {}
复制代码
3.3 @RequestMapping支持Ant 风格的路径(了解)


  • 常用通配符

    a) ?:匹配一个字符
    b) *:匹配任意字符
    c) **:匹配多层路径
  • 示例代码
    1. @RequestMapping("/testAnt/**")
    2. public String testAnt(){
    3.     System.out.println("==>testAnt!!!");
    4.     return SUCCESS;
    5. }
    复制代码
第四章 @PathVariable 注解

4.1 @PathVariable注解位置

  1. @Target(ElementType.PARAMETER)
复制代码


  • 书写在参数前面
4.2 @PathVariable注解作用


  • 获取URL中占位符参数
  • 占位符语法:{}
  • 示例代码
    1. <a th:target="_blank" href="https://www.cnblogs.com/@{/EmpController/testPathVariable/1001}">测试PathVariable注解</a><br>
    复制代码
    1. /**
    2. * testPathVariable
    3. * @return
    4. */
    5. @RequestMapping("/testPathVariable/{empId}")
    6. public String testPathVariable(@PathVariable("empId") Integer empId){
    7.     System.out.println("empId = " + empId);
    8.     return SUCCESS;
    9. }
    复制代码
4.3 @PathVariable注解属性


  • value属性

    • 类型:String
    • 作用:设置占位符中的参数名

  • name属性

    • 类型:String
    • 作用:与name属性的作用一致

  • required属性

    • 类型:boolean
    • 作用:设置当前参数是否必须入参【默认值:true】

      • true:表示当前参数必须入参,如未入参会报如下错误

        • Missing URI template variable 'empId' for method parameter of type Integer

      • false:表示当前参数不必须入参,如未入参,会装配null值


第五章 REST【RESTful】风格CRUD

5.1 REST的CRUD与传统风格CRUD对比


  • 传统风格CRUD

    • 功能                                                 URL                                                                                                                        请求方式
    • 增                             /insertEmp                                                                                   POST
    • 删                             /deleteEmp?empId=1001                      GET
    • 改                             /updateEmp                                             POST
    • 查                             /selectEmp?empId=1001                       GET

  • REST风格CRUD

    • 功能                                                 URL                                                                                                                        请求方式
    • 增                             /emp                                                                                              POST
    • 删                             /emp/1001                                                DELETE
    • 改                             /emp                                                          PUT
    • 查                             /emp/1001                                                GET

5.2 REST风格CRUD优势


  • 提高网站排名

    • 排名方式

      • 竞价排名
      • 技术排名


  • 便于第三方平台对接
5.3 实现PUT&DELETE提交方式步骤


  • 注册过滤器HiddenHttpMethodFilter
  • 设置表单的提交方式为POST
  • 设置参数:_method=PUT或_method=DELETE
5.4 源码解析HiddenHttpMethodFilter
  1. public static final String DEFAULT_METHOD_PARAM = "_method";
  2. private String methodParam = DEFAULT_METHOD_PARAM;
  3. @Override
  4. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
  5.       throws ServletException, IOException {
  6.    HttpServletRequest requestToUse = request;
  7.    if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
  8.       String paramValue = request.getParameter(this.methodParam);
  9.       if (StringUtils.hasLength(paramValue)) {
  10.          String method = paramValue.toUpperCase(Locale.ENGLISH);
  11.          if (ALLOWED_METHODS.contains(method)) {
  12.             requestToUse = new HttpMethodRequestWrapper(request, method);
  13.          }
  14.       }
  15.    }
  16.    filterChain.doFilter(requestToUse, response);
  17. }
  18. /**
  19.          * Simple {@link HttpServletRequest} wrapper that returns the supplied method for
  20.          * {@link HttpServletRequest#getMethod()}.
  21.          */
  22.         private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {
  23.                 private final String method;
  24.                 public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
  25.                         super(request);
  26.                         this.method = method;
  27.                 }
  28.                 @Override
  29.                 public String getMethod() {
  30.                         return this.method;
  31.                 }
  32.         }
复制代码
第六章 SpringMVC处理请求数据

使用Servlet处理请求数据

  • 请求参数

    • String param = request.getParameter();

  • 请求头

    • request.getHeader();

  • Cookie

    • request.getCookies();

6.1 处理请求参数


  • 默认情况:可以将请求参数名,与入参参数名一致的参数,自动入参【自动类型转换】
  • SpringMVC支持POJO入参

    • 要求:请求参数名与POJO的属性名保持一致
    • 示例代码
      1. <form th:action="@{/saveEmp}" method="POST">
      2.     id:<input type="text" name="id"><br>
      3.     LastName:<input type="text" name="lastName"><br>
      4.     Email:<input type="text" name="email"><br>
      5.     Salary:<input type="text" name="salary"><br>
      6.     <input type="submit" value="添加员工信息">
      7. </form>
      复制代码
      1. /**
      2. * 获取请求参数POJO
      3. * @return
      4. */
      5. @RequestMapping(value = "/saveEmp",method = RequestMethod.POST)
      6. public String saveEmp(Employee employee){
      7.     System.out.println("employee = " + employee);
      8.     return  SUCCESS;
      9. }
      复制代码

  • @RequestParam注解

    • 作用:如请求参数与入参参数名不一致时,可以使用@RequestParam注解设置入参参数名
    • 属性

      • value

        • 类型:String
        • 作用:设置需要入参的参数名

      • name

        • 类型:String
        • 作用:与value属性作用一致

      • required

        • 类型:Boolean
        • 作用:设置当前参数,是否必须入参

          • true【默认值】:表示当前参数必须入参,如未入参会报如下错误

            • 400【Required String parameter 'sName' is not present】

          • false:表示当前参数不必须入参,如未入参,装配null值


      • defaultValue

        • 类型:String
        • 作用:当装配数值为null时,指定当前defaultValue默认值


    • 示例代码
      1. /**
      2. * 获取请求参数
      3. * @return
      4. */
      5. @RequestMapping("/requestParam1")
      6. public String requestParam1(@RequestParam(value = "sName",required = false,
      7.                                         defaultValue = "zhangsan")
      8.                                         String stuName,
      9.                             Integer stuAge){
      10.     System.out.println("stuName = " + stuName);
      11.     System.out.println("stuAge = " + stuAge);
      12.     return SUCCESS;
      13. }
      复制代码

6.2 处理请头


  • 语法:@RequestHeader注解
  • 属性

    • value

      • 类型:String
      • 作用:设置需要获取请求头名称

    • name

      • 类型:String
      • 作用:与value属性作用一致

    • required

      • 类型:boolean
      • 作用:【默认值true】

        • true:设置当前请求头是否为必须入参,如未入参会报如下错误

          • 400【Required String parameter 'sName' is not present】

        • false:表示当前参数不必须入参,如未入参,装配null值


    • defaultValue

      • 类型:String
      • 作用:当装配数值为null时,指定当前defaultValue默认值


  • 示例代码
    1. <a th:target="_blank" href="https://www.cnblogs.com/@{/testGetHeader}">测试获取请求头</a>
    复制代码
    1. /**
    2. * 获取请求头
    3. * @return
    4. */
    5. @RequestMapping(value = "/testGetHeader")
    6. public String testGetHeader(@RequestHeader("Accept-Language")String al,
    7.                             @RequestHeader("Referer") String ref){
    8.     System.out.println("al = " + al);
    9.     System.out.println("ref = " + ref);
    10.     return SUCCESS;
    11. }
    复制代码
6.3 处理Cookie信息


  • 语法:@CookieValue获取Cookie数值
  • 属性

    • value

      • 类型:String
      • 作用:设置需要获取Cookie名称

    • name

      • 类型:String
      • 作用:与value属性作用一致

    • required

      • 类型:boolean
      • 作用:【默认值true】

        • true:设置当前Cookie是否为必须入参,如未入参会报如下错误

          • 400【Required String parameter 'sName' is not present】

        • false:表示当前Cookie不必须入参,如未入参,装配null值


    • defaultValue

      • 类型:String
      • 作用:当装配数值为null时,指定当前defaultValue默认值


  • 示例代码
    1. <a th:target="_blank" href="https://www.cnblogs.com/@{/setCookie}">设置Cookie信息</a><br>
    2. <a th:target="_blank" href="https://www.cnblogs.com/@{/getCookie}">获取Cookie信息</a><br>
    复制代码
    1. /**
    2.      * 设置Cookie
    3.      * @return
    4.      */
    5.     @RequestMapping("/setCookie")
    6.     public String setCookie(HttpSession session){
    7. //        Cookie cookie = new Cookie();
    8.         System.out.println("session.getId() = " + session.getId());
    9.         return SUCCESS;
    10.     }
    11.     /**
    12.      * 获取Cookie
    13.      * @return
    14.      */
    15.     @RequestMapping("/getCookie")
    16.     public String getCookie(@CookieValue("JSESSIONID")String cookieValue){
    17.         System.out.println("cookieValue = " + cookieValue);
    18.         return SUCCESS;
    19.     }
    复制代码
6.4 使用原生Servlet-API


  • 将原生Servlet相关对象,入参即可
  1. @RequestMapping("/useRequestObject")
  2. public String useRequestObject(HttpServletRequest request){}
复制代码
第七章 SpringMVC处理响应数据

7.1 使用ModelAndView


  • 使用ModelAndView对象作为方法返回值类型,处理响应数据
  • ModelAndView是模型数据视图对象的集成对象,源码如下
    1. public class ModelAndView {
    2.    /** View instance or view name String. */
    3.    //view代表view对象或viewName【建议使用viewName】
    4.    @Nullable
    5.    private Object view;
    6.    /** Model Map. */
    7.    //ModelMap集成LinkedHashMap,存储数据
    8.    @Nullable
    9.    private ModelMap model;
    10.    
    11.     /**
    12.             设置视图名称
    13.          */
    14.         public void setViewName(@Nullable String viewName) {
    15.                 this.view = viewName;
    16.         }
    17.         /**
    18.          * 获取视图名称
    19.          */
    20.         @Nullable
    21.         public String getViewName() {
    22.                 return (this.view instanceof String ? (String) this.view : null);
    23.         }
    24.     /**
    25.          获取数据,返回Map【无序,model可以为null】
    26.          */
    27.         @Nullable
    28.         protected Map<String, Object> getModelInternal() {
    29.                 return this.model;
    30.         }
    31.         /**
    32.          * 获取数据,返回 ModelMap【有序】
    33.          */
    34.         public ModelMap getModelMap() {
    35.                 if (this.model == null) {
    36.                         this.model = new ModelMap();
    37.                 }
    38.                 return this.model;
    39.         }
    40.         /**
    41.          * 获取数据,返回Map【无序】
    42.          */
    43.         public Map<String, Object> getModel() {
    44.                 return getModelMap();
    45.         }
    46.    
    47.     /**
    48.             设置数据
    49.     */
    50.     public ModelAndView addObject(String attributeName, @Nullable Object attributeValue) {
    51.                 getModelMap().addAttribute(attributeName, attributeValue);
    52.                 return this;
    53.         }
    54.    
    55.    
    56. }
    57.      
    复制代码
  • 示例代码
    1. @GetMapping("/testMvResponsedata")
    2. public ModelAndView testMvResponsedata(){
    3.     ModelAndView mv = new ModelAndView();
    4.     //设置逻辑视图名
    5.     mv.setViewName("response_success");
    6.     //设置数据【将数据共享到域中(request\session\servletContext)】
    7.     mv.addObject("stuName","zhouxu");
    8.     return mv;
    9. }
    复制代码
7.2 使用Model、ModelMap、Map


  • 使用Model、ModelMap、Map作为方法入参,处理响应数据
  • 示例代码
    1. /**
    2.      * 使用Map、Model、ModelMap处理响应数据
    3.      * @return
    4.      */
    5.     @GetMapping("/testMapResponsedata")
    6.     public String testMapResponsedata(Map<String,Object> map
    7.                                          /* Model model
    8.                                         ModelMap modelMap*/){
    9.         map.put("stuName","zhangsan");
    10. //        model.addAttribute("stuName","lisi");
    11. //        modelMap.addAttribute("stuName","wangwu");
    12.         return "response_success";
    13.     }
    复制代码
7.3 SpringMVC中域对象


  • SpringMVC封装数据,默认使用request域对象
  • session域的使用

    • 方式一
      1. /**
      2. * 测试响应数据【其他域对象】
      3. * @return
      4. */
      5. @GetMapping("/testScopeResponsedata")
      6. public String testScopeResponsedata(HttpSession session){
      7.     session.setAttribute("stuName","xinlai");
      8.     return "response_success";
      9. }
      复制代码
    • 方式二
      1. @Controller
      2. @SessionAttributes(value = "stuName") //将request域中数据,同步到session域中
      3. public class TestResponseData {
      4.     /**
      5.      * 使用ModelAndView处理响应数据
      6.      * @return
      7.      */
      8.     @GetMapping("/testMvResponsedata")
      9.     public ModelAndView testMvResponsedata(){
      10.         ModelAndView mv = new ModelAndView();
      11.         //设置逻辑视图名
      12.         mv.setViewName("response_success");
      13.         //设置数据【将数据共享到域中(request\session\servletContext)】
      14.         mv.addObject("stuName","zhouxu");
      15.         return mv;
      16.     }
      17. }
      复制代码

第八章 SpringMVC处理请求响应乱码

8.1 源码解析CharacterEncodingFilter
  1. public class CharacterEncodingFilter extends OncePerRequestFilter {
  2.    //需要设置字符集
  3.    @Nullable
  4.    private String encoding;
  5.    //true:处理请乱码
  6.    private boolean forceRequestEncoding = false;
  7.    //true:处理响应乱码
  8.    private boolean forceResponseEncoding = false;
  9.    
  10.     public String getEncoding() {
  11.                 return this.encoding;
  12.         }
  13.    
  14.     public boolean isForceRequestEncoding() {
  15.                 return this.forceRequestEncoding;
  16.         }
  17.    
  18.     public void setForceResponseEncoding(boolean forceResponseEncoding) {
  19.                 this.forceResponseEncoding = forceResponseEncoding;
  20.         }
  21.    
  22.     public void setForceEncoding(boolean forceEncoding) {
  23.                 this.forceRequestEncoding = forceEncoding;
  24.                 this.forceResponseEncoding = forceEncoding;
  25.         }
  26.    
  27.         @Override
  28.         protected void doFilterInternal(
  29.                         HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
  30.                         throws ServletException, IOException {
  31.                 String encoding = getEncoding();
  32.                 if (encoding != null) {
  33.                         if (isForceRequestEncoding() || request.getCharacterEncoding() == null) {
  34.                                 request.setCharacterEncoding(encoding);
  35.                         }
  36.                         if (isForceResponseEncoding()) {
  37.                                 response.setCharacterEncoding(encoding);
  38.                         }
  39.                 }
  40.         
  41.                 filterChain.doFilter(request, response);
  42.        
  43.     }
  44.    
  45.    
  46. }
复制代码
8.2 处理请求与响应乱码


  • SpringMVC底层默认处理响应乱码
  • SpringMVC处理请求乱码步骤

    • 注册CharacterEncodingFilter

      • 注册CharacterEncodingFilter必须是第一Filter位置

    • 为CharacterEncodingFilter中属性encoding赋值
    • 为CharacterEncodingFilter中属性forceRequestEncoding赋值

  • 示例代码
    1. <filter>
    2.     <filter-name>CharacterEncodingFilter</filter-name>
    3.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    4.     <init-param>
    5.         <param-name>encoding</param-name>
    6.         <param-value>UTF-8</param-value>
    7.     </init-param>
    8.     <init-param>
    9.         <param-name>forceRequestEncoding</param-name>
    10.         <param-value>true</param-value>
    11.     </init-param>
    12. </filter>
    13. <filter-mapping>
    14.     <filter-name>CharacterEncodingFilter</filter-name>
    15.     <url-pattern>/*</url-pattern>
    16. </filter-mapping>
    复制代码
第九章 源码解析SpringMVC工作原理

9.1 Controller中方法的返回值问题


  • 无论方法返回是ModelAndView还是String,最终SpringMVC底层,均会封装为ModelAndView对象
    1. //DispatcherServlet的1061行代码
    2. ModelAndView mv = null;
    3. mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
    复制代码
  • SpringMVC解析mv【ModelAndView】
    1. //DispatcherServlet的1078行代码
    2. processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
    复制代码
  • ThymeleafView对象中344行代码【SpringMVC底层处理响应乱码】
    1. //computedContentType="text/html;charset=UTF-8"
    2. response.setContentType(computedContentType);
    复制代码
  • WebEngineContext对象中783行代码【SpringMVC底层将数据默认共享到request域】
    1. this.request.setAttribute(name, value);
    复制代码
9.2 视图及视图解析器源码


  • 视图解析器将View从ModelAndView中解析出来

    • ThymeleafViewResolver的837行代码
      1. //底层使用反射的方式,newInstance()创建视图对象
      2. final AbstractThymeleafView viewInstance = BeanUtils.instantiateClass(getViewClass());
      复制代码

第十章 SpringMVC视图及视图解析器

10.1 视图解析器对象【ViewResolver】


  • 概述:ViewResolver接口的实现类或子接口,称之为视图解析器
  • 作用:将ModelAndView中的View对象解析出来

10.2 视图对象【View】


  • 概述:View接口的实现类或子接口,称之为视图对象
  • 作用:视图渲染

    • 将数据共享域中
    • 跳转路径【转发或重定向】

第十一章 视图控制器&重定向&加载静态资源

11.1 视图控制器


  • 语法:view-controller
  • 步骤

    • 添加标签:为指定URL映射html页面
    • 添加

      • 有20+种功能
      • 配置了标签之后会导致其他请求路径都失效,添加解决


11.2 重定向


  • 语法:return "redirect:/xxx.html";
11.3 加载静态资源


  • DefaultServlet加载静态资源到服务器

    • 静态资源:html、css、js等资源
    • tomcat->conf->web.xml关键代码如下:
    1. <servlet>
    2.         <servlet-name>default</servlet-name>
    3.         <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    4.         <init-param>
    5.             <param-name>debug</param-name>
    6.             <param-value>0</param-value>
    7.         </init-param>
    8.         <init-param>
    9.             <param-name>listings</param-name>
    10.             <param-value>false</param-value>
    11.         </init-param>
    12.         <load-on-startup>1</load-on-startup>
    13.     </servlet>
    14. <servlet-mapping>
    15.         <servlet-name>default</servlet-name>
    16.         <url-pattern>/</url-pattern>
    17.     </servlet-mapping>
    复制代码
  • 发现问题

    • DispatcherServlet与DefaultServlet的URL配置均为:/,导致DispatcherServlet中的配置将DefaultServlet配置的/覆盖了【DefaultServlet失效,无法加载静态资源

  • 解决方案
    1. [/code]
    2. [/list][size=3]11.4 源码解析重定向原理[/size]
    3. [list]
    4. [*]创建RedirectView对象【ThymeleafViewResolver的775行代码】
    5. [code]// Process redirects (HTTP redirects)
    6. if (viewName.startsWith(REDIRECT_URL_PREFIX)) {
    7.     vrlogger.trace("[THYMELEAF] View "{}" is a redirect, and will not be handled directly by ThymeleafViewResolver.", viewName);
    8.     final String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length(), viewName.length());
    9.     final RedirectView view = new RedirectView(redirectUrl, isRedirectContextRelative(), isRedirectHttp10Compatible());
    10.     return (View) getApplicationContext().getAutowireCapableBeanFactory().initializeBean(view, REDIRECT_URL_PREFIX);
    11. }
    复制代码
  • RedirectView视图渲染

    • RedirectView对象URL处理【330行代码】



  • 执行重定向【RedirectView的627行代码】

第十二章 REST风格CRUD练习

12.1 搭建环境


  • 导入相关jar包
    1. <dependency>
    2.     <groupId>org.springframework</groupId>
    3.     <artifactId>spring-webmvc</artifactId>
    4.     <version>5.3.1</version>
    5. </dependency>
    6. <dependency>
    7.     <groupId>org.thymeleaf</groupId>
    8.     <artifactId>thymeleaf-spring5</artifactId>
    9.     <version>3.0.12.RELEASE</version>
    10. </dependency>
    11. <dependency>
    12.     <groupId>javax.servlet</groupId>
    13.     <artifactId>javax.servlet-api</artifactId>
    14.     <version>4.0.1</version>
    15.     <scope>provided</scope>
    16. </dependency>
    复制代码
  • 编写配置文件

    • web.xml

      • CharacterEncodingFilter
      • HiddenHttpMethodFilter
      • DispatcherServlet

    • springmvc.xml

      • 开启组件扫描
      • 装配视图解析器
      • 装配视图控制器
      • 解决静态资源加载问题
      • 装配annotation-driver


  • dao&pojo
12.2 实现功能思路


  • 实现添加功能思路

    • 跳转添加页面【查询所有部门信息】
    • 实现添加功能

  • 实现删除功能思路

    • 方式一:直接使用表单实现DELETE提交方式
    • 方式二:使用超链接【a】实现DELETE提交方式

      • 使用Vue实现单击超链接,后提交对应表单
      • 取消超链接默认行为
      • 示例代码
        1.     <a target="_blank" href="https://www.cnblogs.com/#" @click="deleteEmp">删除</a>
        2.     <form id="delForm" th:action="@{/emps/}+${emp.id}" method="post">
        3.         <input type="hidden" name="_method" value="DELETE">
        4.     </form>
        复制代码



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表