我认为你不应该在那里使用@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>
看起来 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