Voglio generare un codice HTML basato su una condizione in un file JSP.
if (condition 1) {
Some HTML code specific for condition 1
}
else if (condition 2) {
Some HTML code specific for condition 2
}
Come lo posso fare? Dovrei usare JSTL?
Voglio generare un codice HTML basato su una condizione in un file JSP.
if (condition 1) {
Some HTML code specific for condition 1
}
else if (condition 2) {
Some HTML code specific for condition 2
}
Come lo posso fare? Dovrei usare JSTL?
Risposte:
Dovrei usare JSTL?
Sì.
Puoi usare tag <c:if>e <c:choose>per rendere il rendering condizionale in jsp usando JSTL.
Per simulare se , è possibile utilizzare:
<c:if test="condition"></c:if>
Per simulare se ... altro , puoi usare:
<c:choose>
<c:when test="${param.enter=='1'}">
pizza.
<br />
</c:when>
<c:otherwise>
pizzas.
<br />
</c:otherwise>
</c:choose>
Se vuoi solo produrre un testo diverso, un esempio più conciso sarebbe
${condition ? "some text when true" : "some text when false"}
È molto più breve di c: scegli .
Il costrutto per questo è:
<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>
Se la condizione non è costosa, a volte preferisco semplicemente utilizzare due <c:iftag distinti : facilita la lettura.
<%@ 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>
Nel caso in cui si desideri confrontare stringhe , scrivere il seguente 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>
modo semplice:
<c:if test="${condition}">
//if
</c:if>
<c:if test="${!condition}">
//else
</c:if>
<%@ 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:if test="${any_cond}" var="condition">
//if part
</c:if>
<c:if test="${!condition}">
//else part
</c:if>
Se si desidera eseguire le seguenti operazioni utilizzando JSTL Tag Libe, attenersi alla seguente procedura:
[Requisito] se un numero è maggiore di 40 e inferiore a 50, quindi visualizzare "Numero a due cifre che inizia con 4", altrimenti "Altri numeri".
[Soluzioni]
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>`
Puoi scrivere la condizione if-else all'interno delle <% %>pagine jsp e del codice html all'esterno di<% %>
Per esempio:
<%
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
<%
}
%>
Nel caso in cui si desideri confrontare stringhe, scrivere il seguente 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="${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>
Ho avuto lo stesso problema e ho pensato che non fosse possibile utilizzare Javascript in un JSTL se condizione.
Un tipo di soluzione alternativa potrebbe essere in 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>
Dovrai mettere questo script dove dovrebbe essere il codice html che vuoi scrivere.