从 Java 类传递 Arraylist 并在 Struts 2 的 JSP 页面中获取它 [英] Pass Arraylist from Java class and fetch it in JSP page in Struts 2

查看:24
本文介绍了从 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 页面中获取它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆