zoukankan      html  css  js  c++  java
  • springboot学习笔记:5.spring mvc(含FreeMarker+layui整合)

    Spring Web MVC框架(通常简称为"Spring MVC")是一个富"模型,视图,控制器"的web框架。 Spring MVC允许你创建特定的@Controller或@RestController beans来处理传入的HTTP请求。 使用@RequestMapping注解可以将控制器中的方法映射到相应的HTTP请求。

    示例:

    @RestController
    @RequestMapping(value="/users")
    public class MyRestController {
      @RequestMapping(value="/{user}", method=RequestMethod.GET)
      public User getUser(@PathVariable Long user) {
        // ...
      }
      @RequestMapping(value="/{user}/customers", method=RequestMethod.GET)
      List<Customer> getUserCustomers(@PathVariable Long user) {
        // ...
      }
      @RequestMapping(value="/{user}", method=RequestMethod.DELETE)
      public User deleteUser(@PathVariable Long user) {
        // ...
      }
    }

     SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍

     Spring MVC自动配置

     Spring Boot为Spring MVC提供适用于多数应用的自动配置功能。在Spring默认基础上,自动配置添加了以下特性:

    1. 引入ContentNegotiatingViewResolver和BeanNameViewResolver beans。
    2. 对静态资源的支持,包括对WebJars的支持。
    3. 自动注册Converter,GenericConverter,Formatter beans。
    4. 对HttpMessageConverters的支持。
    5. 自动注册MessageCodeResolver。
    6. 对静态index.html的支持。
    7. 对自定义Favicon的支持。

    如果想全面控制Spring MVC,你可以添加自己的@Configuration,并使用@EnableWebMvc对其注解。如果想保留SpringBoot MVC的特性,并只是添加其他的MVC配置(拦截器,formatters,视图控制器等),你可以添加自己的WebMvcConfigurerAdapter类型的@Bean(不使用@EnableWebMvc注解)。

     HttpMessageConverters

     Spring MVC使用HttpMessageConverter接口转换HTTP请求和响应。对象可以自动转换为JSON(使用Jackson库)或XML(如果Jackson XML扩展可用则使用它,否则使用JAXB)。字符串默认使用UTF-8编码。

    如果需要添加或自定义转换器,你可以使用Spring Boot的HttpMessageConverters类:

    import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
    import org.springframework.context.annotation.*;
    import org.springframework.http.converter.*;
    @Configuration
    public class MyConfiguration {
      @Bean
      public HttpMessageConverters customConverters() {
        HttpMessageConverter<?> additional = ...
        HttpMessageConverter<?> another = ...
      return new HttpMessageConverters(additional, another);
      }
    }

    任何在上下文中出现的HttpMessageConverter bean将会添加到converters列表,你可以通过这种方式覆盖默认的转换器(converters)。

     MessageCodesResolver

     Spring MVC有一个策略,用于从绑定的errors产生用来渲染错误信息的错误码:MessageCodesResolver。如果设置 spring.mvc.message-codes-resolver.format 属性为 PREFIX_ERROR_CODE 或 POSTFIX_ERROR_CODE (具体查看 DefaultMessageCodesResolver.Format 枚举值),

    Spring Boot会为你创建一个MessageCodesResolver。

    静态内容

    默认情况下,Spring Boot从classpath下一个叫/static(/public,/resources或/META-INF/resources)的文件夹或从ServletContext根目录提供静态内容。这使用了Spring MVC的ResourceHttpRequestHandler,所以你可以通过添加自己的WebMvcConfigurerAdapter并覆写addResourceHandlers方法来改变这个行为(加载静态文件)。
    在一个单独的web应用中,容器默认的servlet是开启的,如果Spring决定不处理某些请求,默认的servlet作为一个回退(降级)将从ServletContext根目录加载内容。大多数时候,这不会发生(除非你修改默认的MVC配置),因为Spring总能够通过DispatcherServlet处理请求。
    此外,上述标准的静态资源位置有个例外情况是Webjars内容。任何在/webjars/**路径下的资源都将从jar文件中提供,只要它们以Webjars的格式打包。
    注:如果你的应用将被打包成jar,那就不要使用src/main/webapp文件夹。尽管该文件夹是一个共同的标准,但它仅在打包成war的情况下起作用,并且如果产生一个jar,多数构建工具都会静悄悄的忽略它。

    模板引擎

    正如REST web服务,你也可以使用Spring MVC提供动态HTML内容。Spring MVC支持各种各样的模板技术,包括Velocity,
    FreeMarker和JSPs。很多其他的模板引擎也提供它们自己的Spring MVC集成。
    Spring Boot为以下的模板引擎提供自动配置支持:
    1. FreeMarker
    2. Groovy
    3. Thymeleaf
    4. Velocity
    注:如果可能的话,应该忽略JSPs,因为在内嵌的servlet容器使用它们时存在一些已知的限制。
    当你使用这些引擎的任何一种,并采用默认的配置,你的模板将会从src/main/resources/templates目录下自动加载。
    注:IntelliJ IDEA根据你运行应用的方式会对classpath进行不同的整理。在IDE里通过main方法运行你的应用跟从Maven或Gradle或打包好的jar中运行相比会导致不同的顺序。这可能导致Spring Boot不能从classpath下成功地找到模板。如果遇到这个问题,你可以在IDE里重新对classpath进行排序,将模块的类和资源放到第一位。或者,你可以配置模块的前缀为classpath*:/templates/,这样会查找classpath下的所有模板目录。

    Freemarker整合

    步骤:

    1.pom.xml中添加spring-boot-starter-freemarker

    <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>

    里面包含了spring-boot-starter-web

    2.在配置文件中配置freemarker相关配置

    ①想从ftl中获取request对象,可以配置spring.freemarker.request-context-attribute=request

    ②设置模板默认路径;

    ③设置静态文件访问路径

    详细如下:

    ########################################################
    ###FREEMARKER (FreeMarkerAutoConfiguration)
    ########################################################
    spring.freemarker.allow-request-override=false
    #本机调试时,配置项template_update_delay=0,这样就关闭了模板缓存。注意线上环境要开启缓存
    spring.freemarker.cache=false
    spring.freemarker.settings.template_update_delay=0
    spring.freemarker.check-template-location=true
    spring.freemarker.charset=UTF-8
    spring.freemarker.content-type=text/html
    spring.freemarker.expose-request-attributes=false
    spring.freemarker.expose-session-attributes=false
    spring.freemarker.expose-spring-macro-helpers=false
    spring.freemarker.prefix=
    #若在freemarker获取request对象,在spring boot 在application.properties可以这么配置
    spring.freemarker.request-context-attribute=request
    #spring.freemarker.settings.*=
    spring.freemarker.suffix=.ftl
    #template-loader-path表示所有的模板文件都放在该目录下
    spring.freemarker.template-loader-path=classpath:/templates/ 
    #spring.freemarker.view-names= #whitelistofviewnamesthatcanberesolved
    
    #static-locations可以自定义静态资源路径,不过会覆盖springboot默认路径
    #在这个最末尾的file:${web.upload-path}之所有要加file:是因为指定的是一个具体的硬盘路径,其他的使用classpath指的是系统环境变量
    #spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}
    spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
    
    spring.freemarker.settings.auto_import=common/common.ftl as com
    spring.freemarker.settings.datetime_format=yyyy-MM-dd
    #兼容传统模式
    spring.freemarker.settings.classic_compatible=true
    #表示访问该路径时代表请求静态资源,用户可以直接访问该请求路径中的静态资源
    spring.mvc.static-path-pattern=/static/**
    server.port=8080
    server.context-path=/test
    server.session.timeout=10000

    3.编写拦截器,用来向request中添加host和port;用来作为ftl中的basePath;使用${basePath!}得到

    package com.zjt.chapter05.interceptor;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class CommonInterceptor implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
            String path = httpServletRequest.getContextPath();
            String scheme = httpServletRequest.getScheme();
            String serverName = httpServletRequest.getServerName();
            int port = httpServletRequest.getServerPort();
            String basePath = scheme + "://" + serverName + ":" + port + path;
            httpServletRequest.setAttribute("basePath", basePath);
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    
        }
    
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    
        }
    }
    package com.zjt.chapter05.interceptor;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @Configuration
    public class CommonInterceptorConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new CommonInterceptor()).addPathPatterns("/**");
        }
    }

    4.编写测试用的controller:

    package com.zjt.chapter05.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class TestController {
    
    
        @RequestMapping("/hello")
        public ModelAndView Hello(){
            ModelAndView modelAndView=new ModelAndView();
            modelAndView.setViewName("test");
            modelAndView.addObject("name","赵佳涛");
            modelAndView.addObject("message","祝你新春快乐");
            return modelAndView;
        }
    }

    5.在freemarker的ftl模板中显示,大家可以自由使用宏;

    <#macro head  title="">
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
        <title>${title}</title>
        <#nested />
    </head>
    </#macro>
    
    <#macro body>
    <body>
    <#--事实上IE8和IE9并不支持媒体查询(Media Queries),但你可以使用下面的补丁完美兼容!该补丁来自于开源社区:-->
    <!-- 让IE8/9支持媒体查询,从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <#--将上述代码放入你页面 <body> 标签内的任意位置-->
    <#nested />
    </body>
    </html>
    </#macro>
    <@com.head title="">
        <base id="base" href="${basePath!}">
        <link href="${basePath!}/static/plugins/layui/css/layui.css" type="text/css" media="screen" rel="stylesheet"/>
        <script src="${basePath!}/static/plugins/layui/layui.js" type="text/javascript"></script>
    
    </@com.head>
    <@com.body>
    
    <img src="${basePath!}/static/images/logo.png">
    <br/>
    ${name!}${message!}
    </@com.body>

    6.运行项目,访问controller,返回模板页面:

    查看网页源代码,

    可以发现静态资源全部使用ip+端口+项目名的方式来显示静态资源,这样会减少很多麻烦,填平很多坑,这也是前文拦截器中封装的

    String basePath = scheme + "://" + serverName + ":" + port + path;

    为我们带来的便利;

    freemarker整合源代码:https://github.com/zhaojiatao/springboot-zjt-chapter05.git

  • 相关阅读:
    文言文
    【我回来了】TO DO LIST
    AFO
    [DP]
    [CF1221F]Choose a Square
    关于博主(2)
    自我介绍( 并不?
    老年选手的复健之路
    CSP/NOIP 2019 游记
    CSP前模板复习
  • 原文地址:https://www.cnblogs.com/zhaojiatao/p/8325826.html
Copyright © 2011-2022 走看看