2011-02-16 32 views
0

我正在重構現有的web應用程序,以使用struts標記而不是scriptlet。發生多個事情的其中一種情況是有條件檢查,如struts邏輯標籤:只適用於form bean參數?

<%if ("true".equals(requset.getparameter("someParam"))) {%> 

some html, javascript 

<%else{%> 
some other html,javascript 
<%}%> 

我希望用struts邏輯標記替換它來替換scriptlet。沒有在formbean中存儲一些參數可以做到嗎?默認情況下,邏輯標籤的語法似乎只適用於formbean參數。

回答

1

雖然不是struts特定的解決方案:您是否想過使用JSTL自定義標籤?這些標籤包含在最新的Web容器規範中,並且可以輕鬆添加舊的Web容器,這些容器默認不包含JSTL規範。

這裏是一個解決方案基於JSTL:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> 
... 
<c:choose> 
    <c:when test="${param.someParam eq 'true'}"> 
    some html, javascript 
    </c:when> 

    <c:otherwise> 
    some other html, javascript 
    </c:otherwise> 
</c:choose> 

除非你使用Struts自定義標籤來生成UI組件,它總是傾向於儘可能使用JSTL標準標籤。不過,如果你是在用struts設置,這裏是使用Struts 1的例子:

<bean:parameter id="paramValue" name="someParam" /> 
<logic:equal name="paramValue" value="true"> 
    some html, javascript 
</logic:equal> 

<logic:notEqual name="paramValue" value="true"> 
    some other html, javascript 
</logic:notEqual> 

你會注意到,該解決方案採用的請求參數並投入到一個頁面作用域屬性(paramValue),然後可以由struts邏輯自定義標籤訪問。

+0

謝謝。如果「someParam」爲空(即參數不隨請求發送),則struts1解決方案將不起作用。有沒有解決的辦法? – Victor 2011-02-16 15:12:47

相關問題