【问题标题】:richfaces suggestionBox passing additional values to backing beanRichfaces 建议框将附加值传递给支持 bean
【发布时间】:2010-10-28 12:56:25
【问题描述】:

当使用 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;
}

如您所见,从页面传递了一个值,ObjectSuggest,这是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>

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

【问题讨论】:

  • 您可能必须更明确地说明您正在尝试做什么 - 例如,发布一个带有您想要使用的控件的简单表单,并在用户输入数据时拼出事件序列。
  • 所以您想选择州,那么建议框只会建议该州的郊区?选择状态的代码是什么样的?

标签: java jsf richfaces


【解决方案1】:

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

查看这篇涉及类似内容的博文:RichFaces - SuggestionBox and hidden field

关键是使用&lt;f:setPropertyActionListener value="#{...}" target="#{...}"&gt;包裹在&lt;a4j:support event="onselect" ajaxSingle="true"&gt;中。当为 SuggestionBox 触发 onselect 时,这可用于为支持 bean 设置附加值。

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

【讨论】:

【解决方案2】:

&lt;rich:suggestionbox 中使用&lt;f:parameter 标签有效吗?

【讨论】:

  • 虽然这为我指明了正确的方向,但这不是一个答案。不过,它确实包含有用的信息
【解决方案3】:

你看过这个 RichFaces 建议框demo 了吗?示例下有链接可以查看源码。

编辑:

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

【讨论】:

    【解决方案4】:

    您可以使用rich:suggestionbox 中的&lt;f:parameter 选项卡。我的任务是根据列表元素的某些属性过滤列表,有时该属性可能会被忽略。比如,有时我想要一个只有柑橘类水果的列表,有时我想要一个可用水果的完整列表。

    在页面中:

    <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>
    

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

    Basket:

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

    在 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-26
      • 2011-06-09
      • 1970-01-01
      • 2012-07-19
      相关资源
      最近更新 更多