Using an if statement to decided whether or not to show a table on a jsp page
I have a parameter of user that I am setting equal to -1 if this param is not given. I only want to show the table if the parameter is fulfilled ( for instance if user = 1 ).
- so using an "if" statement.....can I j开发者_StackOverflow中文版ust do something like this:
ex:
if(user >= 1){
<table>
<tr>
.....
</tr>
</table>
}
Is this possible, or how would you recommend doing this?
I would recommend you to use JSTL. <c:if>
tag, also to set the parameter as attribute in request
to use it on jsp
Request parameters are as a Map<String, String>
available by ${param}
in EL. So a request parameter with the name user
is available by ${param.user}
.
JSTL provides the core tags like <c:if>
to control the flow in the page.
So, 1 + 1 = ...
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:if test="${param.user > 0}">
<table>
...
</table>
</c:if>
<% if(user >= 1){ %>
some html
<% } %>
You shouldn't try to intermix Java scriptlets inside JSP. Use JSTL instead. You would have to import the core library <%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
and include jstl.jar standard.jar in your classpath which can be found on the Apache Site
精彩评论