【问题标题】:Accessing action instance variables and model-driven bean variable values in JSP在 JSP 中访问动作实例变量和模型驱动的 bean 变量值
【发布时间】:2012-07-09 14:20:06
【问题描述】:

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

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

searchKey也是PaymentResponseDTO中的一个变量。

我需要根据某些条件从动作类和模型驱动 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>

瓷砖 xml

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

b2c_paymentGatewayResponse.jsp 中存在隐藏域代码。

【问题讨论】:

  • 我似乎不明白你的问题。你有searchKey 作为你的动作类和模型驱动bean 中的变量吗?你有 searchKey 变量的 getter 和 setter。
  • @UchennaNwanyanwu.. 我有 searchKey 作为动作变量和模型驱动 bean,我有 searchKey 的 setter 和 getter
  • 你没有回答第一个问题。你有searchKey作为你的动作类和你的模型驱动bean中的变量吗?
  • @UchennaNwanyanwu... 是的。我在两者中都有 searchKey 作为变量..
  • 模型驱动中的 searchKey 优先,因为您的 Action 类实现了ModelDriven

标签: struts2 ognl


【解决方案1】:

当您的模型(在堆栈顶部)和您的操作(通常是模型下方的项目)具有相同名称的属性时,您可以使用 #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. -->

【讨论】:

  • 感谢您的回复。我不得不多次面对这个问题,但最终在动作类中重命名了该属性。我建议在Struts2 文档中提供。
  • @UchennaNwanyanwu 我在 S2 OGNL 页面上做了一个简短的记录,但暂时不会更新。
  • 如果我想在 h2 dir 属性中添加这个值。怎么会这样?例如

猜你喜欢
  • 2015-06-08
  • 1970-01-01
  • 2017-06-21
  • 2011-08-26
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
  • 2011-05-06
  • 1970-01-01
相关资源
最近更新 更多