【发布时间】:2017-06-01 11:42:17
【问题描述】:
有一个带有隐藏值字段的表单:
<input type = "hidden" th:field="*{key}" value="keyapp" />
<input type = "hidden" th:field="*{secret}" value="supersecret" />
问题是这些字段在控制器中传递为空。有没有办法通过 HTML 值中预定义的 thymeleaf 字段传递?
感谢和亲切的问候, 丹尼斯
更新:
th:object定义:
<form th:action="@{..//do-login}" method="POST" modelAttribute="authEntity" th:object="${authEntity}">
AuthEntity 类
public class AuthEntity {
private String key;
private String secret;
private String scope;
private String grantType;
private String username;
private String password;
// getters & setters omitted //
}
控制器类
@RequestMapping(value = "/do-login", method = RequestMethod.POST, produces = "application/json")
public void doLogin(@ModelAttribute("authEntity") final AuthEntity authEntity,
final Model model, HttpServletResponse servletResponse,
HttpSession httpSession) throws IOException {
log.info("Application Key: {}, Secret: {}", authEntity.getKey(), authEntity.getSecret());
}
【问题讨论】:
-
你是如何定义表格的?你定义了什么
th:object?这个对象有一个key和一个secret属性吗?如果您已将这些值放入 ModelMap 中,则应使用${key}和${secret}引用它们 -
@ThomasPawlitzki 见上面更新的帖子。另请注意,仅使用
th:field即可正确传递所有其他参数 -
你设置了
key和secret这两个字段吗?您如何尝试检索 MVC 处理程序中的值? -
@ThomasPawlitzki 再次更新了帖子。请看。
-
发送到浏览器的html中是否有值?在那里你应该看到带有值属性的隐藏字段。
标签: java spring-mvc thymeleaf