Js background processing and set the value of subscript List method

First, using a struts2 frame, two cases:

1. The list which is a collection of values, not objects, background code:

Copy the code
public class myTest {
    ArrayList<String> mylist;
    
    public ArrayList<String> getMylist() {
        return mylist;
    }

    public void setMylist(ArrayList<String> mylist) {
        this.mylist = mylist;
    }

    public String test(){
        mylist = new ArrayList<String>();
        mylist.add("aa");
        mylist.add("bb");
        mylist.add("cc");
        mylist.add("dd");
        return "success";
    }
}
Copy the code

Front Code:

 

First, the introduction of the appropriate label and js library

(1) introducing jstl tag library, <% @ taglib uri = "HTTP: // the Java .sun.com / JSP / jstl / Core"  prefix = "c" %>

(2)引入jQuery库,<script type="text/JavaScript" src="js/jquery.js"></script>

 (3) js Code:

Copy the code
<script type="text/javascript">
    var array = new Array();
    //console.info("info");
    <c:forEach items="${mylist}" var="item" varStatus="status" >
        array.push("${item}");
        //获得值
        alert("${item}");
        //获得其下标
        alert("${status.count}");
        //var temp = "${item}";
    </c:forEach>
    for(var i=0;i<array.length;i++){
        alert(array[i]);
    }
    
</script>
Copy the code

2. The list set is an object, not the value

(1) the code behind:

 

Copy the code
package com.beans;

public class Dog {
    String name;
    int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

public class myTest {
    ArrayList<Dog> mylist;

    public ArrayList<Dog> getMylist() {
        return mylist;
    }

    public void setMylist(ArrayList<Dog> mylist) {
        this.mylist = mylist;
    }

    public String test(){
        mylist = new ArrayList<Dog>();
        Dog dog1 = new Dog();
        Dog dog2 = new Dog();
        dog1.setName("wangwang");
        dog1.setAge(121);
        dog2.setName("miaomiao");
        dog2.setAge(151);
        mylist.add(dog1);
        mylist.add(dog2);
        return "success";
    }

}
Copy the code

 

(2) Js Code:

 

Copy the code
<Script type = "text / JavaScript"> 
    var = new new Array the Array (); 
    //console.info("info "); 
    <C: forEach items =" $ {mylist} "var =" Item "varStatus =" Status "> 
        Array.push (" Item $ {} "); 
        var TEMP =" Item {} $ "; 
        // get a lower subscript 
        Alert (" $ {status.count} "); 
        // passed over a string , quotes 
        Alert ( "item.name, for $ {}"); 
        // int type is passed over, without the quotation marks 
        Alert (item.age $ {}); 
    </ C: forEach> 
</ Script>
Copy the code

Guess you like

Origin www.cnblogs.com/pypua/p/12336347.html