【发布时间】:2018-10-10 23:02:10
【问题描述】:
我正在尝试获取从 html 表单发送到服务器的对象列表作为我列表的参数,然后我将遍历这些条目,然后通过 springboot th:each 返回它们。但它似乎根本不起作用。加载时会出现表单,但是当我在其中输入值时,它会返回一个错误页面,但是 URL 会变成:
http://localhost:8080/@%7B/%7D?%24%7Bcontent%7D=hello
eclipse 中的这个输出说:
Expression "content" is not valid: only variable expressions ${...} or selection expressions *{...} are allowed in Spring field bindings
注意:这里的内容是我表单中的值属性。 我的控制器如下所示:
@Controller
public HelloList() {
this.addUs = new ArrayList <>();
}
@RequestMapping("/")
public String getlist(@RequestParam (required = false) String content, Model model) {
if (content != null && !content.trim().isEmpty()) {
this.addUs.add(content);
}
model.addAttribute("list",addUs);
return "index";
}
index.html 看起来像这样
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Insert title here</title>
</head>
<body>
<div>
<ul>
<li th:each="amHere: ${addUs}">
<span th:text="${amHere}">hello! world</span>
</li>
</ul>
<form action="@{/}" method="GET">
<input type="text" name="content"/>
<input type="submit" value="Submit"/>
</form>
</div>
</body>
</html>
这可能是重复的,但我遇到的大多数解决方案似乎都没有帮助。因此,任何帮助都会受到赞赏。提前谢谢你。
【问题讨论】:
-
更新:在stackoverflow“运行代码sn-p”选项卡中,我可以看到运行html文件后,它显示默认子元素“hello!world”。但在我的电脑上,这没有发生。我只看到表格。所以我不确定发生了什么。
-
你可能想要
method="post"
标签: java arrays maven spring-boot thymeleaf