0

I am trying to use jquery to get arraylist from action. And then get data from per row of that arraylist. My code like below:

My Controller Action (UserAction.java):

public class UserAction  extends ActionSupport {

private ArrayList<UserVO> listUsers;
public ArrayList<UserVO> getListUsers() {
    return listUsers;
}
public void setListUsers(ArrayList<UserVO> listUsers) {
    this.listUsers = listUsers;
}

public String execute(){

    listUsers = new ArrayList<UserVO>;
    UserVO bean = new UserVO();
    bean.setUserName("john");
    bean.setLastName("mit");
    listUsers.add(bean);

    bean = new UserVO();
    bean.setUserName("Madam");
    bean.setLastName("Git");
    listUsers.add(bean);

    return SUCCESS;
}
}

And UserVO

    public class UserVO  {
         private String userName;
         private Strnig lastName;

         //get, set methods 
             ....
    }

And my jsp:

user.jsp
      <script type="text/javascript">
         jQuery(document).ready(function($){

            var objlistUsers = '${listUsers}';  //get all list from action
            for (var i = 0; i < 2; i++) {
                 var aUser = "objlistUsers[test]";

                 var aaa = aUser.replace("test", i);

                 aaa = '${'+aaa+'}';  ///My purpose: I want to get data per row: ${listUsers[i]}            
                 alert(aaa);  //but can not return exactly
          }

        });

      </script>

I want to get data of per row in list, like: ${listUsers[i]} How I can get it?

Thank you!

Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
MartinJoo
  • 2,784
  • 9
  • 33
  • 39
  • JSP processing, including EL evaluation (like replacing `${listUsers}` with data) is done on the server before the page is sent to the browser. Once the page is sent to the browser, jQuery (javascript) runs there. If your javascript generates EL strings (e.g. `'${'+aaa+'}'`), they will never be processed because the JSP processing stage is over. If your `objlistUsers` array is correctly populated with data, you should be able to use `aaa = objlistUsers[i];` in your for-loop. – tcovo May 28 '13 at 03:07
  • I tried to get object: objlistUsers[i]. but it return is not exactly – MartinJoo May 28 '13 at 03:18
  • I think you might need to do a bit more work to pass the `listUsers` ArrayList into javascript. See these and related questions: http://stackoverflow.com/questions/10780996/how-can-we-get-javascript-array-by-jsp-scriptlet and http://stackoverflow.com/questions/12076654/assigning-a-java-set-to-a-javascript-array-using-el – tcovo May 28 '13 at 03:26

1 Answers1

0

You can use jstl tags for that

   <script>
    <c:forEach var="user" items="${listUsers}">
      alert('${user.userName}');
    </c:forEach>
 </script>

Use this import before using JSTL tags

 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    
PSR
  • 39,804
  • 41
  • 111
  • 151
  • tag is the same with tag of struts. But my case, I want to get per UserVO data by Javascript, not show in JSP. – MartinJoo May 28 '13 at 03:05