zoukankan      html  css  js  c++  java
  • struts2-ajax-传递list集合

    顺便提下,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);
                            });
                        }
                    });
                });
    });


  • 相关阅读:
    Linux常用命令大全
    Shell常用命令整理
    登录织梦后台提示用户名不存在的解决方法介绍
    dedecms系统后台登陆提示用户名密码不存在
    dede被注入后台提示用户名不存在解决方法
    织梦dedecmsV5.7联动类型无法显示的处理方法
    漏洞安全防范
    Sublime 安装、插件CoolFormat
    如何查看sublime安装了哪些插件
    本地如何使用phpstudy环境搭建多站点
  • 原文地址:https://www.cnblogs.com/wyang0126/p/5039924.html
Copyright © 2011-2022 走看看