Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing action instance variables and model-driven bean variable values in JSP

Tags:

struts2

ognl

I have searchKey as a variable in action class and model-driven bean object.

public class PaymentGateWayAction extends ActionSupport implements ModelDriven<PaymentResponseDTO> {
    private String searchKey;
    private PaymentResponseDTO paymentResponseDTO = new PaymentResponseDTO();
    // ...
}

searchKey is also a variable in PaymentResponseDTO.

I need to access searchKey from the action class and modeldriven bean based on some conditions. Having varible with same name is bad. But The above one is already developed. If I do any modification in Java file, I need to do many modifications which are difficult.

Now I need to access the action class variable. I tried to access the variable from action class in the follwing way:

<s:hidden id="searchKey" name="searchKey" value="%{searchKey}"/>

But it is returning null values.

I have below code also:

this.setSearchKey("somevarible");

Please suggest where the wrong is going on

struts.xml

<action name="atomResponse" class="com.PaymentGateWayAction" method="atomPaymentResponse">
  <result name="success" type="tiles">paymentGateWayResponse</result>
    <result name="failure" type="tiles">paymentGateWayResponseError</result>
  </action>

tiles xml

<definition name="paymentGateWayResponse" extends="b2cHome">
    <put-attribute name="body" value="agent_b2c/b2c_paymentGateWayResponse.jsp" />
</definition>

In b2c_paymentGatewayResponse.jsp the hidden field code is present.

like image 201
Daya Avatar asked Dec 21 '22 19:12

Daya


1 Answers

When both your model (on top of the stack) and your action (generally the item below the model) have properties of the same name you can disambiguate using either the #action value stack context variable, or by directly accessing the stack (bad idea).

<!-- Access action properties directly: -->
<s:property value="%{searchKey}" />          <!-- Model; top of stack.       -->
<s:property value="%{#action.searchKey}" />  <!-- Action; accessed directly. -->

<!-- Hope the stack never changes: -->
<s:property value="%{[0].searchKey}" />  <!-- Model;  top of stack.   -->
<s:property value="%{[1].searchKey}" />  <!-- Action; next stack pos. -->
like image 76
Dave Newton Avatar answered May 10 '23 14:05

Dave Newton