在JSP中访问操作实例变量和模型驱动的bean变量值

时间:2012-07-10 06:20:33

标签: struts2 ognl

我将searchKey作为动作类和模型驱动的bean对象中的变量。

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

searchKey也是PaymentResponseDTO中的变量。

我需要根据某些条件从动作类和modeldriven bean访问searchKey。拥有相同名称的变量是不好的。但上面已经开发出来了。如果我在Java文件中进行任何修改,我需要做很多很难的修改。

现在我需要访问动作类变量。我尝试以下面的方式从动作类中访问变量:

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

tile xml

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

b2c_paymentGatewayResponse.jsp中,隐藏的字段代码存在。

1 个答案:

答案 0 :(得分:11)

当您的模型(在堆栈顶部)和您的操作(通常是模型下面的项目)具有相同名称的属性时,您可以使用#action值堆栈上下文变量或直接使用歧义消除歧义访问堆栈(坏主意)。

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