• JS异步传递数组Action接受的实现与疑惑


      最近开发中遇到了页面传递数组到Action后台List类型,接受到的list对象并不是想象的按照数组元素位置对应的接受,例如数组的0位置插入到list对象的0位置,

    而是数组的全部内容全部插入到了list集合的第一位置。经过反复的试验,没有找到很好的解决办法,只能把这种粗糙的实现方式记录下来,以求抛砖引玉望大家能给出更好的实现方式。

    这是jsp页面代码:异步提交数组到Action中:

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    <script src="plugin/dwz/js/jquery-1.7.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        function testceshi(log) {
            var v = new Array();
            v = $(".testname");
            var v1 = new Array();
            for(var i = 0; i < v.length; i++) {
                v1[i] = v[i].value;
            }
            $.ajax({  
            url : "loginAction!testarray",  
            type : "POST",  
            data : "ajaxField=" + v1,  
            success : function(data) {  
            alert(data);  
                    }  
            });  
        }
    </script>
    </head>
    <body>
            <form action="loginAction!success" method="post">
            测试<input class="testname" type="text"/><br>
            测试<input class="testname" type="text"/><br>
            测试<input class="testname" type="text"/><br>
            测试<input class="testname" type="text"/><br>
            测试<input class="testname" type="text"/><br>
            <input type="button" value="测试" onclick="testceshi(11)"/>
            </form>
    </body>
    </html>

     后台java代码:接受页面传递的数组,经过测试数组全部插入到list集合的第一个位置。

    @Controller
    @Scope("prototype")
    public class LoginAction extends ActionSupport {
        private List<String> ajaxField = new ArrayList();
        public void testarray() {
            String str = (String) ajaxField.get(0);
            System.out.println(str);
            List<String> list = new ArrayList<String>();
            String[] strs = str.split(",");
            for (String string : strs) {
                System.out.println(string);
                list.add(string);
            }
            for(int i = 0; i < ajaxField.size(); i++) {
                System.out.println(ajaxField.get(i));
            }    
            HttpServletResponse response = ServletActionContext.getResponse();
            try {
                PrintWriter write = response.getWriter();
                response.setContentType("text/html");
                        write.print("hello");  
                        write.flush();  
                        write.close();  
            } catch (IOException e) {
                e.printStackTrace();
            }
        }        

      虽说实现数组传递的过程有点瑕疵,但是对于异步Ajax传递的实现方式还是正确的,对于不传递数组,只传递变量的情况,还是可以参考的。

  • 相关阅读:
    selinux 关闭
    Microsoft Visual Studio 2013 Language Pack
    Visual Studio Ultimate 2013 with Update 4
    页面滑动
    Android 适配器
    前端空格显示问题
    Your content must have a ListView whose id attribute is 'android.R.id.list'
    Ext.data.Store动态修改url
    Android 页面滑动
    实例化Layout中的布局文件(xml)
  • 原文地址:https://www.cnblogs.com/homeOfJain/p/3801341.html
Copyright © 2020-2023  润新知