在 spring boot 中从 freemaker 获取“以下已评估为空或缺失”

问题描述 投票:0回答:2
java spring-boot freemarker
2个回答
0
投票

我认为你不应该在那里使用@ModelAttribute。试试这个:

@GetMapping("/signup")
public String getSignUpPage(@RequestParam String name, Model model) {
    

我还将 ModelMap 更改为 org.springframework.ui.Model 作为更通用的方法。


根据 https://freemarker.apache.org/docs/pgui_misc_servlet.html Freemarker 还会自动将请求参数放入

RequestParameters
哈希变量中。所以你甚至不需要自己将参数放入模型中,这个 freemarker 代码就足够了:

<h1>Hello, ${RequestParameters.name}</h1>


0
投票

看起来 freemarker 2.3.32 不能使用 spring boot 3。 例如,以下代码适用于 spring boot 2.6.7、freemarker 2.3.31,但不适用于 spring boot 3.0.4

    <h1>Hello, ${RequestParameters.name}</h1>

你会得到这样的异常:

Hello, FreeMarker template error (DEBUG mode; use RETHROW in production!): The following has evaluated to null or missing: ==> RequestParameters [in template "main.ftl" at line 24, column 18] ---- 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. (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: ${RequestParameters.name} [in template "main.ftl" at line 24, column 16] ---- Java stack trace (for programmers): ----

我认为 Freemarker 仍然使用旧的 java ee 包,如 javax.servlet 而不是 jakarta.servlet

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.