InplaceSelect 适用于对象而不是字符串

发布于 2024-12-12 04:44:39 字数 622 浏览 0 评论 0 原文

表单

 <h:inputText value="#{BackingBean.firstString}" />
 <h:inputText value="#{BackingBean.secondString}" />
 <rich:inplaceSelect value="#{BackingBean.myText}" defaultLabel="select">
  <f:selectItems value="#{???}" />
 </rich:inplaceSelect>

BackingBean

private List<MyText> myText;
private String firstString;
private String secondString;

MyText

private String id
private String desc
private String text

我想从列表中选择一个 MyText,而在 inplaceSelect 中我想仅显示 MyText 的一个字段。实现它的最佳方法是什么?

Form

 <h:inputText value="#{BackingBean.firstString}" />
 <h:inputText value="#{BackingBean.secondString}" />
 <rich:inplaceSelect value="#{BackingBean.myText}" defaultLabel="select">
  <f:selectItems value="#{???}" />
 </rich:inplaceSelect>

BackingBean

private List<MyText> myText;
private String firstString;
private String secondString;

MyText

private String id
private String desc
private String text

I want to select one MyText from list while in inplaceSelect I want to show only one field of MyText. What is best way to acheive it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一束光,穿透我孤独的魂 2024-12-19 04:44:39

首先,您需要一个 Converter MyTextString 之间进行转换。根据您的问题历史记录,我了解到您正在使用 JSF 1.2,因此我将给出 JSF 1.2 有针对性的答案。

Converter 应该基本上如下所示(这只是一个示例,请阅读注释,了解如何实现它):

public class MyTextConverter implements Converter {

    public String getAsString(FacesContext context, UIComponent component, Object value) {
        // Write code here to convert MyText to its unique String representation.
        return String.valueOf(((MyText) value).getId());
    }

    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        // Write code here to convert unique String representation of MyText to MyText object.
        return myTextService.find(Long.valueOf(value));
    }

}

faces- 中按如下方式注册它config.xml

<converter>
    <converter-for-class>com.example.model.MyText</converter-for-class>
    <converter-class>com.example.converter.MyTextConverter</converter-class>
</converter>

方式使用输入:

<rich:inplaceSelect value="#{bean.myText}" defaultLabel="select">
    <f:selectItems value="#{bean.myTextItems}" />
</rich:inplaceSelect>

然后您可以按如下

private MyText myText;
private List<SelectItem> myTextItems;

@PostConstruct
public void init() {
    myTextItems = new ArrayList<SelectItem>();

    for (MyText myText : myTextService.list()) {
        myTextItems.add(new SelectItem(myText, myText.getText()));
    }
}

// ...

First, you need a Converter to convert between MyText and String. Based on your question history I understand that you're using JSF 1.2, so I'll give a JSF 1.2 targeted answer.

The Converter should basically look like this (it's just an example, read the comment how you're supposed to implement it):

public class MyTextConverter implements Converter {

    public String getAsString(FacesContext context, UIComponent component, Object value) {
        // Write code here to convert MyText to its unique String representation.
        return String.valueOf(((MyText) value).getId());
    }

    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        // Write code here to convert unique String representation of MyText to MyText object.
        return myTextService.find(Long.valueOf(value));
    }

}

Register it as follows in faces-config.xml:

<converter>
    <converter-for-class>com.example.model.MyText</converter-for-class>
    <converter-class>com.example.converter.MyTextConverter</converter-class>
</converter>

Then you can use the input as follows:

<rich:inplaceSelect value="#{bean.myText}" defaultLabel="select">
    <f:selectItems value="#{bean.myTextItems}" />
</rich:inplaceSelect>

with

private MyText myText;
private List<SelectItem> myTextItems;

@PostConstruct
public void init() {
    myTextItems = new ArrayList<SelectItem>();

    for (MyText myText : myTextService.list()) {
        myTextItems.add(new SelectItem(myText, myText.getText()));
    }
}

// ...
陌路黄昏 2024-12-19 04:44:39

在您的 backing-bean 中,由于您有 ,因此更改

private List<MyText> myText;

private MyText myText;

并在中添加 SelectItem 列表backing-bean:

private List<SelectItem> selItems;

然后您可以在构造函数中或使用 (@PostConstruct) 的方法中使用 SelectItem 填充该列表。

您可以使用构造函数 SelectItem(Object value, String label)

selItems.add(new SelectItem(myTextObj, myTextObj.text)); //or id or desc instead of text
//...

然后在视图中:

<f:selectItems value="#{backingBean.selItems}" />

In you backing-bean, since you have <rich:inplaceSelect value="#{backingBean.myText}", change

private List<MyText> myText;

to

private MyText myText;

And add a list of SelectItems in the backing-bean:

private List<SelectItem> selItems;

And then you can populate that list with SelectItems either in the constructor or a method with (@PostConstruct).

You can use the constructor SelectItem(Object value, String label).

selItems.add(new SelectItem(myTextObj, myTextObj.text)); //or id or desc instead of text
//...

And then in the view:

<f:selectItems value="#{backingBean.selItems}" />
|煩躁 2024-12-19 04:44:39
<f:selectItems
    value="#{BackingBean.myTextID}"
    var="value"
    itemLabel="#{value.desc}"
    itemValue="#{item.id}"
    />

或者您需要作为项目标签/值的任何属性。需要 JSF 2。

<f:selectItems
    value="#{BackingBean.myTextID}"
    var="value"
    itemLabel="#{value.desc}"
    itemValue="#{item.id}"
    />

or whatever attributes you need to be the item label/value. JSF 2 required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文