从 Java 类传递 Arraylist 并在 Struts 2 的 JSP 页面中获取它
我试图在从 java 类传递的 JSP 页面中获取 ArrayList
.但最终我没有成功.
I am trying to get ArrayList
in JSP page passed from java class. But eventually I didn't succeed.
这是我所做的:
这是我的 POJO 类:
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;
}
}
这是我编写业务逻辑的Java类:
And this one is Java class where I write business logic:
public class Leverage extends ActionSupport{
List<Coordinates> mylist = new ArrayList<Coordinates>();
public String getMapDetail()throws Exception{
LevService lev =LevService .getInstance();
mylist = lev .getCurrentLocation();
System.out.println("Size of list is: "+mylist.size());
return SUCCESS;
}
这是我的 JSP 页面:
<Table>
<s:iterator value="mylist" status="Status">
<tr>
<td><s:property value="%{mylist[#Status.index].latitude}" /></td>
<td><s:property value="%{mylist[#Status.index].longitude}" /></td>
</tr>
</s:iterator>
</Table>
它在控制台中打印 ArrayList
的大小.但它不会创建行.
It prints the size of ArrayList
in console. But it doesn't create the row.
推荐答案
迭代器标签需要一个 myList
,所以你应该提供一个 getter
The iterator tag expects a myList
, so you should provide a getter
public List<Coordinates> getMyList() {
return myList;
}
这个值应该像你一样初始化
This value should be initialized like you did
private List<Coordinates> 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<Coordinates> list = lev.getCurrentLocation();
if (list != null && list.size() > 0)
myList = list;
在 JSP 中,您可以从迭代器标签中获取值,它会在值堆栈中查找标签主体内引用的所有值.您无需提供索引表达式即可获取值.
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.
<table>
<s:iterator value="myList">
<tr>
<td><s:property value="%{latitude}" /></td>
<td><s:property value="%{longitude}" /></td>
</tr>
</s:iterator>
</table>
相关文章