关于if语句:if还是JSP或JSTL中的else

if…else within JSP or JSTL

我想根据JSP文件中的某些条件输出一些HTML代码。

1
2
3
4
5
6
if (condition 1) {
    Some HTML code specific for condition 1
}
else if (condition 2) {
    Some HTML code specific for condition 2
}

我怎样才能做到这一点? 我应该使用JSTL吗?


Should I use JSTL ?

是。

您可以使用标记在jsp中使用JSTL进行条件渲染。

要模拟是否,您可以使用:

1
<c:if test="condition"></c:if>

要模拟if ... else,你可以使用:

1
2
3
4
5
6
7
8
9
10
<c:choose>
    <c:when test="${param.enter=='1'}">
        pizza.
        <br />
    </c:when>    
    <c:otherwise>
        pizzas.
        <br />
    </c:otherwise>
</c:choose>


如果您只想输出不同的文本,则会有一个更简洁的示例

1
${condition ?"some text when true" :"some text when false"}

它比c更短:选择。


这个结构是:

1
2
3
4
5
<c:choose>
   <c:when test="${..}">...</c:when> <!-- if condition -->
   <c:when test="${..}">...</c:when> <!-- else if condition -->
   <c:otherwise>...</c:otherwise>    <!-- else condition -->
</c:choose>

如果条件不昂贵,我有时更喜欢简单地使用两个不同的标签 - 它使它更容易阅读。


1
2
3
4
5
6
7
8
9
10
<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %>
<c:set var="val" value="5"/>
<c:choose>
  <c:when test="${val == '5'}">
    Value is 5
  </c:when>
  <c:otherwise>
    Value is not 5
  </c:otherwise>
</c:choose>

如果您想比较字符串,请编写以下JSTL:

1
2
3
4
5
6
7
8
9
10
11
<c:choose>
    <c:when test="${myvar.equals('foo')}">
        ...
    </c:when>
    <c:when test="${myvar.equals('bar')}">
        ...
    </c:when>
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>


简单方法:

1
2
3
4
5
6
<c:if test="${condition}">
    //if
</c:if>
<c:if test="${!condition}">
    //else
</c:if>


1
2
3
4
5
6
7
8
<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %>
<c:set var="isiPad" value="value"/>
<c:choose>
   <!-- if condition -->
   <c:when test="${...}">Html Code</c:when>
   <!-- else condition -->
   <c:otherwise>Html code</c:otherwise>  
</c:choose>

1
2
3
4
5
6
7
8
9
10
11
12
13
<c:choose>
<c:when test="${not empty userid and userid ne null}">
      <sql:query dataSource="${dbsource}" var="usersql">
                SELECT * FROM newuser WHERE ID = ?;
                <sql:param value="${param.userid}" />
      </sql:query>
 </c:when>
 <c:otherwise >
       <sql:query dataSource="${dbsource}" var="usersql">
                 SELECT * FROM newuser WHERE username = ?;
                 <sql:param value="${param.username}" />
       </sql:query>                              
  </c:otherwise>


如果您想比较字符串,请编写以下JSTL:

1
2
3
4
5
6
7
8
9
10
11
<c:choose>
    <c:when test="${myvar.equals('foo')}">
        ...
    </c:when>
    <c:when test="${myvar.equals('bar')}">
        ...
    </c:when>
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>


我有同样的问题,我想你不能在JSTL中使用Javascript条件。

一种解决方法可以在Javascript中:

1
2
3
4
5
6
7
    var isiPad = navigator.userAgent.match(/iPad/i) != null;

    if (isiPad) {
        document.write("Some HTML code for con1");
    } else {
        document.write("Some HTML code for con2");
    }

您必须将此脚本放在您要编写的html代码的位置。


您可以在jsp页面中的<% %>内部编写if-else条件,在<% %>之外编写html代码

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   <%
        String username = (String)session.getAttribute("username");
        if(username==null)  {
    %>            
        <p>
 username is null
</p> //html code
    <%
        } else {
    %>
        <p>
 username is not null
</p> //html code
    <%
        }
    %>


如果您想使用JSTL Tag Libe执行以下操作,请按照以下步骤操作:

[要求]如果数字大于等于40且小于50,则显示"以4开头的两位数",否则显示"其他数字"。

[解决方案]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1. Please Add the JSTL tag lib on the top of the page.`
     <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>`

2. Please Write the following code
`
<c:choose>
       <c:when test="${params.number >=40 && params.number <50}">
              <p>
 Two digit number starting with 4.
</p>
       </c:when>
       <c:otherwise>
              <p>
 Other numbers.
</p>
       </c:otherwise>
  </c:choose>`