【问题标题】:Struts 2 Avoid duplicate actionStruts 2 避免重复动作
【发布时间】:2014-05-25 19:55:09
【问题描述】:

考虑以下示例中的 struts 2 应用程序。这是一个显示网格的页面,用户可以将 SAME 网格导出为 PDF。

我们开发了一个包含两个动作的类,一个动作以 JSON 格式返回网格,另一个将 SAME 网格导出为 PDF。

代码结构如下:

定义了两个不同的动作图/ShowGrid/ExportGrid

@Action (name="ShowGrid") // Result will be set to JSON
@validation ( @Required (..... // Validation Rules for fromDate toDate etc
public String showGrid(){
gird = serviceFacade.getGrid(fromDate,toDate);
return SUCCESS;
}


@Action (name="ExportGrid" ) //Result will be set as stream
@validation ( @Required ..... // Validation Rules for fromDate toDate etc
public String exportGrid(){
grid = serviceFacade.getGrid(fromDate,toDate);
inputStream = convertGridToStream(grid); // By using jasper report or other tools
return SUCCESS;
}

如您所见,上述方法有很多共同的结构,

  • 验证重复(在我的用例中,验证规则很多)
  • 调用服务方法重复

有什么办法可以避免吗?!

【问题讨论】:

    标签: java configuration struts2 annotations struts-validation


    【解决方案1】:

    您可以使用@Actions 注释将许多操作映射到同一个方法。在 actions 方法中,您可以从操作上下文中获取名称并定义逻辑。

    @Actions({
      @Action ("ExportGrid"), //Result will be set as stream
      @Action ("ShowGrid") // Result will be set to JSON
    })
    @validation ( @Required ..... // Validation Rules for fromDate toDate etc
    public String doGrid(){
      grid = serviceFacade.getGrid(fromDate,toDate);
      if (ActionContext.getContext().getName().equals("ExportGrid")
        inputStream = convertGridToStream(grid); // By using jasper report or other tools
      return SUCCESS;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-11
      • 1970-01-01
      • 2015-02-18
      • 2015-03-17
      • 2018-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-08
      相关资源
      最近更新 更多