使用Converter for type Boolean时,Spring复选框JSP标记被破坏 [英] Spring checkbox JSP Tag is broken when using Converter for type Boolean

查看:124
本文介绍了使用Converter for type Boolean时,Spring复选框JSP标记被破坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Spring Roo和Spring MVC建立了一个CRUD Web应用程序。我的问题是:因为我使用转换器来本地化显示布尔值,所以Spring JSP Tag 复选框被破坏,这意味着复选框不会从支持bean中获取实际值。它们总是错误且未经检查。

I've set up a CRUD web application with Spring Roo and Spring MVC. My problem is: since I'm using a converter for localizing the displaying of boolean values the Spring JSP Tag checkbox is broken which means the checkboxes don't take the real value from the backing beans. They are always false and unchecked.

我做了一些研究,可能在 org的 writeTagDetails 方法中发现错误。 springframework.web.servlet.tags.form.CheckboxTag 即可。以下是此方法的有趣部分:

I've done some research and probably found the error in the writeTagDetails method of org.springframework.web.servlet.tags.form.CheckboxTag. Here is the interesting part of this method:

// the concrete type may not be a Boolean - can be String
if (boundValue instanceof String) {
    boundValue = Boolean.valueOf((String) boundValue);
}
Boolean booleanValue = (boundValue != null ? (Boolean) boundValue : Boolean.FALSE);
renderFromBoolean(booleanValue, tagWriter);

因为我使用转换器来显示是/否而不是真/假 boundValue 是一个String,并且 Boolean.valueOf 的调用总是导致false,因为 valueOf 方法不知道使用的Spring Converter并解释是/不是假的。

Because I'm using a converter to display yes/no instead of true/false the boundValue is a String and the call of Boolean.valueOf always results in false because the valueOf method isn't aware of the used Spring Converter and interprets yes/no as false.

如何用Spring解决这个问题?有人有线索吗?我的大脑已经到了一条死胡同。

How can I solve this issue with Spring? Has anybody a clue? My brain has reached a blind alley.

为了完整性:布尔类型的转换器按预期工作(代码见下文)。

Just for completeness: The converter for the Boolean type is working as expected (code see below).

public class BooleanConverter implements Converter<Boolean,String>, Formatter<Boolean> {

@Autowired
private MessageSource messageSource;

@Override
public String print(Boolean object, Locale locale) {
    return (object)
            ? messageSource.getMessage("label_true", null, LocaleContextHolder.getLocale())
            : messageSource.getMessage("label_false", null, LocaleContextHolder.getLocale());
}

@Override
public String convert(Boolean source) {
    return this.print(source, null);
}
}


推荐答案

这似乎有可能克服。那就是你想要一个人类可读的Formatter来向用户显示模型中布尔值的yes / no。但你仍然希望复选框HTML元素工作,看起来那些HTML复选框元素/小部件/ JSP标签需要使用true / false字符串(或布尔Java类型)它似乎不使用转换器来获取任意是/否字符串返回布尔类型。

This seems possible to overcome. That is you want a human readable Formatter to display yes/no to the user for boolean values in the model. But you still want the checkbox HTML elements to work and it appears those HTML checkbox elements/widgets/JSP tags expects true/false string to be used (or a boolean Java type) it doesn't appear to use the converter to get the arbitrary yes/no string back to a boolean type.

对我来说这个问题表现为当模型具有Boolean.TRUE值时,从不勾选复选框的初始状态组。这意味着对记录的任何读取 - 修改 - 更新(不编辑该字段,当用户未更改时,最终会从true转换为false)。这是由于UI中的初始状态与模型不一致(它显示始终未检查,即假状态),即使模型是真状态也是如此。显示的值是HTML编辑记录屏幕中未选中的复选框,即使模型的值为Boolean.TRUE也是如此。这是因为HTML复选框元素不会将yes解释为true,并且默认为false(因为这是默认的布尔值)。

This problem for me manifests itself as the initial state of the checkbox is never ticked when the model has a Boolean.TRUE value set. This means any read-modify-update of the record (without editing that field ends up transitioning from 'true' to 'false' when it was not changed by the user). This is due to the initial state in the UI being inconsistent with the model (it shows always unchecked i.e. false state) even when model is true state. The displayed value is of an unchecked checkbox in the HTML edit record screen, even when the model has Boolean.TRUE for that value. This is because "yes" does not get interpreted as "true" by the HTML checkbox elements and it defaults to false (as that is the default boolean value).

所以定义你的Formatter / Converter(正如你已经做的那样)。但是在你的@Controller中添加:

So define your Formatter/Converter (as you are already doing). But in your @Controller add:

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(Boolean.class, "friesWithThat", new CustomBooleanEditor(false));
}

这似乎使字符串显示值继续为是/否但是使用的值并传递给复选框HTML元素继续为true / false。

This appears to make the string display value continue to be yes/no but the value used and passed to the checkbox HTML elements continues to be true/false.

现在编辑/更新记录时(在CRUD中)复选框的初始状态是一致的使用模型并保存数据(不编辑任何字段)不会转换复选框状态(这是我对您遇到的问题的理解)。

Now when editing/updating the record (in CRUD) the initial state of the checkbox is consistent with the model and saving the data (without editing any fields) does not transition the checkbox state (which is my understanding of the problem you have).

所以从这里我我们可以理解转换器/格式化器用于一般数据显示,而PropertyEditors用于映射模型数据,因此UI小部件需要数据。

So from this I think we can understand that Converters/Formatters are for general display of data and that PropertyEditors are for mapping model data so the data needed by the UI widget.

这篇关于使用Converter for type Boolean时,Spring复选框JSP标记被破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆