JSTL的相关语法入门

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使用:

  1. <body>
  2. <%--
  3. forEach标签:循环
  4. items : 准备用于循环的数据,支持EL表达式
  5. var 每一次遍历到的数据,存放到作用域,在循环体中,可以通过var的值获得内容,当次循环结束,数据将被移除
  6. --%>

  7. <h3>字符串</h3>
  8. <%
  9. //必须放置作用域
  10. pageContext.setAttribute("str", "你好,这是page中的消息");
  11. %>
  12. <c:forEach items="${str}" var="s">
  13. ${s} <br/>
  14. </c:forEach>
  15. 循环外:${s} <br/>
  16. <h3>数组</h3>
  17. <%
  18. String[] arr = {"张三","Hello","李四"};
  19. //将遍历的数据放置作用域
  20. pageContext.setAttribute("arr", arr);
  21. %>
  22. <c:forEach items="${arr}" var="a">
  23. ${a} <br/>
  24. </c:forEach>
  25. <h3>list</h3>
  26. <%
  27. List<String> list = new ArrayList<String>();
  28. list.add("晓东");
  29. list.add("哈雷");
  30. list.add("菊郎");
  31. pageContext.setAttribute("list", list);
  32. %>
  33. <c:forEach items="${list}" var="u">
  34. ${u} <br/>
  35. </c:forEach>
  36. <h3>Map</h3>
  37. <%
  38. Map<String,String> map = new HashMap<String,String>();
  39. map.put("ds007", "威哥");
  40. map.put("ds008", "伟哥");
  41. map.put("ds009", "魏哥");
  42. pageContext.setAttribute("map", map);
  43. %>
  44. <c:forEach items="${map}" var="m"> <%--遍历的每一个数据项 Map.Entry , entry.getKey()--%>
  45. ${m.key} # ${m.value} <br/>
  46. </c:forEach>
  47. <%--如果遍历数据,没有使用EL,有可能出现异常,javax.el.PropertyNotFoundException: Property 'key' not found on type java.lang.String --%>
  48. <h3>Map--JavaBean</h3>
  49. <%
  50. Map<String,Student> map2 = new HashMap<String,Student>();
  51. map2.put("ds007", new Student("ds00x","涛涛"));
  52. map2.put("ds008", new Student("ds00y","桂桂"));
  53. map2.put("ds009", new Student("ds00z","云云"));
  54. pageContext.setAttribute("map2", map2);
  55. %>
  56. <c:forEach items="${map2}" var="entry">
  57. ${entry.key} : ${entry.value.id} , ${entry.value.name} <br/>
  58. </c:forEach>
  59. <h3>普通循环</h3>
  60. <c:forEach begin="1" end="9" step="1" var="m">
  61. <c:forEach begin="1" end="${m}" var="n">
  62. ${n} * ${m} = ${m*n}
  63. </c:forEach>
  64. <br/>
  65. </c:forEach>
  66. <h3>jsp脚本元素</h3>
  67. <%
  68. for(int i = 1 ; i <= 9 ; i ++){
  69. for(int j = 1 ; j <=i ; j ++){
  70. %>
  71. <%=j %> * <%=i %> = <%=i*j %>
  72. <%
  73. }
  74. %>
  75. <br/>
  76. <%
  77. }
  78. %>
  79. <table border="1">
  80. <%
  81. for(int i = 1 ; i <= 9 ; i ++){
  82. %>
  83. <tr>
  84. <%
  85. for(int j = 1 ; j <=i ; j ++){
  86. %>
  87. <td><%=j %> * <%=i %> = <%=i*j %></td>
  88. <%
  89. }
  90. %>
  91. </tr>
  92. <%
  93. }
  94. %>
  95. </table>
  96. </body>


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值