ChatGPT解决这个技术问题 Extra ChatGPT

if...else 在 JSP 或 JSTL 中

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

if (condition 1) {
    Some HTML code specific for condition 1
}
else if (condition 2) {
    Some HTML code specific for condition 2
}

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

Exampledepot 有一些很好的例子。看看 > exampledepot.8waytrips.com/egs/javax.servlet.jsp.jstl.core/…

A
Andrea Ligios

我应该使用 JSTL 吗?

是的。

您可以使用 <c:if><c:choose> 标签在使用 JSTL 的 jsp 中进行条件渲染。

要模拟 if ,您可以使用:

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

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

<c:choose>
    <c:when test="${param.enter=='1'}">
        pizza. 
        <br />
    </c:when>    
    <c:otherwise>
        pizzas. 
        <br />
    </c:otherwise>
</c:choose>

谢谢...实际上我更喜欢 UI 开发...所以对 JSTL 了解不多..您能否提供一个类似的 JSTL 工作示例..我的意思是 if..else
举个例子,谢谢..我的条件实际上是一个 JS 条件..if navigator.userAgent.match(/iPad/i) != null 所以我可以直接在 if test="condition"..
JSTL 将在服务器端执行,它将从中生成 HTML 并将其传递给客户端
好的..很好...但是对于实际条件,即 test="condition" ...我可以指定任何 JS 条件吗?如果可以,我可以直接写吗,例如我可以写
你能用你想测试的条件更新你的问题吗
K
KIR

如果您只想输出不同的文本,更简洁的示例是

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

它比 c:choose 短得多。


这是 javascript 还是 .jsp?
使用布尔变量作为条件时不起作用有什么建议吗?
这需要包含在一个特殊的标签中,还是直接粘贴到 HTML 中?
@otherDewi:它是 JSTL,可以在 JSP 文件中使用。但是,条件或三元运算符存在于许多其他语言中,包括 Javascript。
@bergie3000:,这不是 JSTL。这是EL
B
Bozho

对此的构造是:

<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>

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


我可以假设如果第一个条件满足,那么第二个什么时候不会被执行?
因为您首先获得了开箱即用的内容辅助。并且因为 webdevs 可以在不学习又一个 dsl 的情况下使用它
@hop 不,你不能
a
aebersold

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

<c:choose>
    <c:when test="${myvar.equals('foo')}">
        ...
    </c:when>
    <c:when test="${myvar.equals('bar')}">
        ...
    </c:when>
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>

我认为你只能在新的(ish)容器上调用方法(比如 .equals),比如 Tomcat 7+。
H
Hiren Odedra
<%@ 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>

M
Milad Heidari

简单的方法:

<c:if test="${condition}">
    //if
</c:if>
<c:if test="${!condition}">
    //else
</c:if>

S
Shivam

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

例如:

   <%
        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
    <%
        }
    %>

保护你!这是我的问题的解决方案,但是这种语法令人作呕....但是再次感谢您!
a
aristotll
<%@ 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>

S
Sachin Kumar
<c:if test="${any_cond}" var="condition">
    //if part
</c:if>
<c:if test="${!condition}">
    //else part
</c:if>

S
Sachin Kumar
<c:if test="${condition1 && condition2 && condition3}" var="condition">
    //if
</c:if>
<c:if test="${!condition}">
    //else
</c:if>

这是在变量中采用 if 条件的另一种方法。


u
user3466950

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

【要求】如果一个数字大于等于40小于50则显示“以4开头的两位数”,否则显示“其他数字”。

[解决方案]

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>`

C
Codekie

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

<c:choose>
    <c:when test="${myvar.equals('foo')}">
        ...
    </c:when>
    <c:when test="${myvar.equals('bar')}">
        ...
    </c:when>
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>

A
Aman Kumar
<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>


它可能会提供解决方案,但请考虑使用代码添加一些细节