比较struts 2中的属性值

时间:2012-11-12 07:11:29

标签: struts2

我想在struts的测试方法中将会话变量与属性值进行比较。如果条件。

<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值进行比较。我尝试了很多东西,但他们没有工作。有人能给我答案吗?

感谢。

2 个答案:

答案 0 :(得分:1)

您可以简单地引用该属性。您只需要在Action中使用 public String 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>

答案 1 :(得分: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>

您甚至可以像这样直接比较请求中的bean

<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 +表达语言有很多帮助。    希望它有所帮助。

相关问题