【问题标题】:Access action getters in the JSP在 JSP 中访问 action getter
【发布时间】:2013-03-02 02:01:52
【问题描述】:

我是 Struts 的新手,我想在 JSP 中访问 Action 类中的一些值。如何访问这些值?

public static String[] getSoldItemsColNames() {
    return SellerHistory.soldItemsColNames;
}

public ItemType[] getActiveItems() {
    return sellerHistory.getActiveItems();
}

这就是我正在尝试的方式。

<s:iterator value="soldItemsColNames" status="soldItemsColNames" var="element"> 
<th><s:property value="%{#element}" /></th> 
</s:iterator> 

【问题讨论】:

  • 您面临的问题是什么?

标签: java jsp web-applications struts2 jsp-tags


【解决方案1】:

这是我的程序 - 完美运行:

 Action class : 
 static Account[] account=new Account[1];
 static String[] arrayofStrings={"1","2","4"};
 {
    Account a=new Account();
    a.setName(arrayofStrings);
    account[0]=a;

 } 

 //..getters and setters , execute()

  public String[] getArrayofStrings() {
    return this.account[0].getName();//Just like yours
  }

JSP 逻辑

  <s:iterator value="arrayofStrings" status="true" var="element">
  <p><s:property value="%{#element}" /></p>
  </s:iterator>

【讨论】:

  • 我不知道为什么,但在我的情况下,一旦我删除了 static 关键字,它就会起作用。好笑。
  • @ArunAbraham 这是因为通过 OGNL 访问静态方法存在安全风险(考虑到 OGNL 用于评估请求参数,并且可以静态访问敏感的系统资源)。因此,默认情况下,引用静态方法在 Struts2 中不起作用。但是,您可以通过配置设置将其打开,但由于存在安全风险,通过实例变量而不是直接访问静态资源要好得多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多