Pass Arraylist from Java class and fetch it in JSP page in Struts 2(从 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>
这篇关于从 Java 类传递 Arraylist 并在 Struts 2 的 JSP 页面中获取它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 Java 类传递 Arraylist 并在 Struts 2 的 JSP 页面中获取它
基础教程推荐
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
