【问题标题】:JSF & Webflow - <h:selectManyListbox> converting troublesJSF & Webflow - <h:selectManyListbox> 转换麻烦
【发布时间】:2011-04-29 12:29:45
【问题描述】:

在 JSF 中,我有这个:

<h:selectManyListbox id="createAccountBasicInfo_select_Types"
 styleClass="selectManyCheckbox" value="#{party.roles}" size="6"
 converter="persistenceObjectToStringTwoWayConverter">
      <f:selectItems value="#{accTypes.selectItems}" />
</h:selectManyListbox>

我的转换器:

 //[...]
 import javax.faces.convert.Converter;
 //[...]

public class PersistenceObjectToStringJSFConverter implements Converter {
    //[...]

public Object getAsObject(FacesContext context, UIComponent component, String value) {
    Long id = Long.valueOf(value);
    Object object = null;
    try {
        object = getPersistenceService(context).loadByEntityId(id); // here I load the appropriate record
    } catch (CoreException e) {
        e.printStackTrace();
    } catch (ElementCreationException e) {
        e.printStackTrace();
    }
    return object; //here I need to return an ArrayList of the loaded Objects instead of a single object
}
 }

在 HTML 中,我得到了这个:

<select id="form_party:createAccountBasicInfo_select_Types"
 name="form_party:createAccountBasicInfo_select_Types" class="selectManyCheckbox" 
 multiple="multiple" size="6"> 
  <option value="171128">Andere</option>
  <option value="171133">Interessent</option>
  <option value="171130">Kunde</option>
  <option value="171131">Lieferant</option>
  <option value="171134">Mitarbeiter</option>
  <option value="171132">Mitbewerber</option>
  <option value="171129">Partner</option>
</select>

每个选项的值都是一个 Id,我必须从数据库中加载它。 然后将所选条目的 ArrayList 提供给 WebFlow,然后保存到数据库中。

当我按下“保存”按钮时,所选项目通过转换器运行,我需要从数据库加载项目(按值,例如“171128”)并将其添加到 ArrayList,这将是插入“party.roles”(检查JSF代码)。

我的问题: 我收到以下 JSF 异常:

/WEB-INF/page/core/fragments/account/accountBasicInfo.xhtml @152,58 value="#{party.roles}": Property 'roles' not writable on type java.util.List

我认为我的转换器有问题。我需要改变什么?

感谢您的回答!

(我使用的是 JSF 1.2)

【问题讨论】:

    标签: java jsf spring-webflow


    【解决方案1】:

    例外是#{party} 实际上是一个java.util.List,而它又确实没有setRoles() 方法,所以#{party.roles} 是行不通的。

    #{party} 应该是一个托管 bean,它应该有一个带有 getter 的 private List&lt;Role&gt; roles 属性。转换器不应在getAsObject() 上返回List&lt;Role&gt;,但应返回Role

    【讨论】:

    • Party 是一个单独的对象,由 JAXB 生成。每个属性都有 getter 和 setter,因此“角色”也有一个。 “角色”属性是 List。所以我认为这不是解决方案。
    • 在阅读了您的 getAsObject() 方法的返回行中的评论后,我稍微更新了答案。根据给定的唯一 ID 返回 List&lt;Role&gt; 而不是 Role 肯定是错误的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多