开发者

struts 2.0.6 ajax

Can someone give me an example of usi开发者_运维百科ng ajax to fill a dropdown using struts 2.0.6?


Use jquery on client side:

$.getJSON("/getDropDownlistValues", function(result) {
    var options = $("#options");
    //don't forget error handling!
    $.each(result, function(item) {
        options.append($("<option />").val(item.value).text(item.text);
    });
});

On server side write this key value strings into Servlet response type.

See this post for details:

Setting the content-type of a response in Struts2


I guess you want to populate the second dropdown based on first. Or if you want to populate just single dropdown, you can simply call the action from your view.

This is the example for the former. You can use simple ajax to acheive this.

For Example, city dropdown should be populated based on selected state. Have an onchange event to call this function in state select field.

function getCityList(stateId) {
var http = getHTTPObject();
var url = 'CityList.action?stateId=' + stateId; 
http.open("GET", url, true);
http.onreadystatechange = function() {
    if (http.readyState == 4) {
        if (http.status == 200) {
            document.getElementById("city").innerHTML = http.responseText;
        }
    }
};
http.send(null);}

this function call the action and inturn the action returns the action page. In this case, the entire action page is the responseText. So what you should do is just have only the second dropdown in that action success page. The entire action page is shown in the

<div id="city"></div

Then you can change the view as per your needs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜