如何拥有<s:if>在 Struts 2 中有两个条件 [英] How to have <s:if> with two conditions in Struts 2

查看:26
本文介绍了如何拥有<s:if>在 Struts 2 中有两个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遍历一个项目列表,如果元素的状态等于学生或教师,则需要显示一个特定的下拉列表.以下代码显示所有字段,但不显示任何元素的下拉列表!

I iterate through a list of items, and need to show a specific dropdown list if the state of element is equal to student or teacher. The following code shows all fields but does not show the dropdown for any of the elements!

 <s:iterator value="listOfPeople" status="element">
   .....
  <s:if test='%{elements[#element.index].Status.equalsIgnoreCase("Student") ||
          elements[#element.index].Status.equalsIgnoreCase("Teacher")}'>

            <s:select name="elements[%{#element.index}].Status"
                               id="elements[%{#element.index}].Status"
                               label="Status"
                               list="#{'Student':'Student','Teacher':'Teacher'}"
                               headerKey = "-1"
                               headerValue=" "
                               value="%{Status}"
             />

    </s:if>

在控制器中:

    PeopleModel peopleModel = new PeopleModel();
    listOfPeople = peopleModel.findPeople();
    System.out.println("size is:" + listOfPeople.size());  //returns 8
    return "listPeople";

我的模特:

    List<People> people = new ArrayList();
    final Session session = HibernateUtil.getSession();
    try {
        final Transaction tx = session.beginTransaction();
        try {

            Criteria criteria = session.createCriteria(People.class, "People")
            people.addAll((List<People>) criteria.list());
            if (!tx.wasCommitted()) {
                tx.commit();
            }
            if (session.isOpen()) {
                session.close();
            }
            System.out.println("size is ::: " + people.size());   //returns 8
            return people;

        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
        }
    } finally {
        HibernateUtil.closeSession();
    }
    return null;
}

以下代码的结果

   Status: ${Status}
   <br/>value : [<s:property value="elements[%{#element.index}].Status" />]
                       

Status : Principle
value : [] Status : Student
value : [] Status : Teacher
value : [] Status : Teacher
value : [] Status : Teacher
value : [] Status : Teacher
value : [] Status : Teacher
value : [] Status : Teacher
value : [] 

上面的结果显示第一个人的状态与其他人分开,这就是为什么最后一个值:[] 没有显示任何状态.

The above result is showing the first person's status separate to the other ones thats why the last value : [] is not showing any status.

如果我取出 Status: ${Status} 结果将是

If I take out Status: ${Status} the result would be

value : []
value : []
value : []
value : []
value : []
value : []
value : []
value : []
               

推荐答案

%{} 放在整个表达式周围,而不是像其他标签的其他属性一样放在中间:

Put %{} around the whole expression, not in the middle as in other attributes of other tags:

还为字符串使用更合适的相等函数,就像这里描述的那样

Also use a more appropriate equality function for Strings, like described here

<s:if test='%{elements[#element.index].status.equalsIgnoreCase("Student") ||
              elements[#element.index].status.equalsIgnoreCase("Teacher")}'>

编辑:伙计,你做了很多奇怪的事情;

EDIT: dude, you are doing a lot of odd things;

  1. ${Status}是JSP EL,你不用用;
  2. 你正在迭代一个源,并检查另一个源:打印 <s:property value="elements[%{#element.index}].Status"/> 给你一个空结果,我在您的代码中的任何地方都看不到 elements 东西;
  3. 属性中的第一个大写字母是错误的,因为如果变量名为 foo 并且 getter 是 getFoo(),则在页面中您将有 .foo,而不是 .Foo.如果您的变量名为 Foo,则违反规范/最佳实践,让我们以小写字母开头变量名称.
  1. ${Status} is JSP EL, you have no need of using it;
  2. You are iterating a source, and checking another source: printing <s:property value="elements[%{#element.index}].Status" /> gives you an empty result, and I can't see that elements thing anywhere in your code;
  3. the capital letter as first in an attribute is WRONG, because if the variable is named foo and the getter is getFoo(), in page you will have .foo, not .Foo. If your variable is named Foo, it is against the specs / best practices, let's start variables names with a lowercase letter.

那么如果你有private Object Status,把它改成private Object status;,连同getter和setter,在页面使用:

Then If you have private Object Status, change it to private Object status;, along with the getter and the setter, and in page use:

<s:iterator value="listOfPeople" status="element">
    <s:if test='%{listOfPeople[#element.index].status.equalsIgnoreCase("Student") ||
                  listOfPeople[#element.index].status.equalsIgnoreCase("Teacher")}'>

或使用 var

<s:iterator value="listOfPeople" status="element" var="row">
    <s:if test='%{row.status.equalsIgnoreCase("Student") ||
                  row.status.equalsIgnoreCase("Teacher")}'>

或者干脆

<s:iterator value="listOfPeople" status="element">
    <s:if test='%{status.equalsIgnoreCase("Student") ||
                  status.equalsIgnoreCase("Teacher")}'>

奇怪的代码导致奇怪的结果...然后直接使用它:)

Strange code leads to weird results... then use it straight :)

这篇关于如何拥有&lt;s:if&gt;在 Struts 2 中有两个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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