tes*_*dtv 277 jsp if-statement jstl
我有一个开放式的问题..
我想要一个基于条件(桌面/ ipad)的HTML代码..说条件1 /条件2
我希望为每个条件都有单独的HTML代码段...
if (condition 1) {
Some HTML code specific for condition 1
}
else if (condition 2) {
Some HTML code specific for condition 2
}
Run Code Online (Sandbox Code Playgroud)
我想测试的条件(在JS中)是;
if (condition 1) {
Some HTML code specific for condition 1
}
else if (condition 2) {
Some HTML code specific for condition 2
}
Run Code Online (Sandbox Code Playgroud)
现在必须在.jsp页面中实现...
那我该怎么做?我应该使用JSTL吗?什么是最好的方法?
主要的是只有实际加载/呈现相应的代码,例如,如果条件1为真,则条件2中的HTML代码根本不应该执行(除了隐藏在浏览器中)
Jig*_*shi 516
我应该使用JSTL吗?
是.
您可以使用<c:if>
和<c:choose>
标签在jsp中使用JSTL进行条件渲染.
要模拟是否,您可以使用:
<c:if test="condition"></c:if>
Run Code Online (Sandbox Code Playgroud)
要模拟if ... else,你可以使用:
<c:choose>
<c:when test="${param.enter=='1'}">
pizza.
<br />
</c:when>
<c:otherwise>
pizzas.
<br />
</c:otherwise>
</c:choose>
Run Code Online (Sandbox Code Playgroud)
KIR*_*KIR 170
如果您只想输出不同的文本,则会有一个更简洁的示例
${condition ? "some text when true" : "some text when false"}
Run Code Online (Sandbox Code Playgroud)
它比c更短:选择.
Boz*_*zho 102
这个结构是:
<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>
Run Code Online (Sandbox Code Playgroud)
如果条件不贵,我有时更喜欢简单地使用两个不同的<c:if
标签 - 它使阅读更容易.
Hir*_*dra 11
<%@ 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>
Run Code Online (Sandbox Code Playgroud)
aeb*_*old 10
如果您想比较字符串,请编写以下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>
Run Code Online (Sandbox Code Playgroud)
你可以在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
<%
}
%>
Run Code Online (Sandbox Code Playgroud)
简单方法:
<c:if test="${condition}">
//if
</c:if>
<c:if test="${!condition}">
//else
</c:if>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
772275 次 |
最近记录: |