在 JSP 中访问动作实例变量和模型驱动的 bean 变量值 [英] Accessing action instance variables and model-driven bean variable values in JSP
问题描述
我将 searchKey
作为动作类和模型驱动 bean 对象中的变量.
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
也是 PaymentResponseDTO
中的一个变量.
searchKey
is also a variable in PaymentResponseDTO
.
我需要根据某些条件从操作类和模型驱动 bean 访问 searchKey
.具有相同名称的变量是不好的.但是上面的已经开发好了.如果我对Java文件做任何修改,我需要做很多困难的修改.
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}"/>
但它返回空值.
我也有以下代码:
this.setSearchKey("somevarible");
请指出哪里出错了
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
<definition name="paymentGateWayResponse" extends="b2cHome">
<put-attribute name="body" value="agent_b2c/b2c_paymentGateWayResponse.jsp" />
</definition>
在 b2c_paymentGatewayResponse.jsp
中存在隐藏域代码.
In b2c_paymentGatewayResponse.jsp
the hidden field code is present.
推荐答案
当您的模型(位于堆栈顶部)和您的操作(通常是模型下方的项目)具有相同名称的属性时,您可以使用以下任一方法消除歧义#action
值堆栈上下文变量,或通过直接访问堆栈(坏主意).
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. -->
这篇关于在 JSP 中访问动作实例变量和模型驱动的 bean 变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!