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!

3
  • 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. Commented May 28, 2013 at 3:07
  • I tried to get object: objlistUsers[i]. but it return is not exactly Commented May 28, 2013 at 3:18
  • I think you might need to do a bit more work to pass the listUsers ArrayList into javascript. See these and related questions: stackoverflow.com/questions/10780996/… and stackoverflow.com/questions/12076654/… Commented May 28, 2013 at 3:26

1 Answer 1

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" %>    
Sign up to request clarification or add additional context in comments.

1 Comment

<c:forEach> tag is the same with <s:iterator> tag of struts. But my case, I want to get per UserVO data by Javascript, not show in JSP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.