I am trying to get ArrayList in JSP page passed from java class. But eventually I didn't succeed. Here is what I have done.
Here is my POJO class:
public class Coordinates {
private double latitude;
private double longitude;
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
And this one is Java class where I write business logic:
public class Leverage extends ActionSupport{
List mylist = new ArrayList();
public String getMapDetail()throws Exception{
LevService lev =LevService .getInstance();
mylist = lev .getCurrentLocation();
System.out.println("Size of list is: "+mylist.size());
return SUCCESS;
}
Here is my JSP page:
It prints the size of ArrayList in console. But it doesn't create the row.
解决方案
The iterator tag expects a myList, so you should provide a getter
public List getMyList() {
return myList;
}
This value should be initialized like you did
private List myList = new ArrayList<>();
Then you should not override it in the action, just create a local variable or rename a variable returned by the service.
List list = lev.getCurrentLocation();
if (list != null && list.size() > 0)
myList = list;
In the JSP you can get the values from the iterator tag, it finds all values referenced inside the body of the tag in the value stack. You don't need to provide an indexed expression to get the values.