如何在Struts2中具有两个条件的if [英] How to have a if with two conditions in Struts2

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

问题描述

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

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},结果将是

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. 属性中的首字母大写是WRONG,因为如果变量名为foo,而吸气剂是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,请将其连同getter和setter一起更改为private Object status;,并在页面中使用:

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")}'>

或带有变量

<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 :)

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

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