【问题标题】:how to map String on Radio如何在 Radio 上映射字符串
【发布时间】:2013-03-12 09:10:27
【问题描述】:

我遇到了下一个问题:

我想在 gxt 网格中创建 CRUD 操作,但我遇到了这样的情况: 我在表单上将sex 字段设置为RadioGroup,并为create 操作编写了一个转换器(代码如下),因此它可以正常工作(下面的实体和转换器代码):

private Long id;
private String name;
private String sex;

formBinding.getBinding(radioGroup).setConverter(new Converter() {
        @Override
        public Object convertFieldValue(Object value) {
            return ((Radio) value).getData("value");
        }
});

这是将 radiogroup 添加到我的表单的代码:

Radio radio = new Radio();
radio.setBoxLabel("M");
radio.setData("value", "M");
Radio radio2 = new Radio();
radio2.setBoxLabel("F");
radio.setData("value", "F");

radioGroup = new RadioGroup();
radioGroup.setSelectionRequired(true);
radioGroup.setFieldLabel("Sex");
radioGroup.add(radio);
radioGroup.add(radio2);
radioGroup.setName("sex");
simplePanel.add(radioGroup, formData);

我需要将值从 String 转换为 radio 并将其设置为 radiogroup。 但是当我尝试编写相反的操作,即编辑时,它抛出了这样一个异常:

11:52:23.381 [ERROR] [hellogxt] Uncaught exception escaped
java.lang.ClassCastException: java.lang.String cannot be cast to com.extjs.gxt.ui.client.widget.form.Radio
    at com.extjs.gxt.ui.client.widget.form.RadioGroup.setValue(RadioGroup.java:1)
    at com.extjs.gxt.ui.client.binding.FieldBinding.updateField(FieldBinding.java:207)
    at com.extjs.gxt.ui.client.binding.FieldBinding.bind(FieldBinding.java:82)
    at com.extjs.gxt.ui.client.binding.Bindings$1.execute(Bindings.java:92)
    at com.google.gwt.user.client.CommandExecutor.doExecuteCommands(CommandExecutor.java:310)
    at com.google.gwt.user.client.CommandExecutor$2.run(CommandExecutor.java:205)
    at com.google.gwt.user.client.Timer.fire(Timer.java:149)
    at sun.reflect.GeneratedMethodAccessor35.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:337)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:218)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:213)
    at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:292)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:546)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363)
    at java.lang.Thread.run(Thread.java:722)

我尝试向我的转换器添加一些功能,但似乎 binder 没有调用我的代码(formbinder 在编辑操作时根本没有进入该方法)。

【问题讨论】:

    标签: java string gwt radio-button gxt


    【解决方案1】:

    在一个代码块中,你有:

    radio.setData("value", "M");
    

    然后,你有:

            return ((Radio) value).getData("value");
    

    由于您使用键 "value" 将字符串(即"M")放入数据中,因此当您再次将其读回时,您将获得相同的字符串。一旦你将它转换为Radio,你就会得到这个异常:

    java.lang.ClassCastException:java.lang.String 无法转换为 com.extjs.gxt.ui.client.widget.form.Radio

    我不确定转换为 Radio 是否有意义,因为您已经拥有一个 Radio 对象 - 如果这是您需要的,为什么不直接返回 value ?但是,如果是这种情况,您根本不需要Converter。相反,您可能需要该字符串,因此不应转换为 Radio

    【讨论】:

      猜你喜欢
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 2022-11-12
      • 2011-12-04
      • 2017-09-17
      相关资源
      最近更新 更多