【问题标题】:How to populate struts 2 ui select tag by json returned list using jquery?如何使用jquery通过json返回列表填充struts 2 ui select标签?
【发布时间】:2013-10-10 05:19:13
【问题描述】:
//struts.xml
    <package name="ajax" namespace="/" extends="struts-default,json-default">
        <action name="CheckUserAction" class="com.controller.ajax.AjaxAction">
            <result type="json">
            <param name="root">jsonData</param>
            </result>
        </action>
    </package>



    //Action Class
public class AjaxAction extends ActionSupport {
private String capacity;
private String greeting;
private List<String> countryList;
private Map<String, Object> jsonData;
public String execute() throws Exception {
    jsonData = new LinkedHashMap<String, Object>();
    greeting = "HI " + "AMIT";
    countryList = new ArrayList<String>();
    countryList.add("US");
    countryList.add("UK");
    countryList.add("Russia");
    jsonData.put("capacity", this.getCapacity());
    jsonData.put("countryList", countryList);
    jsonData.put("countryMap", countryMap);
    jsonData.put("greeting", greeting);
    return SUCCESS;
}
//Getter and setters

}

//Script
$(document).ready(function() {
        $("#capacity").change(function() {
            var capacity = $("#capacity").val();
            $.getJSON('CheckUserAction.action', {
                capacity : capacity
            }, function(data) {
                $('#abc').append('<br>' + data.capacity);
                $('#abc').append('<br>' + data.countryList);
                $('#xyz').attr('value', data.greeting);
                $('#pqr').attr('list', data.countryList);
            });
            return false;
        });
    });





//JSP code
<table border="0">

        <tr>
            <td><s:label for="capacity" value="Capacity" /></td>
            <td><s:textfield name="capacity" placeholder="100" id="capacity"
                    value="abc"></s:textfield></td>
        </tr>
        <tr>
            <td><s:label for="dynamic" value="Ajax" /></td>
            <td><s:textfield name="dynamic" value="learning" id="xyz"></s:textfield></td>
        </tr>
        <tr>
            <td><s:label for="flightId" value="Flights" /></td>
            <td><s:select list="#{'1':'1' }" name="fightId" id="pqr"></s:select></td>
    </tr>
</table>
<div id="abc">Initial</div>

我想用 json 返回的列表 countryList 填充列表(id:pqr),我能够更改文本字段(id:xyz)的属性值,我也将它附加到 div(id:abc)但我不能更改 struts 选择标记的属性列表。 是否与如何在 HTML 中填充 struts 标签有关。

我也没有使用 struts-jquery 插件或 dojo 插件。 谢谢

【问题讨论】:

标签: jquery struts2 struts struts2-jquery struts2-json-plugin


【解决方案1】:

您在 data.countryList 中收到了什么样的数据?你可以发布那个数据吗?我希望下拉列表没有名为 list 的 attr 属性。列出由 Struts 提供的属性。您可以像

那样为下拉菜单组合数据
<option value="1">India</option>
<option value="2">United States</option>
<option value="3">United Kingdom</option>

如果您要发送新列表,您可以填写 countryList,例如,

$('#pqr').html(composed_country_list);

否则,您正在更新国家意味着您可以将数据附加到下拉列表中,例如,

$('#pqr').append(composed_country_list);

如果这有帮助,请告诉我..

【讨论】:

    【解决方案2】:

    以下代码对我有用

         //JSP
        <s:select list="{}" name="fightId" id="pqr"></s:select>
    

    脚本文件

        $(document).ready(function() {
        $("#capacity").change(function() {
            var capacity = $("#capacity").val();
            $.getJSON('CheckUserAction.action', {
                capacity : capacity
            }, function(data) {
    
                countryListItem = data.countryList;
            });
            return false;
        });
    });       
    
    
    
    
        var pqr = $('#pqr');
        pqr.find('option').remove();
        pqr.append('<option value=-1>--Select--</option>');
         for(var i = 0; i < countryListItem .length; i++) {
                var opt = document.createElement('option');
                opt.innerHTML = countryListItem [i];
                opt.value = countryListItem [i];
                pqr.append(opt);
    
            }
    

    使用 javascript 通过创建选项属性来填充列表。 struts 的 select 标签实际上在内部生成了这种 HTML。

    【讨论】:

      猜你喜欢
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多