Primefaces selectOneMenu侦听器未使用除字符串以外的其他对象调用

编程入门 行业动态 更新时间:2024-10-11 11:25:13
本文介绍了Primefaces selectOneMenu侦听器未使用除字符串以外的其他对象调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Jsf 2.0和Primefaces 3.2实现一个Webapp. 我注意到了这种意外行为:我有一个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.

对于您的具体问题,如果您添加了与表单等效的<h:message>,<h:messages>或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.

您需要创建自定义 Converter 可以在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侦听器未使用除字符串以外的其他对象调用

本文发布于:2023-11-24 19:38:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626547.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:侦听器   字符串   对象   Primefaces   selectOneMenu

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!