java Richfaces RecommendationBox 将附加值传递给支持 bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/903172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:17:24  来源:igfitidea点击:

richfaces suggestionBox passing additional values to backing bean

javajsfrichfaces

提问by Martlark

When using the RichFaces suggestionBoxhow can you pass more than one id or value from the page with the text input to the suggestionBoxbacking bean. ie: to show a list of suggested cities within a selected state? Here is my autoCompletemethod.

使用 RichFaces 时,suggestionBox如何将带有文本输入的页面中的多个 id 或值传递给suggestionBox支持 bean。即:显示所选州内的建议城市列表?这是我的autoComplete方法。

public List< Suburb > autocomplete(Object suggest)
{
    String pref = (String) suggest;
    ArrayList< Suburb > result = new ArrayList< Suburb >();

    Iterator< Suburb > iterator = getSuburbs().iterator();
    while( iterator.hasNext() )
    {
        Suburb elem = ((Suburb) iterator.next());
        if( (elem.getName() != null && elem.getName().toLowerCase().indexOf( pref.toLowerCase() ) == 0) || "".equals( pref ) )
        {
            result.add( elem );
        }
    }
    return result;
}

As you can see there is one value passed from the page, Objectsuggest, which is the text of the h:inputText(in the faceLets m:textFormRow)

如您所见,从页面中传递了一个值,Object建议,它是h:inputText(在 faceLets 中m:textFormRow)的文本

<m:textFormRow id="suburb" label="#{msgs.suburbPrompt}" 
    property="#{bean[dto].addressDTO.suburb}"
    required="true" maxlength="100" size="30" />

<rich:suggestionbox height="200" width="200" usingSuggestObjects="true"
    suggestionAction="#{suburbsMBean.autocomplete}" var="suburb" for="suburb"
    fetchValue="#{suburb.name}" id="suggestion">
    <h:column>
        <h:outputText value="#{suburb.name}" />
    </h:column>
</rich:suggestionbox>

Earlier in the page you can select a state which I'd like to use to pare down the list of suburbs that the suggestion box displays.

在页面的前面,您可以选择一个州,我想用它来减少建议框显示的郊区列表。

回答by Jonik

(Disclaimer: I'm aware that the question was asked rather long time ago, but maybe this'll help someone with a similar problem...)

(免责声明:我知道这个问题是很久以前提出的,但也许这会帮助有类似问题的人......)

Check out this blog post which deals with something similar: RichFaces - SuggestionBox and hidden field.?

查看这篇博客文章,其中涉及类似的内容:RichFaces - SuggestionBox and hidden field.?

The key is to use <f:setPropertyActionListener value="#{...}" target="#{...}">wrapped inside <a4j:support event="onselect" ajaxSingle="true">. This can be used to set an additional value for a backing bean when onselectis triggered for the SuggestionBox.

关键是用<f:setPropertyActionListener value="#{...}" target="#{...}">包裹在里面<a4j:support event="onselect" ajaxSingle="true">。这可用于在onselect为 SuggestionBox 触发时为支持 bean 设置附加值。

With this approach I managed to create a SuggestionBox that displays (and autocompletes) customers' namesbut upon selection sets a whole customer object(with several properties; identified by an id) for a bean.

通过这种方法,我设法创建了一个 SuggestionBox,它显示(并自动完成)客户的姓名,但在选择时为 bean设置一个完整的客户对象(具有多个属性;由 id 标识)。

回答by Jim Barrows

Does using <f:parametertag inside the <rich:suggestionboxwork?

<f:parameter<rich:suggestionbox工作中使用标签吗?

回答by PMorganCA

You can use the <f:parametertab inside the rich:suggestionbox. My task was filtering a list according to some attribute of the list element, where sometimes that attribute could be ignored. Like, sometimes I want a list of only citrus fruit, and sometimes I want the entire list of available fruit.

您可以使用<f:parameter标签内rich:suggestionbox。我的任务是根据列表元素的某些属性过滤列表,有时可以忽略该属性。就像,有时我只想要柑橘类水果的列表,有时我想要可用水果的完整列表。

In the page:

在页面中:

<rich:suggestionbox usingSuggestObjects="true"
        suggestionAction="#{listBuilder.autocompleteFilterFruit('')}" var="ind"
        for="fruitInput" fetchValue="#{fruit.name}" id="suggestion" >
    <f:param name="constrainInd" value="#{basket.isConstrainedToCitrus}" />

    ...

</rich:suggestionbox>

I had one class (Basket) that knew if the list had to be special-filtered, and another class (ListBuilder) that built the list.

我有一个类 ( Basket) 知道是否必须对列表进行特殊过滤,还有另一个类 ( ListBuilder) 来构建列表。

In Basket:

Basket

public Boolean getIsConstrainedToCitrus ()
{
    return new Boolean ( logic that answers "is this basket for citrus only" );
}

In ListBuilder:

在列表生成器中:

public List<Fruit> autocompleteFilterFruit (Object arg)
{
    List<Fruit> rtnList = new ArrayList<Fruit> ();

    String suggestion = (String) arg;

    // get the filter control that the page retrieved from the Basket
    //
    Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext ().getRequestParameterMap();
    boolean isConstrainedToCitrus = "true".equals (params.get ("constrainInd"));

    // allFruit is a pre-initialized list of all the available fruit. use it to populate the return list according 
    // to the filter rules and matches to the auto-complete suggestions
    for (Fruit item : allFruit)
    {
        if ((!isConstrainedToCitrus || item.isCitrus())  &&  item.name.startsWith(suggestion))
        {
            rtnList.add (item);
        }
    }
    return rtnList;
}

回答by Mark

Have you looked at this RichFaces suggestionBox demoyet ? There are links under the examples to view the source.

您是否看过这个 RichFaces 建议框演示?示例下有链接可以查看源代码。

Edit:

编辑:

Sounds like you need the value of state in your bean before the user types in the suggestionBox. I would use the RichFaces ajax support to pass the value of state to the bean so when the autocomplete method is called is has the state the user selected on the page to populate a list of suburbs.

听起来像您需要在用户输入建议框之前在 bean 中的状态值。我将使用 RichFaces ajax 支持将 state 的值传递给 bean,因此当调用自动完成方法时,用户在页面上选择的状态来填充郊区列表。