rich:自动完成前缀

发布于 2024-12-11 07:11:49 字数 2402 浏览 1 评论 0原文

我正在测试自动完成组件,但我不明白如何将前缀(用作搜索输入)传递给 bean。 这是我的代码:

page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:t="http://myfaces.apache.org/tomahawk">

<f:loadBundle basename="resources.application" var="msg" />

<h:head>
    <title>Test page
    </title>
</h:head>

<h:body>
    <rich:panel style="height:500px;overflow:auto">

         <rich:autocomplete mode="cachedAjax" tokens=", " minChars="1"
            autofill="false"
            autocompleteMethod="#{comuniBean.autocomplete}" />


    </rich:panel>


</h:body>
</html>

Bean:

package it.ubi.test.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ComuniBean implements Serializable{

    private static final long serialVersionUID = 1L;

    private ArrayList<String> comuniList;

    public void init() {
        comuniList = new ArrayList<String>();
        comuniList.add("Brescia (BS)");
        comuniList.add("Milano (MI)");
        comuniList.add("Bergamo (BG)");
    }

    public List<String> autocomplete(String prefix) {
        ArrayList<String> result = new ArrayList<String>();
        if ((prefix == null) || (prefix.length() == 0)) {
                comuniList = new ArrayList<String>();
                comuniList.add("Brescia (BS)");
                comuniList.add("Milano (MI)");
                comuniList.add("Bergamo (BG)");
                result = comuniList;
        } else {
            Iterator<String> iterator = comuniList.iterator();
            while (iterator.hasNext()) {
                String elem = iterator.next();
                if ((elem.toLowerCase().indexOf(prefix.toLowerCase()) == 0)
                    || "".equals(prefix)) {
                    result.add(elem);
                }
            }
        }
        return result;
    }


    }

“前缀”变量始终为空。有人可以解释如何获取前缀值吗?

I'm testing the autocomplete component, but I don't understand how prefix (used as search input) is passed to the bean.
Here my code:

page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:t="http://myfaces.apache.org/tomahawk">

<f:loadBundle basename="resources.application" var="msg" />

<h:head>
    <title>Test page
    </title>
</h:head>

<h:body>
    <rich:panel style="height:500px;overflow:auto">

         <rich:autocomplete mode="cachedAjax" tokens=", " minChars="1"
            autofill="false"
            autocompleteMethod="#{comuniBean.autocomplete}" />


    </rich:panel>


</h:body>
</html>

Bean:

package it.ubi.test.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ComuniBean implements Serializable{

    private static final long serialVersionUID = 1L;

    private ArrayList<String> comuniList;

    public void init() {
        comuniList = new ArrayList<String>();
        comuniList.add("Brescia (BS)");
        comuniList.add("Milano (MI)");
        comuniList.add("Bergamo (BG)");
    }

    public List<String> autocomplete(String prefix) {
        ArrayList<String> result = new ArrayList<String>();
        if ((prefix == null) || (prefix.length() == 0)) {
                comuniList = new ArrayList<String>();
                comuniList.add("Brescia (BS)");
                comuniList.add("Milano (MI)");
                comuniList.add("Bergamo (BG)");
                result = comuniList;
        } else {
            Iterator<String> iterator = comuniList.iterator();
            while (iterator.hasNext()) {
                String elem = iterator.next();
                if ((elem.toLowerCase().indexOf(prefix.toLowerCase()) == 0)
                    || "".equals(prefix)) {
                    result.add(elem);
                }
            }
        }
        return result;
    }


    }

The "prefix" variable is always null. Can somebody explain how to get the prefix value?

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

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

发布评论

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

评论(1

雨巷深深 2024-12-18 07:11:49

我找到了这个问题的解决方案:

标签必须在标签内

<h:form>
    <rich:autocomplete id="test" mode="ajax"
                               ...
    </rich:autocomplete>
</h:form>

I found solution for this porblem:

tag must be inside tag

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