Access to action instance variables and model-driven bean variables in JSP

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

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

searchKey also a variable in PaymentResponseDTO .

I need to access searchKey from class action and modeldriven bean based on some conditions. Misuse of the same name. But the above has already been developed. If I make any changes to the Java file, I need to make many changes that are complex.

Now I need to access the action class variable. I tried to access the variable from the action class as follows:

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

But it returns null values.

I have the code below:

 this.setSearchKey("somevarible"); 

Please indicate where the wrong thing is happening.

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> 

xml tiles

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

In b2c_paymentGatewayResponse.jsp there is a hidden field code.

+4
source share
1 answer

When your model (on top of the stack) and your action (usually an element under the model) have properties with the same name, you can eliminate the ambiguity using either the context variable of the #action stack 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. --> 
+10
source

Source: https://habr.com/ru/post/953410/


All Articles