我想根据jsp文件中的某些条件输出一些html代码。
if (condition 1) { Some HTML code specific for condition 1}else if (condition 2) { Some HTML code specific for condition 2}
if (condition 1) {
Some HTML code specific for condition 1
}
else if (condition 2) {
Some HTML code specific for condition 2
我该怎么做?我应该使用jstl吗?
yzckvree1#
如果要比较字符串,请编写以下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:otherwise>
</c:otherwise>
</c:choose>
ibps3vxo2#
ekqde3dh3#
其结构是:
<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:when test="${..}">...</c:when> <!-- if condition -->
<c:when test="${..}">...</c:when> <!-- else if condition -->
<c:otherwise>...</c:otherwise> <!-- else condition -->
如果条件不贵,我有时更喜欢简单地使用两个不同的条件 <c:if 标签-使它更容易阅读。
<c:if
flmtquvp4#
简单方法:
<c:if test="${condition}"> //if</c:if><c:if test="${!condition}"> //else</c:if>
<c:if test="${condition}">
//if
</c:if>
<c:if test="${!condition}">
//else
xqk2d5yq5#
我应该使用jstl吗?对。你可以用 <c:if> 以及 <c:choose> 使用jstl在jsp中进行条件呈现的标记。要模拟是否,可以使用:
<c: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>
<c:when test="${param.enter=='1'}">
pizza.
<br />
pizzas.
ilmyapht6#
<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: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:otherwise >
SELECT * FROM newuser WHERE username = ?;
<sql:param value="${param.username}" />
7qhs6swi7#
如果要使用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>`
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:when test="${params.number >=40 && params.number <50}">
<p> Two digit number starting with 4. </p>
<p> Other numbers. </p>
</c:choose>`
mpbci0fu8#
<%@ 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>
<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %>
<c:set var="val" value="5"/>
<c:when test="${val == '5'}">
Value is 5
Value is not 5
14ifxucb9#
<%@ 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>
<c:set var="isiPad" value="value"/>
<!-- if condition -->
<c:when test="${...}">Html Code</c:when>
<!-- else condition -->
<c:otherwise>Html code</c:otherwise>
dy1byipe10#
你可以在里面写条件 <% %> 在jsp页面和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 <% } %>
<%
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
pcww981p11#
我也遇到了同样的问题,我认为在jstlif条件下不能使用javascript。解决方法可以是javascript:
<script> 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"); }</script>
<script>
var isiPad = navigator.userAgent.match(/iPad/i) != null;
if (isiPad) {
document.write("Some HTML code for con1");
document.write("Some HTML code for con2");
</script>
你必须把这个脚本放在你想写的html代码应该放的地方。
ruarlubt12#
如果您只想输出不同的文本,可以使用更简洁的示例
${condition ? "some text when true" : "some text when false"}
它比c:choose短得多。
3hvapo4f13#
<c:if test="${any_cond}" var="condition"> //if part</c:if><c:if test="${!condition}"> //else part</c:if>
<c:if test="${any_cond}" var="condition">
//if part
//else part
13条答案
按热度按时间yzckvree1#
如果要比较字符串,请编写以下jstl:
ibps3vxo2#
如果要比较字符串,请编写以下jstl:
ekqde3dh3#
其结构是:
如果条件不贵,我有时更喜欢简单地使用两个不同的条件
<c:if
标签-使它更容易阅读。flmtquvp4#
简单方法:
xqk2d5yq5#
我应该使用jstl吗?
对。
你可以用
<c:if>
以及<c:choose>
使用jstl在jsp中进行条件呈现的标记。要模拟是否,可以使用:
要模拟if…else,可以使用:
ilmyapht6#
7qhs6swi7#
如果要使用jstl tag libe执行以下操作,请执行以下步骤:
[要求]如果数字大于等于40小于50,则显示“从4开始的两位数”,否则显示“其他数字”。
[解决方案]
mpbci0fu8#
14ifxucb9#
dy1byipe10#
你可以在里面写条件
<% %>
在jsp页面和html代码之外<% %>
例如:pcww981p11#
我也遇到了同样的问题,我认为在jstlif条件下不能使用javascript。
解决方法可以是javascript:
你必须把这个脚本放在你想写的html代码应该放的地方。
ruarlubt12#
如果您只想输出不同的文本,可以使用更简洁的示例
它比c:choose短得多。
3hvapo4f13#