如果…JSP或JSTL中的else
我想根据JSP文件中的某些条件输出一些HTML代码如果…JSP或JSTL中的else,jsp,if-statement,jstl,Jsp,If Statement,Jstl,我想根据JSP文件中的某些条件输出一些HTML代码 if(条件1){ 某些特定于条件1的HTML代码 } 否则,如果(条件2){ 某些特定于条件2的HTML代码 } 我该怎么做?我应该使用JSTL吗 我应该使用JSTL吗 对 您可以使用和标记使用JSTL在jsp中进行条件呈现 要模拟如果,可以使用: 要模拟if…else,可以使用: 披萨 比萨饼。 这方面的结构是: <c:choose> <c:when test="${..}">...</c:wh
if(条件1){
某些特定于条件1的HTML代码
}
否则,如果(条件2){
某些特定于条件2的HTML代码
}
我该怎么做?我应该使用JSTL吗
我应该使用JSTL吗
对
您可以使用
和
标记使用JSTL在jsp中进行条件呈现
要模拟如果,可以使用:
要模拟if…else,可以使用:
披萨
比萨饼。
这方面的结构是:
<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>
...
...
...
如果条件不昂贵,我有时更喜欢简单地使用两个不同的
,如果您只想输出不同的文本,一个更简洁的示例是
${condition ? "some text when true" : "some text when false"}
它比c:choose短得多。如果要比较字符串,请编写以下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>
<c:choose>
<c:when test="${myvar.equals('foo')}">
...
</c:when>
<c:when test="${myvar.equals('bar')}">
...
</c:when>
<c:otherwise>
...
</c:otherwise>
</c:choose>
...
...
...
值为5
值不是5
Html代码
Html代码
如果要使用JSTL标记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>`
1。请在页面顶部添加JSTL标记库`
`
2.请写下面的代码
`
以4开头的两位数
其他数字
`
您可以在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
<%
}
%>
用户名为空//html代码
用户名不为空//html代码
简单方法:
<c:if test="${condition}">
//if
</c:if>
<c:if test="${!condition}">
//else
</c:if>
//如果
//否则
如果要比较字符串,请编写以下JSTL:
...
...
...
从newuser中选择*,其中ID=?;
从newuser中选择*,其中username=?;
//如果部分
//其他部分
Thx为此…实际上我更喜欢UI开发…所以对JSTL没有太多的了解..你能为我提供一个类似的JSTL工作示例..我的意思是if..elseThx为示例..我的条件实际上是一个JS条件..if navigator.userAgent.match(/iPad/I)!=空,所以我可以直接在if test=“condition”中写入它。JSTL将在服务器端执行,它将从中提取HTML并将其传递给clientOk。这很好…但是对于实际情况,即在test=“condition”…我可以指定任何JS条件,如果是,我可以直接写吗?例如,我可以写吗?你可以用你想要测试的条件更新你的问题吗?我可以假设,如果第一个条件满足,那么第二个条件将不会执行吗?因为你可以从盒子中取出内容辅助,作为一个开始。而且因为webdevs可以使用它而不必再学习另一个-dsl@hop不,你不能,例如,Depot有一些很好的例子。查看>此javascript或.jsp是否可能重复?如果使用布尔变量作为条件,则不起作用?是否需要将其包含在特殊标记中,或者直接粘贴到HTML中?@otherDewi:这是JSTL,可以在jsp文件中使用。但是,条件运算符或三元运算符存在于许多其他语言中,包括Javascript。@bergie3000:不,这不是。这是。我认为只能在新的(ISH)容器上调用方法(如.AdALS),如Tomcat 7 +。它可以提供解决方案,但是请考虑添加一些代码细节。
<c:if test="${condition}">
//if
</c:if>
<c:if test="${!condition}">
//else
</c:if>
<c:choose>
<c:when test="${myvar.equals('foo')}">
...
</c:when>
<c:when test="${myvar.equals('bar')}">
...
</c:when>
<c:otherwise>
...
</c:otherwise>
</c:choose>
<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>
<c:if test="${any_cond}" var="condition">
//if part
</c:if>
<c:if test="${!condition}">
//else part
</c:if>