如何在Struts2网格中显示模型类的嵌套属性? [英] How to display nested properties of a model class in Struts2 grid?

查看:59
本文介绍了如何在Struts2网格中显示模型类的嵌套属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个

可以看出,在几列中有一些嵌套的属性.它们没有在给定的网格中列出(显示).关联的列只是留为空白.其余字段将照常显示.

As can be seen, there are some nested properties in a few columns. They are not listed (displayed) in the given grid. The associated columns are simply left blank. The rest of the fields are displayed as usual.

我也尝试将它们包含在%{...}内,但无济于事.

I have also tried enclosing them within %{...} but to no avail.

如何在网格中显示此类嵌套属性?对他们有什么特殊待遇吗?

How to display such nested properties in a grid? Is there any special treatment for them?

已验证这些嵌套属性是从数据库中获取的,并且模型已正确初始化.

动作类:

@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value="json-default")
@InterceptorRefs({@InterceptorRef(value="store", params={"operationMode", "AUTOMATIC"})})
public final class ProductAction extends ActionSupport implements Serializable, ModelDriven<Product>
{
    @Autowired
    private final transient ProductService productService=null;
    private static final long serialVersionUID = 1L;

    private Product entity=new Product();
    private List<Product>gridModel=new ArrayList<Product>();

    private String id;
    // Get how many rows we want to have into the grid - rowNum attribute in the grid
    private Integer rows=5;
    // Get the requested page. By default grid sets this to 1.
    private Long page=1L;
    // sorting order - asc or desc
    private String sord;
    // get index row - i.e. user click to sord.
    private String sidx;
    // Search Field
    private String searchField;
    // The Search String
    private String searchString;
    // The Search Operation ['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc']
    private String searchOper;
    // Your Total Pages
    private Long total;
    // All Records
    private Long records;
    private String oper;

    @Override
    public Product getModel() {
        return entity;
    }

    @Action(value = "ProductCRUD",
    results = {
        @Result(name = ActionSupport.SUCCESS, location = "Product.jsp"),
        @Result(name = ActionSupport.INPUT, location = "Product.jsp")},
    interceptorRefs = {
        @InterceptorRef(value = "defaultStack", params = {"validation.validateAnnotatedMethodOnly", "true", "validation.excludeMethods", "load"})})
    public String edit() throws Exception {

        if(oper.equalsIgnoreCase("add")) {
            // Add a row.
        }
        else if(oper.equalsIgnoreCase("edit")) {
            // Update a row.
        }
        else if(oper.equalsIgnoreCase("del")) {
            // Delete a row.
        }
        return ActionSupport.SUCCESS;
    }

    @Action(value = "ProductGrid",
    results = {
        @Result(name = ActionSupport.SUCCESS, type = "json", params = {"includeProperties", "gridModel\\[\\d+\\]\\.prodId, gridModel\\[\\d+\\]\\.prodName, gridModel\\[\\d+\\]\\.prodCode, gridModel\\[\\d+\\]\\.prodDesc, gridModel\\[\\d+\\]\\.marketPrice, gridModel\\[\\d+\\]\\.salePrice, gridModel\\[\\d+\\]\\.featured, gridModel\\[\\d+\\]\\.expressDelivery, gridModel\\[\\d+\\]\\.weight, gridModel\\[\\d+\\]\\.occassion, gridModel\\[\\d+\\]\\.quantity, gridModel\\[\\d+\\]\\.visible, gridModel\\[\\d+\\]\\.latest, gridModel\\[\\d+\\]\\.subCategory, gridModel\\[\\d+\\]\\.fabric, gridModel\\[\\d+\\]\\.brand, gridModel\\[\\d+\\]\\.subCategory\\[\\d+\\]\\.category, total, records, rows, page, sord, sidx, searchField, searchString, searchOper", "excludeNullProperties", "true"})},
    interceptorRefs = {
        @InterceptorRef("params")})
    public String executeAction() {
        records=productService.rowCount().longValue();
        total=new BigDecimal(records).divide(new BigDecimal(rows), 0, BigDecimal.ROUND_CEILING).longValue();

        gridModel=productService.getList((int)(page-1)*rows, rows, new HashMap<String, String>(){{put(sidx, sord);}}, null);
        return SUCCESS;
    }

    public String getJSON() {
        return executeAction();
    }

    public List<Product> getGridModel() {
        return gridModel;
    }

    public void setGridModel(List<Product> gridModel) {
        this.gridModel = gridModel;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public Long getPage() {
        return page;
    }

    public void setPage(Long page) {
        this.page = page;
    }

    public String getSord() {
        return sord;
    }

    public void setSord(String sord) {
        this.sord = sord;
    }

    public String getSidx() {
        return sidx;
    }

    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    public String getSearchField() {
        return searchField;
    }

    public void setSearchField(String searchField) {
        this.searchField = searchField;
    }

    public String getSearchString() {
        return searchString;
    }

    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }

    public String getSearchOper() {
        return searchOper;
    }

    public void setSearchOper(String searchOper) {
        this.searchOper = searchOper;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public Long getRecords() {
        return records;
    }

    public void setRecords(Long records) {
        this.records = records;
    }

    public String getOper() {
        return oper;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }

    @Action(value = "Product",
        results = {
            @Result(name=ActionSupport.SUCCESS, location="Product.jsp"),
            @Result(name = ActionSupport.INPUT, location = "Product.jsp")},
        interceptorRefs={
            @InterceptorRef(value = "defaultStack", params = {"validation.validateAnnotatedMethodOnly", "true", "validation.excludeMethods", "load"})})
    public String load() throws Exception {
        // This method is only needed to return an initial view on page load. Nothing to see here. Leave it empty.
        return ActionSupport.SUCCESS;
    }
}

作为响应,所有这些嵌套属性均为空.一行的JSON响应如下.

In response, those all nested properties are empty. The JSON response for a single row is as follows.

{
  "gridModel": [
    {
      "brand": {

      },
      "expressDelivery": false,
      "fabric": {

      },
      "featured": true,
      "latest": false,
      "marketPrice": 12.00,
      "occassion": "222",
      "prodCode": "aaa",
      "prodDesc": "xxx",
      "prodId": 5,
      "prodName": "ddd",
      "quantity": 1,
      "salePrice": 12.00,
      "subCategory": {

      },
      "visible": true,
      "weight": 22.00
    }    
  ],
  "page": 1,
  "records": 5,
  "rows": 5,
  "sidx": "",
  "sord": "asc",
  "total": 1
}

推荐答案

includePproperties参数中,您需要

  • 指定内部属性的完整路径
  • 转义点
  • 在应用于非集合的对象时删除第二个\[\d\]表示法
  • specify the full path to the inner properties
  • escape the dots
  • remove the second \[\d\] notation when applied to objects that are not collections

一个例子:

错误:gridModel\\[\\d+\\]\\.subCategory\\[\\d+\\]\\.category

:gridModel\\[\\d+\\]\\.subCategory\\.category\\.catName

这篇关于如何在Struts2网格中显示模型类的嵌套属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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