本文介绍了从Java类传递Arraylist并在JSP页面中获取它在Struts 2中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我试图在从java类传递的JSP页面中获取 ArrayList 。但最终我没有成功。这就是我所做的。

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.

这是我的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并在JSP页面中获取它在Struts 2中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:53