簡體   English   中英

使用JSTL訪問Struts2 Action

[英]Access Struts2 Action with JSTL

我正在使用常規Servlet將Java Web應用程序遷移到Struts2 Actions。

我似乎無法使用JSTL標記訪問JSP上的Action對象。 標簽在遷移之前正常工作,但現在它們只顯示變量名稱。 但是,我可以使用Struts2標簽訪問該對象,所以我不確定JSTL的問題是什么。

我的動作有以下對象:

private String prueba = "hola";

public String getPrueba() {
    return prueba;
}

public void setPrueba(String prueba) {
    this.prueba = prueba;
}

在JPS上:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

此標記在屏幕上顯示“$ {prueba}”。

<c:out value="${prueba}" />

雖然這個標簽正確顯示“hola”。

<s:property value="prueba"/>

我的類路徑上有最新版本的JSTL庫,控制台上沒有顯示錯誤,所以我不確定是什么問題。 我是否需要在struts.xml,web.xml或我的Action類上配置其他內容?

提前致謝。

更新:

我的struts.xml

<struts><!-- Configuration for the default package. -->

    <package name="default" namespace="/" extends="struts-default">

        <action name="listaAutos" 
            class="com.neoris.training.lab.autos.negocios.AutomovilServlet"
            method="service">
            <result name="success">/listaautos.jsp</result>
        </action>

    </package>
</struts>

我的web.xml

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

    <web-app>
      <display-name>Team1PrjStruts</display-name>
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
      </filter>

      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    </web-app>

更新

您還可以確保在類路徑中有jstl-xx.jar嗎?

為什么我不能在Struts標簽中使用JSTL樣式的EL表達式?

從Struts版本2.0.9開始,對於評估OGNL的Struts標記屬性,已禁用JSTL / JSP表達式語言(EL)。 這是針對安全漏洞的預防措施,這些漏洞可能是由於首次將屬性作為JSTL / JSP EL表達式處理然后將結果作為OGNL表達式處理時發生的雙重評估所導致的。 解決方案是直接使用OGNL表達式表示Struts標記中的所有動態屬性值。

http://struts.apache.org/release/2.3.x/docs/using-struts-and-xwork-with-jsp-20-and-jstl-11.html

但你可以試試這個:

http://struts.apache.org/release/2.3.x/docs/using-struts-and-xwork-with-jsp-20-and-jstl-11.html

嘗試<c:out value="${action.prueba}" />或只需${action.prueba}

根據所有Struts2請求都被重新StrutsRequestWrapperStrutsRequestWrapper中的StrutsRequestWrapper (並且getAttribute方法在那里重載使用ValueStack來搜索變量/屬性,如果使用普通的JSTL EL方式找不到的話) - 在JSP中使用${prueba}應該工作非常好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM