Primefaces selectOneMenu 侦听器未使用字符串以外的对象调用 [英] Primefaces selectOneMenu listener not called with Objects other than Strings

查看:18
本文介绍了Primefaces selectOneMenu 侦听器未使用字符串以外的对象调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Jsf 2.0 和 Primefaces 3.2 实现一个 web 应用程序.我注意到了这种意外的行为:我有一个 selectOneMenu 和一个 commandButton,如下所示

I'm implementing a webapp using Jsf 2.0 and Primefaces 3.2. I've noticed this unexpected behavoiur: I have a selectOneMenu and a commandButton, as below

<p:selectOneMenu id="selsel" value="#{bean.myObj}">
  <f:selectItems value="#{bean.myObjList}" />
</p:selectOneMenu>
<p:commandButton id="btnid" value="Ok" actionListener="#{bean.updateSelectValues()}" />

如果myObj 不是String,则不会调用updateSelectValues 方法.我根本看不到任何异常或错误,只是没有调用它.这是支持 bean:

What happens is that if myObj is not a String, the updateSelectValues method is not called. I can't see any exception or error at all, it's just not called. Here's the backing bean:

private List<MyObj> myObjList;
private MyObj myObj;
// getters and setters

public void updateSelectValues() {
  System.out.println(this.myObj);
}

myObj 的代码:

public class MyObj implements Serializable {

  private static final long serialVersionUID = 1L;

  private String param1;
  private int param2;

  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("MyObj [param1=");
    builder.append(this.param1);
    builder.append(", param2=");
    builder.append(this.param2);
    builder.append("]");
    return builder.toString();
  }

}

推荐答案

那是因为 HTML 和 HTTP 不理解 Java 对象.当 JSF 生成 HTML 时,所有 Java 对象都被转换为 String.当提交的表单数据要由 JSF 处理时,所有 String 的 HTTP 请求参数都应该被转换回 Java 对象.

That's because HTML and HTTP does not understand Java objects. All Java objects are converted to String when HTML is to be produced by JSF. All HTTP request parameters which are String are supposed to be converted back to Java object when the submitted form data is to be processed by JSF.

至于您的具体问题,如果您添加了 或与表单等效的 PrimeFaces(和还在 ajax 提交时更新了它),那么您应该已经注意到空转换器"的转换错误.此外,如果您关注过服务器日志,您应该也会看到有关未处理消息的警告.

As to your concrete problem, if you have added a <h:message>, <h:messages> or the PrimeFaces equivalent to the form (and also updated it on ajax submits), then you should have noticed a conversion error for "null converter". Also, if you have paid attention to the server log, you should also have seen a warning about an unhandled message.

您需要创建一个自定义的转换器MyObj 和它唯一的 String 表示之间转换.例如:

You need to create a custom Converter which converts between MyObj and its unique String representation. For example:

@FacesConverter(forClass=MyObj.class)
public class MyObjConverter implements Converter {

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object valueToRender) {
        // Convert MyObj to its unique String representation.
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
        // Convert String to MyObj.
    }

}

通常这些对象已经通过它们的 ID 存储在某个数据库或映射中.然后,您可以使用该 ID 作为唯一的 String 表示.

Usually those objects are already stored in some database or mapping by their ID. You then use exactly that ID as unique String representation.

这篇关于Primefaces selectOneMenu 侦听器未使用字符串以外的对象调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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