使用struts标记在JSP中迭代HashSet

时间:2017-06-20 14:22:36

标签: jsp struts2

我是JSP的新手。我有一个Employer bean对象列表,它有一个Employee info属性。

   List<Employer> employerList;

   class Employer{
      private Set<EmployeeInfo> employeeInfo;

      public Set<EmployeeInfo> getEmployeeInfo() {
        return employeeInfo;
      }

     public void setEmployeeInfo(Set<EmployeeInfo> employeeInfo) {
       this.employeeInfo= employeeInfo;
     }
  }

class EmployeeInfo{
      private String name;

      public setName(String name){
         this.name=name;
      }

       public getName(){
          return name;
      }
}

我正在尝试检查employeeSetInfo是否为空/空,然后使用struts 2标记显示jsp中每个员工的姓名。

<s:if test="%{employerList.employeeInfo.size>0}">
    <s:iterator value="employerList.employeeInfo" var="employee">
        <s:property value="#employee.name" />
    </s:iterator>
</s:if>

然而,它不起作用。

2 个答案:

答案 0 :(得分:1)

很多事情,首先要直接回答你的问题,如评论中所提到的,你需要迭代你的清单:

<html>

<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

</head>

<script src="script.js"></script>

<body ng-app="appModule" class="ng-scope">

  <div class="row">
    <div class="col-xs-12 col-sm-12" ng-controller="ProfileTickerController">
      <div class="form-group">
        <label for="subject">
                        Ticker 
                        <span ng-show="!edit">
                            <a href="javascript:void(0);" title="Edit Ticker" ng-click="editTicker()"><i class="glyphicon glyphicon-pencil"></i></a>
                        </span>
                        <span ng-show="edit">
                            <a href="javascript:void(0);" title="Save Ticker" ng-click="saveTicker()"><i class="glyphicon glyphicon-ok"></i></a> 
                            <a href="javascript:void(0);" title="Cancel" ng-click="cancelEdit()"><i class="glyphicon glyphicon-remove"></i></a>
                        </span>
                    </label>
        <textarea name="ticker_edit" id="ticker_edit" class="form-control textarea-non-resize" ng-model="ticker" ng-show="edit" placeholder="Customize your ticker here" required cols="50" rows="4" style="margin-bottom: 10px;"></textarea>
        <div class="grey-box">
          Preview:
          <div id="ticker" class="text-center">
            <h3 compile="ticker"></h3>
          </div>
        </div>
      </div>
    </div>
  </div>

</body>

</html>

现在减少空值检查(这只是为了避免JSP中的疯狂空值检查,并且对任何方式的答案都不重要)...

在EmployeeInfo中放置一个构造函数,并且不允许设置null,因为它没有意义。在构造期间初始化数据结构(静态初始化很好)。然后使用getter获取容器并添加到列表/集合中。然后就不需要检查null。您仍然可以保留容器的setter(set,list,whatever),但也许保护它以提醒您只有关闭的东西(应该在同一个包中应该能够直接添加值,并希望知道不要分配null和其他遥远的事情没有业务,但与公共吸气者合作)。

如果你不想这样做,你可以用以下方法包装所有这些迭代器和属性访问:

<!-- the "test" attribute is evaluated by default so we don't need to wrap it with 
%{}, also note that you are not testing for null so this isn't safe, your code 
should be modified to prevent nulls, where reasonable. For this reason the
if can be omitted-->

<s:iterator value="employerList"> <!-- will push each Employer's attributes to the top of the stack exposing its values --> 
   <s:iterator value="employeeInfo"> <!-- same at other iterator -->
      <s:property value="name"/>
   </s:iterator>
</s:iterator>

此外,您无法遵循此建议(经常发生)<s:if test="something != null"> <!-- Do stuff with verified to be safe reference --> </s:if> <s:else> <!-- maybe state there are no values so you aren't left wondering --> </s:else> 来拯救您。

答案 1 :(得分:0)

使用fn:length函数。

在JSP文件的开头声明fn名称空间

String

稍后在代码中

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

https://stackoverflow.com/a/26962901/4525175

Source