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!
${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 yourobjlistUsersarray is correctly populated with data, you should be able to useaaa = objlistUsers[i];in your for-loop.listUsersArrayList into javascript. See these and related questions: stackoverflow.com/questions/10780996/… and stackoverflow.com/questions/12076654/…