if ... else在JSP或JSTL中 [英] if...else within JSP or JSTL
问题描述
我想有一个基于条件(桌面/ ipad)的HTML代码.. say Condition 1 /条件2
我希望为这些条件中的每一个分别设置HTML片段...
(条件1)
con1的一些HTML代码
else if(条件2)
con2的一些HTML代码
我想测试的条件(在JS中)是;
var isiPad = navigator.userAgent.match(/ iPad / i)!= null;
if(isiPad)
{}
else
{}
现在必须在.jsp页面中实现...
那我该怎么做?我应该使用JSTL吗?
最好的方法是什么?
最重要的是,只有相应的代码应该实际加载/呈现
例如如果条件1为真,条件2中的HTML代码根本不应该被执行(除了隐藏在浏览器中)之外
我应该使用JSTL吗?
是。
您可以使用< ; c:如果>
和< c:choose>
标签,使用JSTL在jsp中进行条件呈现。
要模拟 if ,您可以使用:
< c:if test =condition>< / c:if>
要模拟 if ... else ,您可以使用:
< c:choose>
< c:when test =$ {param.enter =='1'}>
披萨。
< br />
< / c:当>
< c:其他>
比萨饼。
< br />
< / c:否则>
< / c:选择>
I have a kind of open-ended question..
I want to have a HTML code based on condition (desktop/ipad)..say Condition 1/Condition 2
I want to have separate HTML snippets for each of these conditions...
if (Condition 1)
Some HTML code for con1
else if (Condition 2)
Some HTML code for con2
The condition I want to test (in JS) is;
var isiPad = navigator.userAgent.match(/iPad/i) != null;
if (isiPad)
{}
else
{}
Now this has to be implemented in a .jsp page...
So how do I do that? Should I use JSTL ? What is the best way?
The main thing is that only corresponding code should actually be loaded/rendered e.g. if condition 1 is true, the HTML code in condition 2 should not be executed at all (apart from being hidden in the browser)
Should I use JSTL ?
Yes.
You can use <c:if>
and <c:choose>
tags to make conditional rendering in jsp using JSTL.
To simulate if , you can use:
<c:if test="condition"></c:if>
To simulate if...else, you can use:
<c:choose>
<c:when test="${param.enter=='1'}">
pizza.
<br />
</c:when>
<c:otherwise>
pizzas.
<br />
</c:otherwise>
</c:choose>
这篇关于if ... else在JSP或JSTL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!