4
votes

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.

1
i don't seem to understand your question. do you have searchKey both as a variable in your action class and model-driven bean. Do you have getters and setters for the searchKey variable.Uchenna Nwanyanwu
@UchennaNwanyanwu.. I have searchKey as variable in action and model-driven bean, and i have setter and getters for searchKeyDaya
you didn't answer the first question. do you have searchKey as a variable in your action class and also in your model driven bean.Uchenna Nwanyanwu
@UchennaNwanyanwu... yeah. I have searchKey as a variable in both..Daya
the searchKey in your Model driven takes precedence since your Action class implements ModelDriven.Uchenna Nwanyanwu

1 Answers

11
votes

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. -->