JSTL中的switch case语句:choose when:
示例一:
<c:choose>
<c:when test="${param.num < 10}">
num 小于 10
</c:when>
<c:when test="${param.num < 20 }">
num 大于等于10 ,且小于20
</c:when>
<c:otherwise>
其他,大于等于20
</c:otherwise>
</c:choose>
示例二:
<c:choose>
<c:when test="${param.username eq 'zhangsan'}">用户名是李四</c:when>
<c:when test="${param.username eq 'zhangsan'}">用户名是张三</c:when>
<c:otherwise>this is otherwise param</c:otherwise>
</c:choose>
测试时,请求该网页的url上加上参数即可,假设该网页为http://www.smiling.cc/b.jsp则请求网页可以这样写:
http://www.smiling.cc/b.jsp?username=zhangsan
JSTL中的IF语句:
<c:if test="${empty username}">没有登录</c:if>
<c:if test="${not empty username}">已经登录了</c:if><br/>
JSTL中的URL标签:
<%-- url标签: 自动添加web项目名称--%>
<c:url value="/index.jsp"></c:url><br/>
等价于${pageContext.request.contextPath}/index.jsp<br/>
<%-- url标签: 自动添加web项目名称--%>
<c:url value="/index.jsp"></c:url><br/>
等价于${pageContext.request.contextPath}/index.jsp<br/>
<%--
url标签:用于在jsp页面处理url路径,自动添加web项目名称
* 如果没有var,将处理的结果直接输出
* 如果有var,将处理的结果存放到scope指定域中
redirect标签:jsp重定向
--%>
<c:url value="/index.jsp" var="myUrl" ></c:url> <br/>
my:url ${myUrl} <br/>
${pageContext.request.contextPath}/index.jsp <br/>
<c:url value="/findAllBookServlet" var="baseUrl">
<c:param name="username" value="jack"></c:param>
</c:url>
<a href="路径"></a><br/>
${baseUrl}&pageNum=1<br/>
${baseUrl}&pageNum=2<br/>
${baseUrl}&pageNum=3<br/>
<%--
<c:redirect url="/index.jsp"></c:redirect>
--%>
JSTL中的forEach使用:
-
<body>
-
<%--
-
forEach标签:循环
-
items : 准备用于循环的数据,支持EL表达式
-
var : 每一次遍历到的数据,存放到作用域,在循环体中,可以通过var的值获得内容,当次循环结束,数据将被移除
-
-
--%>
-
-
<h3>字符串</h3>
-
<%
-
//必须放置作用域
-
pageContext.setAttribute("str", "你好,这是page中的消息");
-
%>
-
<c:forEach items="${str}" var="s">
-
${s} <br/>
-
</c:forEach>
-
循环外:${s} <br/>
-
-
-
<h3>数组</h3>
-
<%
-
String[] arr = {"张三","Hello","李四"};
-
//将遍历的数据放置作用域
-
pageContext.setAttribute("arr", arr);
-
%>
-
<c:forEach items="${arr}" var="a">
-
${a} <br/>
-
</c:forEach>
-
-
<h3>list</h3>
-
<%
-
List<String> list = new ArrayList<String>();
-
list.add("晓东");
-
list.add("哈雷");
-
list.add("菊郎");
-
pageContext.setAttribute("list", list);
-
%>
-
<c:forEach items="${list}" var="u">
-
${u} <br/>
-
</c:forEach>
-
-
-
<h3>Map</h3>
-
<%
-
Map<String,String> map = new HashMap<String,String>();
-
map.put("ds007", "威哥");
-
map.put("ds008", "伟哥");
-
map.put("ds009", "魏哥");
-
-
pageContext.setAttribute("map", map);
-
%>
-
<c:forEach items="${map}" var="m"> <%--遍历的每一个数据项 Map.Entry , entry.getKey()--%>
-
${m.key} # ${m.value} <br/>
-
</c:forEach>
-
<%--如果遍历数据,没有使用EL,有可能出现异常,javax.el.PropertyNotFoundException: Property 'key' not found on type java.lang.String --%>
-
-
-
<h3>Map--JavaBean</h3>
-
<%
-
Map<String,Student> map2 = new HashMap<String,Student>();
-
map2.put("ds007", new Student("ds00x","涛涛"));
-
map2.put("ds008", new Student("ds00y","桂桂"));
-
map2.put("ds009", new Student("ds00z","云云"));
-
-
pageContext.setAttribute("map2", map2);
-
%>
-
<c:forEach items="${map2}" var="entry">
-
${entry.key} : ${entry.value.id} , ${entry.value.name} <br/>
-
</c:forEach>
-
-
<h3>普通循环</h3>
-
<c:forEach begin="1" end="9" step="1" var="m">
-
<c:forEach begin="1" end="${m}" var="n">
-
${n} * ${m} = ${m*n}
-
</c:forEach>
-
<br/>
-
</c:forEach>
-
-
-
<h3>jsp脚本元素</h3>
-
-
<%
-
for(int i = 1 ; i <= 9 ; i ++){
-
for(int j = 1 ; j <=i ; j ++){
-
%>
-
<%=j %> * <%=i %> = <%=i*j %>
-
<%
-
}
-
%>
-
<br/>
-
<%
-
}
-
%>
-
-
<table border="1">
-
<%
-
for(int i = 1 ; i <= 9 ; i ++){
-
%>
-
<tr>
-
<%
-
for(int j = 1 ; j <=i ; j ++){
-
%>
-
<td><%=j %> * <%=i %> = <%=i*j %></td>
-
<%
-
}
-
%>
-
</tr>
-
<%
-
}
-
%>
-
</table>
-
-
</body>