问题
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:
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. -->
来源:https://stackoverflow.com/questions/11407686/accessing-action-instance-variables-and-model-driven-bean-variable-values-in-jsp