if…else在jsp或jstl中

xjreopfe  于 2021-06-29  发布在  Java
关注(0)|答案(13)|浏览(528)

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

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

我该怎么做?我应该使用jstl吗?

yzckvree

yzckvree1#

如果要比较字符串,请编写以下jstl:

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

ibps3vxo2#

如果要比较字符串,请编写以下jstl:

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

ekqde3dh3#

其结构是:

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

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

flmtquvp

flmtquvp4#

简单方法:

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

xqk2d5yq5#

我应该使用jstl吗?
对。
你可以用 <c:if> 以及 <c:choose> 使用jstl在jsp中进行条件呈现的标记。
要模拟是否,可以使用:

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

要模拟if…else,可以使用:

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

ilmyapht6#

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

7qhs6swi7#

如果要使用jstl tag libe执行以下操作,请执行以下步骤:
[要求]如果数字大于等于40小于50,则显示“从4开始的两位数”,否则显示“其他数字”。
[解决方案]

  1. 1. Please Add the JSTL tag lib on the top of the page.`
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>`
  3. 2. Please Write the following code
  4. `
  5. <c:choose>
  6. <c:when test="${params.number >=40 && params.number <50}">
  7. <p> Two digit number starting with 4. </p>
  8. </c:when>
  9. <c:otherwise>
  10. <p> Other numbers. </p>
  11. </c:otherwise>
  12. </c:choose>`
mpbci0fu

mpbci0fu8#

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

14ifxucb9#

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

dy1byipe10#

你可以在里面写条件 <% %> 在jsp页面和html代码之外 <% %> 例如:

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

pcww981p11#

我也遇到了同样的问题,我认为在jstlif条件下不能使用javascript。
解决方法可以是javascript:

  1. <script>
  2. var isiPad = navigator.userAgent.match(/iPad/i) != null;
  3. if (isiPad) {
  4. document.write("Some HTML code for con1");
  5. } else {
  6. document.write("Some HTML code for con2");
  7. }
  8. </script>

你必须把这个脚本放在你想写的html代码应该放的地方。

ruarlubt

ruarlubt12#

如果您只想输出不同的文本,可以使用更简洁的示例

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

它比c:choose短得多。

3hvapo4f

3hvapo4f13#

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

相关问题