1

In my jsp file I set the value of the variable statusNum. When I am trying to pass the value of statusNum to the method getStatusString() (this method is defined in Action class) the value of statusNum is null. But when I displayed using ${statusNum} I am getting the value. Here is my code.

<table style="width: 100%;">   
 <s:iterator value="emailList">
   <tr>
     <td style="width:39%"><s:property value='name'/></td>
     <td style="width:39%;"><s:property value='email'/></td>
     <s:set var="statusNum"><s:property value="status"/></s:set>
      ${statusNum} 
     <s:set var="stringNum" value="%{getStatusString(#statusNum)}"/>
  </tr>
 </s:iterator>
</table>

here is skeleton of getStatusString() method in Action class

public String getStatusString(Integer sendStatus) {
      ------------- 
      ------------- //code returning string value
      -------------         
}

Please help me to get the status String value corresponding to the statusNum through the method getStatusString().

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
rajani chowdhary
  • 175
  • 1
  • 4
  • 16

2 Answers2

1

You should try with the <s:property value="%{getStatusString(#statusNum)}"/> tag in order to call an action method.

Anwyay... In my opinion this is not a good choice... Why you should call an action method inside a jsp and mix the business layer with the presentation layer?

It's easier to move everything inside your controller and use the jsp just to render the results.

I mean... You're slicing a single operation in two different stages. Your action is creating a Collection called emailList then you iterate your collection in the jsp. While you're iterating you call a method to get a status string.


Workflow: Action -> jsp -> iterate -> {method x n} -> result


Why don't you prepare that status string inside your action? In this way you will just print the value and your stack won't be full of "garbage" (if we can say so).


Workflow: Action -> jsp -> iterate -> result


<table style="width: 100%;">   
  <s:iterator value="emailList">
     <tr>
       <td style="width:39%"><s:property value='name'/></td>
       <td style="width:39%;"><s:property value='email'/></td>
       <td style="width:39%;"><s:property value='statusString'/></td>
     </tr>
  </s:iterator>
</table>

If that string may change over time, then use Ajax. in order to keep your page always refreshed and in a much more secure way.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
IlGala
  • 3,331
  • 4
  • 35
  • 49
  • emailList is of the type List EmailList . then calling the method of EmailBizObj. So shall I prepare statusString method in EmalBizObj class so that I can get statusNum to prepare statusString for that statusNum – rajani chowdhary Dec 14 '15 at 09:53
1

I'm not sure why it doesn't work, but since it's a number, you should do

<s:set var="statusNum" value="status"/>

instead of

<s:set var="statusNum"><s:property value="status"/></s:set>

that is using it as HTML and hence as String.

IlGala is right, you should do it on the controller, or on the emailList bean, but you could also use another trick as described in this answer:

Iterate the emailList objects and call an action method without passing any parameters:

<s:iterator value="emailList">
  <tr>
     <td> <s:property value='name'/>                    </td>
     <td> <s:property value='email'/>                   </td>
     <td> <s:property value="status"/>                  </td>
     <td> <s:property value="statusStringByStatusNum"/> </td>
  </tr>
</s:iterator>

then in the action method retrieve the currently iterated object from the Value Stack, and get the parameter you need from there:

public String getStatusStringByStatusNum(){
    EmailBizObj currentlyIteratedObj = 
                             (EmailBizObj) ActionContext.getContext().getValueStack().peek();
    Integer sendStatus = currentlyIteratedObj.getStatus();
    return ...; // do here what you need with your sendStatus and your Email object.
}
Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243