• 【JSP】JSTL使用core标签总结(不断更新中)


    使用core标签

    在页面中使用taglib指令指定标签URI和prefix.如:
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
    接着可以在页面上使用,如core标签:
    	<c:out value=“${expression}” default=“a”/> 
    	<c:set var=“str” value=“试下” scope=“session”></c:set>//设置某个范围如session属性的值
    	<c:set target=“bean实例” property=“” value=“”/>//设置某个bean成员变量的值  等同于 <jsp:setProperty property=“” value=“”/>
    	<c:remove var=“” scope=“”/>//移除某个范围的变量
    要运行JSTL标签,需要jstl1.2.jar包

    core标签库

    JSTL核心标签库支持使用<c:import>来包含文件,使用<c:url>来打印和格式化URL,使用<c:redirect>来重定向URL.

    将url http://www.url.com/edit.js包含到当前页的当前位置,并将url保存到newsfeed变量中.
    <c:import url="http://www.url.com/edit.js" var="newsfeed"/>
    将请求重新定向到http://www.yourname.com/login.jsp页,相当于response.setRedirect();
    <c:redirect url="http://www.yourname.com/login.jsp"/>

    <c:param>标签

    <c:param>标签用来传递参数给一个重定向或包含页面,例子: 
    <c:redirect url="login.jsp"><c:param name="id" value="888"/></c:redirect>
    将参数888以id为名字传递到login.jsp页面,相当于login.jsp?id=888

    If判断

      <%
      	int score = 90;
      	pageContext.setAttribute("score", score);
      %>
      <c:if test="${score>80}">优秀</c:if>

    if多分支判断

      <%
      	int score = 90;
      	pageContext.setAttribute("score", score);
      %>
      <c:choose>
      	<c:when test="${score<60}">不及格</c:when>
      	<c:when test="${score<80}">及格</c:when>
      	<c:otherwise>优秀</c:otherwise>
      </c:choose>

    for循环遍历ArrayList

      <%
      ArrayList aList = new ArrayList();
      aList.add(23);
      aList.add(true);
      aList.add("ArrayList");
      aList.add(new Date());
      aList.add(3445652);
      pageContext.setAttribute("aList", aList);
      int size = aList.size();
      pageContext.setAttribute("size", size);
      %>
      <c:forEach begin="0" end="${size}" var="i">
      	${aList[i]} 
      </c:forEach>

    增强型for循环遍历ArrayList

      <%
      ArrayList aList = new ArrayList();
      aList.add(23);
      aList.add(true);
      aList.add("ArrayList");
      aList.add(new Date());
      aList.add(3445652);
      pageContext.setAttribute("aList", aList);
      %>
      <c:forEach items="${aList}" var="i">
      	${i} 
      </c:forEach>




  • 相关阅读:
    【基础算法】- 全排列
    【基础算法】- 2分查找
    区块链培训
    Static Binding (Early Binding) vs Dynamic Binding (Late Binding)
    test
    No data is deployed on the contract address!
    "throw" is deprecated in favour of "revert()", "require()" and "assert()".
    Variable is declared as a storage pointer. Use an explicit "storage" keyword to silence this warning.
    京都行
    Failed to write genesis block: database already contains an incompatible
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3285642.html
Copyright © 2020-2023  润新知