jsp - How to have a if with two conditions in Struts2 -


i iterate through list of items, , need show specific dropdown list if state of element equal student or teacher. following code shows fields not show dropdown of 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> 

in controller:

    peoplemodel peoplemodel = new peoplemodel();     listofpeople = peoplemodel.findpeople();     system.out.println("size is:" + listofpeople.size());  //returns 8     return "listpeople"; 

my model:

    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 ::: " + people.size());   //returns 8             return people;          } catch (exception e) {             tx.rollback();             e.printstacktrace();         }     } {         hibernateutil.closesession();     }     return null; } 

result of following code blew

   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 showing first person's status separate other ones thats why last value : [] not showing status.

if take out status: ${status} result

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

put %{} around whole expression, not in middle in other attributes of other tags:

also use more appropriate equality function strings, like described here

<s:if test='%{elements[#element.index].status.equalsignorecase("student") ||               elements[#element.index].status.equalsignorecase("teacher")}'> 

edit: dude, doing lot of odd things;

  1. ${status} jsp el, have no need of using it;
  2. you iterating source, , checking source: printing <s:property value="elements[%{#element.index}].status" /> gives empty result, , can't see elements thing anywhere in code;
  3. the capital letter first in attribute wrong, because if variable named foo , getter getfoo(), in page have .foo, not .foo. if variable named foo, against specs / best practices, let's start variables names lowercase letter.

then if have private object status, change private object status;, along getter , setter, , in page use:

<s:iterator value="listofpeople" status="element">     <s:if test='%{listofpeople[#element.index].status.equalsignorecase("student") ||                   listofpeople[#element.index].status.equalsignorecase("teacher")}'> 

or var

<s:iterator value="listofpeople" status="element" var="row">     <s:if test='%{row.status.equalsignorecase("student") ||                   row.status.equalsignorecase("teacher")}'> 

or simply

<s:iterator value="listofpeople" status="element">     <s:if test='%{status.equalsignorecase("student") ||                   status.equalsignorecase("teacher")}'> 

strange code leads weird results... use straight :)


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -