JSP之JSTL标签-核心标签库

来源:互联网 发布:数字青少年宫网络寄语 编辑:程序博客网 时间:2024/05/17 08:52

JSTL全名为:Java Standard Tag Library   java标准标签库



使用JSTL标签库的步骤

1.导入jstl支持的jar包      注意:使用javaee5.0以上的项目自动导入jstl支持jar包

2.使用taglib指令导入标签库  <%@tagliburi="uri"prefix="short-name"%>

<uri>http://java.sun.com/jsp/jstl/core</uri>
<short-name>c</short-name>

核心标签库的重点标签:             

保存数据:           

<c:set></c:set>  

<%--使用标签 --%><%--set标签 :保存数据(保存到域中)默认保存到page域 --%><c:set var="name" value="张三" scope="request"></c:set>


 获取数据:

             <c:outvalue=""></c:out>

<%--out标签: 获取数据(从域中) default: 当value值为null时,使用默认值escapeXml: 是否对value值进行转义,false,不转义,true,转义(默认)--%><c:out value="${name}" default="空" escapeXml="false"></c:out>

 单条件判断

            <c:iftest=""></c:if>

<%--if标签 :单条件判断--%>    <c:if test="${10>3 }">    <h3>当test为true时,能看到</h3></c:if>

多条件判断

          <c:choose></c:choose>

          <c:whentest=""></c:when>

          <c:otherwise></c:otherwise>

<%--choose标签+when标签+otherwirse标签: 多条件判断 --%><c:set var="score" value="71"></c:set><c:choose>    <c:when test="${score > 90 && score <= 100}">    优秀    </c:when>    <c:when test="${score > 70 && score <= 90}">    良好    </c:when>    <c:when test="${score > 60 && score <= 70}">    较差    </c:when>    <c:otherwise>    不及格    </c:otherwise></c:choose>

 循环数据

          <c:forEach></c:forEach>

           定义集合:

<%     //List集合List<Student>  list = new ArrayList<Student>();list.add(new Student("张三",20));     list.add(new Student("李四",21));list.add(new Student("王五",22));     //放入域中     pageContext.setAttribute("list",list);           //Map集合     Map<String,Student> map = new HashMap<String,Student>();     map.put("100",new Student("张三",20));     map.put("101",new Student("李四",21));     map.put("102",new Student("王五",23));     //放入域中     pageContext.setAttribute("map",map);%>


遍历:

<%-- forEach标签:循环 --%><%--    begin="" : 从哪个元素开始遍历,从0开始.默认从0开始    end="":     到哪个元素结束。默认到最后一个元素    step="" : 步长    (每次加几)  ,默认1    items="": 需要遍历的数据(集合)     var="": 每个元素的名称     varStatus="": 当前正在遍历元素的状态对象。(count属性:当前位置,从1开始)--%><!-- 遍历List集合 --><c:forEach begin="0" end="2" step="1" var="student" varStatus="stu" items="${list}">     编号:${stu.count}   姓名:${student.name},年龄:${student.age} <br/></c:forEach><hr/><!-- 遍历Map集合 --><c:forEach  var="entry" varStatus="stu" items="${map }">    编号${entry.key}   姓名:${entry.value.name}   年龄:${entry.value.age}<br/></c:forEach>
遍历特殊字符串:

<c:forTokensitems=""delims=""></c:forTokens>


<%String msg = "java-js-C#-C++";pageContext.setAttribute("msg", msg);%>   <%-- forToken标签: 循环特殊字符串 --%><c:forTokens items="${msg}" delims="-" var="s">    ${s}<br/></c:forTokens>

 重定向

           <c:redirect></c:redirect>

<c:redirect url="https://www.baidu.com"></c:redirect>






原创粉丝点击