顺便提下,struts2 Action中的方法不要以get开头命名,否则这个方法会执行两次的

先贴上struts.xml

	<package name="demo3" extends="json-default" namespace="/demo3">
		<action name="*_*" class="com.lan.action.json.{1}Action"
			method="{2}" >
			<result name="success" type="json"></result>
		</action>
	</package>

贴上action

package com.lan.action.json;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.json.annotations.JSON;

import com.lan.Bean.Person;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.inject.Scoped;

/**
 * Struts2-Json 返回一个list
 * 
 * @author wy
 * 
 */

public class Demo3Action extends ActionSupport {
	
	private List<Person> persons = new ArrayList<Person>();

	private String type;

	public String gainList() {
		Person p1 = new Person("ABC", 10, "M");
		Person p2 = new Person("DEF", 20, "F");
		Person p3 = new Person("XYZ", 30, "M");
		persons.add(p1);
		persons.add(p2);
		persons.add(p3);
		System.out.println(persons.size());
		return SUCCESS;
	}

	@JSON
	public List<Person> getPersons() {
		return persons;
	}

	public void setPersons(List<Person> persons) {
		this.persons = persons;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}
贴上js

/*
 * ajax list對象
 */
//load
$(function() {
    $("#list").load("demo3/Demo3_gainList.action", {}, function() {
    });
});
// post
$(function() {
    $("#button-list-001").click(
            function() {
                $.post("demo3/Demo3_gainList.action", {
                    type : "post"
                }, function(data) {
                    $("#list-type").html(data.type);
                    $.each(data.persons, function(i, list) {
                        var v_tr = "<tr> <td>" + list.name + "</td><td>"
                                + list.age + "</td><td>" + list.sex
                                + "</td></tr>";
                        $("#list-table").append(v_tr);
                    });
                });
            });
});
// get
$(function() {
    $("#button-list-002").click(
            function() {
                $.get("demo3/Demo3_gainList.action", {
                    type : "get"
                }, function(data) {
                    $("#list-type").html(data.type);
                    $.each(data.persons, function(i, list) {
                        var v_tr = "<tr> <td>" + list.name + "</td><td>"
                                + list.age + "</td><td>" + list.sex
                                + "</td></tr>";
                        $("#list-table").append(v_tr);
                    });
                });
            });
});
// ajax
$(function() {
    $("#button-list-003").click(
            function() {
                $.ajax({
                    url : "demo3/Demo3_gainList.action",
                    type : "post",
                    data : "type=" + "ajax",
                    dataType : "JSON",
                    success : function(data) {
                        $("#list-type").html(data.type);
                        $.each(data.persons, function(i, list) {
                            var v_tr = "<tr> <td>" + list.name + "</td><td>"
                                    + list.age + "</td><td>" + list.sex
                                    + "</td></tr>";
                            $("#list-table").append(v_tr);
                        });
                    }
                });
            });
});


posted on 2013-12-05 14:03  wyang0126  阅读(555)  评论(0编辑  收藏  举报