lshfong 2020-12-07 10:07 采纳率: 0%
浏览 99

springboot2+Freemarker模板中内置对象Request为空

开发工具IDEA

配置

  freemarker:
    request-context-attribute: request
    #禁止模板缓存
    cache: false
    template-loader-path: classpath:/templates
    suffix: .html
    settings:
      classic_compatible: true

模板文件

访问:http://192.168.0.2:8001/hello/freemarker?id=1
<dl>
    <dt>Request:${Request["method"]} </dt>内置Request对象为空
     <dt>request:${request.id} </dt>配置文件绑定的request也为空
</dl>

请问是怎么会事呢

  • 写回答

1条回答 默认 最新

  • 小日子呀 2023-03-23 11:32
    关注

    可以参考以下步骤:
    在pom.xml中添加freemarker依赖:

    org.springframework.boot spring-boot-starter-freemarker

    2、在application.yml中配置freemarker模板引擎:

    spring:
      freemarker:
        template-loader-path: classpath:/templates/
        suffix: .ftl
        content-type: text/html
        charset: UTF-8
        request-context-attribute: request
    
    

    3、在Controller中添加@ModelAttribute注解:

    @Controller
    public class TestController {
        @ModelAttribute
        public void addAttributes(Model model) {
            model.addAttribute("msg", "页面信息");
        }
        @RequestMapping("/test")
        public String test(Model model) {
            return "test";
        }
    }
    
    

    4、在freemarker模板中使用Request对象:

    <#assign request=RequestContext.getRequest()>
    <#if request??>
        ${request.getParameter("name")}
    </#if>
    
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建