如何在struts迭代器中将对象列表传递给js函数

How to pass list of objects to js function in struts iterator

本文关键字:列表 函数 js 对象 struts 迭代器      更新时间:2023-09-26

在JSP页面中,我使用struts迭代器

<s:iterator value="categoryList" status="category"> //other code.. <a href="#" onclick="a_onClick('<s:property value="productsByOid"/>')"><s:property value="name"/></a> </s:iterator>

我想用对象列表作为参数来调用js函数。在那里,我想迭代这个列表并获得产品属性(名称、价格)。我该怎么做?

 function a_onClick(p) {
    alert('a_onClick');
    console.log(p);
}

在控制台上接收:[pl.model.Product@3270eb8a]

碎片模型类别:

public class Category {
private Integer oid;
private String name;
private Collection<Product> productsByOid;

}

public class Product {
  private Integer oid;
  private Integer dostPno;
  private String opis;
  private String name;
  private String producent;
  private String price;
  private Category categoryByCategoryOid;

如果您的列表打印在jsp页面中,那么您可以使用jquery选择器,并通过迭代执行您想要的任何操作。

如果您想加载堆栈中存在的对象(如您在jsp中迭代的列表),则必须使用struts2属性标记从堆栈中获取该对象。即

<a onclick='functionsName(<s:propery value="nameOnStack")'

在传递其值时,可能还需要在上面的参数中添加引号。