【发布时间】:2020-05-16 04:41:50
【问题描述】:
我的项目卡住了,我在我网站的另一个页面上有一个看似相同的设置,但我无法让这个工作。
DemoApplication.java
@GetMapping("/generate")
public String GenerateForm(Model model) {
model.addAttribute("RecipeGenerator", new RecipeGenerator());
return "generate";
}
@PostMapping("/generate")
public String GenerateSubmit(@ModelAttribute RecipeGenerator recipegenerator) throws IOException {
recipegenerator.execute();
System.out.println(recipegenerator.getContent());
return "generate_result";
}
在这里我可以从我的方法 recipegenerator.execute 中获取任何值,但它在我的 html 页面 generate_result 上不可见,我收到此错误:
出现意外错误(类型=内部服务器错误,状态=500)。 评估 SpringEL 表达式的异常:“recipegenerator.content”(模板:“generate_result” - 第 11 行,第 10 列)
我的 HTML,生成结果:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Recipes</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p><b><a th:text="${recipegenerator.content}" ></a></b></p>
</body>
</html>
【问题讨论】:
-
在模型中,属性名称是RecipeGenerator(大写字母R)。但是在 html
<p><b><a th:text="${recipegenerator.content}" ></a></b></p>中,情况就不同了。这可能是个问题 -
实际上是
recipeGenerator。您可以使用@ModelAttribute(name = "whatever")来决定属性的名称。 -
谢谢,当使用recipeGenerator 时它可以工作......但我仍然不明白为什么,它不应该是RecipeGenerator 吗? recipeGenerator 没有在任何地方提到...
标签: java html spring thymeleaf