springboot+freemarker,spring boot freemarker

  springboot+freemarker,spring boot freemarker

  

目录

跳靴整合为什么要使用踩坑报错问题原因解决方法跳趾自由标记基础配置及使用1.基础配置2.基础使用

 

  

springboot整合freemarker踩坑

 

  

报错

2021-04-23 02:01:18.148错误9484 - [nio-8080-exec-1] o.a.c[.[/].路径为[]的上下文中的servlet [dispatcherServlet]的【调度器servlet】: servlet。服务()引发异常[请求处理失败;嵌套异常为freemarker。核心。invalidreferenceexception :以下内容的计算结果为空或缺少:==hots[在模板" inc/right.ftl "的第6行第11列中]

 

  - Tip:如果已知失败的表达式合法地引用了有时为空或缺失的内容,请指定一个默认值,如myOptionalVar!我的默认值,或者使用#如果myOptionalVar?when-present # else when-missing/# if .(这些只涵盖了表达式的最后一步;要覆盖整个表达式,请使用括号: (myOptionalVar.foo)!myDefault,(myOptionalVar.foo)?-

  FTL堆栈跟踪( ~ 表示嵌套相关): -失败于: @ hots[in template Inc/right。超光速飞行第6行第9列] -通过: #包含/Inc/right。ftl“[在模板中]”索引。超光速飞行第52行第13列] ~通过: #嵌套[在模板 inc/layout.ftl 中中宏布局第25行第5列] ~通过 : @布局首页[在模板" index.ftl "的第注射毒品行第数字一(one)列] - ]带有根本原因

  freemarker。核心。invalidreferenceexception :以下内容的计算结果为空或缺少:==hots[在模板 inc/right.ftl 的第6行第11列中]

  - Tip:如果已知失败的表达式合法地引用了有时为空或缺失的内容,请指定一个默认值,如myOptionalVar!我的默认值,或者使用#如果myOptionalVar?when-present # else when-missing/# if .(这些只涵盖了表达式的最后一步;要覆盖整个表达式,请使用括号: (myOptionalVar.foo)!myDefault,(myOptionalVar.foo)?-

  FTL堆栈跟踪( ~ 表示嵌套相关): -在第6行的: @ hots[in template Inc/right。超光速飞行处失败

  , column 9] - Reached through: #include "/inc/right.ftl" [in template "index.ftl" at line 52, column 13] ~ Reached through: #nested [in template "inc/layout.ftl" in macro "layout" at line 25, column 5] ~ Reached through: @layout "首页" [in template "index.ftl" at line 2, column 1]---- at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) ~[freemarker-2.3.28.jar:2.3.28] at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106) ~[freemarker-2.3.28.jar:2.3.28] at freemarker.core.Environment.visit(Environment.java:330) ~[freemarker-2.3.28.jar:2.3.28] at freemarker.core.Environment.visit(Environment.java:336) ~[freemarker-2.3.28.jar:2.3.28] at freemarker.core.Environment.include(Environment.java:2582) ~[freemarker-2.3.28.jar:2.3.28] at freemarker.core.Include.accept(Include.java:171) ~[freemarker-2.3.28.jar:2.3.28] at freemarker.core.Environment.visit(Environment.java:366) ~[freemarker-2.3.28.jar:2.3.28] at freemarker.core.Environment.invokeNestedContent(Environment.java:618) ~[freemarker-2.3.28.jar:2.3.28] at freemarker.core.BodyInstruction.accept(BodyInstruction.java:60) ~[freemarker-2.3.28.jar:2.3.28] at freemarker.core.Environment.visit(Environment.java:366) ~[freemarker-2.3.28.jar:2.3.28] at freemarker.core.Environment.invoke(Environment.java:775) ~[freemarker-2.3.28.jar:2.3.28] at freemarker.core.UnifiedCall.accept(UnifiedCall.java:83) ~[freemarker-2.3.28.jar:2.3.28] at freemarker.core.Environment.visit(Environment.java:330) ~[freemarker-2.3.28.jar:2.3.28] at freemarker.core.Environment.visit(Environment.java:336) ~[freemarker-2.3.28.jar:2.3.28] at freemarker.core.Environment.process(Environment.java:309) ~[freemarker-2.3.28.jar:2.3.28] at freemarker.template.Template.process(Template.java:384) ~[freemarker-2.3.28.jar:2.3.28] at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:389) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:302) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:253) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:178) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:316) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.14.jar:9.0.14] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na] at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.14.jar:9.0.14] at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]

  2021-04-23 02:01:18.177 ERROR 9484 --- [nio-8080-exec-1] s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [/] and exception [The following has evaluated to null or missing:==> hots [in template "inc/right.ftl" at line 6, column 11]

  ----Tip: If the failing expression is known to legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??----

  ----FTL stack trace ("~" means nesting-related): - Failed at: @hots [in template "inc/right.ftl" at line 6, column 9] - Reached through: #include "/inc/right.ftl" [in template "index.ftl" at line 52, column 13] ~ Reached through: #nested [in template "inc/layout.ftl" in macro "layout" at line 25, column 5] ~ Reached through: @layout "首页" [in template "index.ftl" at line 2, column 1]----] as the response has already been committed. As a result, the response may have the wrong status code.

  

 

  

 

  

问题原因

因为在service里试着把redis逻辑代码运行,然后再控制台上输出,结果发现结果是没有问题的,而且通过报错信息初步认为是前端的问题,hots没找到,但是跟之前的一个类似功能相比较,然后通过不断的对比,找到问题是出在配置类。

 

  之前一直认为template类中的getname方法就能完成映射功能

  

 

  其实需要在FreemarkerConfig类里面进行声明

  

 

  

解决方法

再FreemarkerConfig类先通过@Autowired导入template类,然后使用configuration.setSharedVariable(hots, hotsTemplate);进行声明,缺少这一步会使得前端对于<@hots>这个标签无法识别。

 

  

 

  

springboot freemarker基础配置及使用

 

  

1.基础配置

1.1引入依赖

 

  

<!-- 引入freemarker模板引擎的依赖 --><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-freemarker</artifactId></dependency>

1.2在application.properties配置文件中添加如下配置:

 

  

# 是否开启thymeleaf缓存,本地为false,生产建议为truespring.freemarker.cache=falsespring.freemarker.charset=UTF-8spring.freemarker.allow-request-override=falsespring.freemarker.check-template-location=true#类型spring.freemarker.content-type=text/htmlspring.freemarker.expose-request-attributes=truespring.freemarker.expose-session-attributes=true##文件后缀spring.freemarker.suffix=.ftl#路径 .ftl文件就创建在templates下面或者子目录spring.freemarker.template-loader-path=classpath:/templates/

 

  

2.基础使用

首先我在controller类中编写一个测试接口用来测试使用,编写内容如下

 

  

@Controllerpublic class FreeMarkerController {    Map<String ,Object> map = new HashMap<>();    List<Object> list = new ArrayList();    @GetMapping("/freemaker/{id}/{name}")    public String demo(@PathVariable("id")String id ,@PathVariable("name") String name,Model model){        map.clear();        map.put("id",id);        map.put("name",name);        list.add(2);        list.add(4);        map.put("aa",null);        model.addAttribute("map",map);        model.addAttribute("list",list);        model.addAttribute("date",new Date());        return "index";    }}

以下所有变量都是有上面接口提供

 

  2.1调用map类型数据,实体类也可使用此格式调用属性

  

/*可直接获取id对应的v值*/id:${map.id}name:${map.name}

2.2集合调用,set集合也可用此格式调用相应的元素

 

  

list01:${list[0]}list02:${list[1]}

2.3字符串拼接的两种方法

 

  

${"你好啊${map.name}"}${"你好啊"+map.name}

2.4FreeMarker表达式中支持+、-、*、/、%运算符

 

  

${list[1]+list[0]}${list[1]-list[0]}${list[1]*list[0]}${list[1]/list[0]}${list[1]%list[0]}

2.5表达式中支持的比较运算符有如下几种:

 

  

1. =(或者==):判断两个值是否相等;2. !=:判断两个值是否不相等;注: =和!=可以用作字符串、数值和日期的比较,但两边的数据类型必须相同。而且FreeMarker的比较是精确比较,不会忽略大小写及空格。3. >(或者gt):大于4. >=(或者gte):大于等于5. <(或者lt):小于6. <=(或者lte):小于等于注: 上面这些比较运算符可以用于数字和日期,但不能用于字符串。大部分时候,使用gt比>有更好的效果,因为FreeMarker会把>解释成标签的结束字符。可以使用括号来避免这种

 

  

2.6时间格式转换

 

  

${date?string("yyyy-MM-dd")}

2.7支持逻辑表达式

 

  FreeMarker支持逻辑运算但是只支持布尔型逻辑运算两个值进行比较运算的的时候需要添加括号才能进行逻辑运算,一般用于指令中

  

FreeMarker支持逻辑运算但是只支持布尔型逻辑运算两个值进行比较运算的的时候需要添加括号才能进行逻辑运算,一般用于指令中1. &&: 逻辑与;2. :逻辑或;3. !:逻辑非逻辑运算符只能用于布尔值。

 

  

2.8FreeMarker无法理解null值

 

  

FreeMarker的变量必须赋值,否则就会抛出异常。而对于FreeMarker来说,null值和不存在的变量是完全一样的,因为FreeMarker无法理解null值。FreeMarker提供两个运算符来避免空值:1.!运算1.1若是msg不存在不报异常默认空值,若是不使用!会报异常<br>${msg!}1.2若是存在则输出,若是不存在则默认为abc<br>${map.a!"abc"}<br>2.??返回布尔值一般用在指令中下面会有引用此处不解释<br>

 

  

2.9 if指令

 

  

<#if map.aa?? >    存在    <#else >    不存在</#if><br><#if (list[0]>=3) >    优秀    <#elseif (list[0]>=2)>    良好    <#else >    差劲</#if>

2.10 list指令 遍历

 

  

<#list list as li>    ${li}<br></#list>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持盛行IT。

 

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: