【问题标题】:Struts2 jQuery Autocompleter with select box带有选择框的 Struts2 jQuery 自动完成器
【发布时间】:2012-12-18 16:51:57
【问题描述】:

我已将 Struts2 jQuery autocompleter 用于我的 Struts 2 应用程序。

这是我的代码:

JSP:

 <s:form id="frm_demo" theme="simple" action="ManagersAutoCompleter1">
        <s:url var="remoteurl" action="test" />
    <sj:autocompleter href="%{remoteurl}" id="echo3" name="echo"
        list="itemList" listKey="id" listValue="name" emptyOption="true"
        headerKey="-1" headerValue="Please Select a Language" selectBox="true" />

        <s:submit value="submit" />
    </s:form>

Struts.xml:

<action name="test" class="test.TestAction" method="populate">
  <result type="json">
  </result>
</action>

动作类:

 public String populate() throws Exception {

        itemList = new ArrayList<ListValue>();
        itemList.add(new ListValue("Php", "Php"));
        itemList.add(new ListValue("Java", "Java"));
        itemList.add(new ListValue("Mysl", "Mysl"));
        return SUCCESS;
    } //getter setter for itemList

列表类:

public class ListValue {
    private String id;
    private String name;

    public ListValue(String id, String name) {
        this.id = id;
        this.name = name;
    } //getter setter methods

但是这个 Struts2 jQuery autocompleter 不工作。它不填充任何值。

【问题讨论】:

    标签: java json jsp struts2 struts2-jquery


    【解决方案1】:

    做这个

    <s:url id="remoteurl" action="test"/>
    <sj:select 
         id="customersjsonlstid" 
         name="echo"
         label="Handle a List"
         href="%{remoteurl}" 
         list="itemList"
         listValue="name" 
         listKey="id" 
         autocomplete="true"  
         loadMinimumCount="2" 
         id="echo3"/>
    

    不是这个

    <sj:autocompleter href="%{remoteurl}" id="echo3" name="echo"
    list="itemList" listKey="id" listValue="name" emptyOption="true"
    headerKey="-1" headerValue="Please Select a Language" selectBox="true" />
    

    并确保您从您的操作类返回列表。 要检查这一点,请使用您的 IDE 调试器或 System.out.print 等进行。

    ex...
    
    
        -------------
        ------------
        itemList.add(new ListValue("Mysl", "Mysl") );
        System.out.println("Size of my list="+itemList.size());
        return SUCCESS;
    }
    

    你还应该在你的动作类中定义 getter 和 setter

    private List itemList; 
        public List getItemList() {
        return itemList;
    } 
    
    public void setItemList(List itemList) {
        this.itemList = itemList;
    }
    

    【讨论】:

    • awesome..awesome 它使用 json 工作!!!,实际上我是 JQuery 的新手,非常感谢您的支持
    【解决方案2】:

    这是错误的:

    <sj:autocompleter href="%{remoteurl}" id="lst" name="lst"
        list="itemList" listValue="name" listKey="id" selectBox="true" />
    

    您正在为自动完成器提供地图,而不是您自己构建的自定义对象。

    HashMap 没有任何 nameid 字段,而是有 keys 和 values 字段。

    首先改变它,看看它是否有效:

    <sj:autocompleter href="%{remoteurl}" id="lst" name="lst"
        list="itemList" listValue="value" listKey="key" selectBox="true" />
    

    【讨论】:

    • 好的,谢谢。我已经编辑了我的问题。现在我用简单列表替换地图。但它仍然不起作用。你能帮帮我吗?
    【解决方案3】:

    您输入了错误的未引用属性。

    <s:url id="remoteurl" action="test" />
    

    应该是

     <s:url var="remoteurl" action="test" />
    

    使用列表项 bean 类

    public class ListValue {
      private String id;
      private String name;
    ...
    }
    
    public String populate() throws Exception {
      itemList.add(new ListValue("Php", "Php"));
      itemList.add(new ListValue("Java","Java") );
      itemList.add(new ListValue("Mysl", "Mysl") );
      return SUCCESS;
    }
    

    假设已经添加了构造函数和修改器。

    【讨论】:

    • Struts2-Jquery 自动完成器不填充任何值,它像文本框一样显示。它没有下拉功能。我使用 struts2-jquery-tree-plugin-3.5.0。 jar 和 struts2-json-plugin-2.3.7.jar 插件。
    • 你是否包含了 json-plugin?
    • 但是你的列表项没有id,也没有name属性。
    • 我使用 LabelValueBean 来呈现列表。
    • LabelValueBean 您可以在 Apache 项目中找到。检查编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多