2012-11-12 102 views
0

我想比較一個會話變量與struts if條件的測試方法中的屬性值。struts 2中的屬性值比較

<s:property value="band"/> 
<s:if test="#session.bandname == property value"> 
    <s:form action="BandPost"> 
     <s:textarea key="post" accesskey="post">Write a post</s:textarea> 
     <s:submit/> 
    </s:form> 
</s:if> 

我想比較上述屬性值if條件與#session.bandname值。我嘗試了很多東西,但他們不工作。有人能給我答案嗎?

謝謝。

+0

如果bandname是一個字符串,則使用String方法(例如'equals')... ie:test =「#session.bandname.equals(propertyValue)」。 – Quaternion

+0

我想在測試條件下得到「」值。 – grahesh

回答

1

您可以簡單地引用該屬性。您只需在您的操作中使用公用字符串getBand()方法。

<s:property value="band"/> 
<s:if test="%{#session.bandname == band}"> 
    <s:form action="BandPost"> 
     <s:textarea key="post" accesskey="post">Write a post</s:textarea> 
     <s:submit/> 
    </s:form> 
</s:if> 
+0

非常感謝。 – grahesh

0

我認爲core JSTL可以很容易地解決這個問題。

<c:set var="varForComparison" value="ABC" scope="page"></c:set> 

<c:if test="${ sessionScope.varInSession eq pageScope.varForComparison }"> 
// DO WHAT YOU HAVE TO 
</c:if> 

,你甚至可以要求直接這樣

<c:if test="${ sessionScope.varInSession eq requestScope.beanInRequest.propertyToCompare }"> 
// DO WHAT YOU HAVE TO 
</c:if> 

另外如果你想多個條件類似的if-else梯,你可以使用這個

<c:choose> 
    <c:when test=""></c:when> 
    <c:otherwise></c:otherwise> 
</c:choose> 

在C比較豆:選擇,你可以使用盡可能多的c:當你想要的時候。核心JSTL +表達式語言幫助了很多。 希望它有幫助。