Spring + jQuery + FTL:如何处理" on"的复选框值。或者为null

时间:2017-08-01 15:05:23

标签: jquery json spring checkbox freemarker

当我序列化我的表单并使用一些代码将序列化查询字符串转换为JSON对象时,我插入的复选框:

<@spring.formCheckbox path="signUpDto.checkbox_terms" />

&#34;检查&#34;和&#34;未经检查&#34; &#34; on&#34;的值和null分别。我搜索了这个行为,我发现它是jQuery表单序列化的标准行为。

在我的DTO中,我的checkbox_terms变量声明如下:

private boolean checkbox_terms;

但是,当我尝试将序列化对象表单数据发布回服务器时,它会返回400错误(错误请求)。可能是因为checkbox_terms上的这种错误数据类型,因为这两个值是&#34; on&#34;和null。

所以我的问题是,我该如何处理这种情况?

2 个答案:

答案 0 :(得分:1)

为什么您认为问题来自复选框? 因为415错误意味着服务器不处理POST请求的Content-Type,在这种情况下是application / json。

我猜你使用的是Spring MVC。所以你可以尝试设置consumes =&#34; application / json&#34;在RequestMapping中。

参考:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#consumes--

答案 1 :(得分:0)

只需将boolean更改为字符串,如下所示:

private String checkbox_terms;

当您需要检查是否已选中时,请执行

[dto].getCheckbox_terms().equalsIgnoreCase("on")

用于选中或

![dto].getCheckbox_terms().equalsIgnoreCase("on")

未选中。