Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Thymeleaf:无法传递隐藏th:字段的预定义值_Java_Spring Mvc_Thymeleaf - Fatal编程技术网

Java Thymeleaf:无法传递隐藏th:字段的预定义值

Java Thymeleaf:无法传递隐藏th:字段的预定义值,java,spring-mvc,thymeleaf,Java,Spring Mvc,Thymeleaf,存在一个具有隐藏值字段的窗体: <input type = "hidden" th:field="*{key}" value="keyapp" /> <input type = "hidden" th:field="*{secret}" value="supersecret" /> AuthEntity类 public class AuthEntity { private String key; private String secret; p

存在一个具有隐藏值字段的窗体:

<input type = "hidden" th:field="*{key}" value="keyapp" />
<input type = "hidden" th:field="*{secret}" value="supersecret" />
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());
}
控制器类

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());
}

我在隐藏输入时遇到了同样的问题,但没有找到解决方案,而是手动设置字段的“id”和“name”属性,并忽略了th:field。

您是如何定义表单的?您定义了什么对象?这个对象有
密钥
秘密
属性吗?如果您已将这些值放入模型映射中,则应使用
${key}
${secret}
@ThomasPawlitzki引用它们。请参见上文更新的帖子。还请注意,仅使用
th:field
即可正确传递所有其他参数。您是否设置了字段
key
secret
?那么如何在MVC处理程序中检索值呢?@ThomasPawlitzki再次更新了帖子。请参阅。html中是否有值发送到浏览器?在那里,您应该可以看到具有值属性的隐藏字段。但是值是否已传递?如果保留“value”属性,它将被传递(至少对我有效:)。IIRC表单中所有有名称的内容都会在提交时传递,但我可能会在这里混淆名称和id。使用spring和thymeleaf?你确定吗?)是的。我的代码看起来是这样的:并且它被正确地提交了。啊,好吧,但是如果值是html静态的呢?