JSP基础语法之十六:JSTL配置和核心标签(C)的使用

来源:互联网 发布:会员中心源码 编辑:程序博客网 时间:2024/04/30 14:09



一:下载jstl1.2

在CSDN上放了个备份:http://download.csdn.net/detail/ruantao1989/4636521

把jar包放在 \apache-tomcat-6.0.30\lib下




二:将jar包中需要的tld拷贝到虚拟目录的WEB-INF下

比如c.tld , fmt.tld ,sql.tld 等,

放在WEB-INF/JSTL下(其实放置的位置随意,下边会映射位置信息的)




三:配置web.xml

以C核心库为例,其余几个也大同小异

<!-- JSTL标签配置 --><jsp-config><taglib><taglib-uri>JSTL-c</taglib-uri><taglib-location>/WEB-INf/JSTL/c.tld</taglib-location></taglib></jsp-config>




四:测试jsp

<%@ taglib prefix="c" uri="JSTL-c"%>这句和要引入的包 还有web.xml的配置有关

<%@ page contentType="text/html" pageEncoding="GBK"%><%@ page import="com.rt.demo.testBean.TestBean"%><%@ page import="java.util.*"%><%@ taglib prefix="c" uri="JSTL-c"%><html><%TestBean tb = new TestBean();request.setAttribute("testBean",tb);%><body><!-- 1.设置属性 --><c:set var="info" value="valuteOfInfo" scope="page" /><c:set value="valueOfBean" target="${testBean}" property="name"/><h3>1:写到Bean中:${testBean.name}</h3><!-- 2.内容输出 --><h3>1: <c:out value="JSTL-c"/> </h3><h3>2: <c:out value="${info}"/> </h3><h3>3:<c:out value="${xiaBianDe}" /> </h3><h3>4: <c:out value="${xiaBianDe}" default="没有此属性"/> </h3><!-- 3.在属性范围中删除 --><c:remove var="info" scope="request"/><!-- 4.异常处理--><c:catch var="err"><%  int i = 1/0; %></c:catch><h3>异常信息: ${err}</h3><!-- 5.判断 --><c:if test="${2>1}" var="res" scope="page"><h3>c:if结果: ${res}</h3></c:if><!-- 6.循环 --><%String[] arry = {"1","2","3","4","5"};pageContext.setAttribute("ay",arry);%><h3>数组(或List)循环输出<c:forEach items="${ay}" var="out" begin="0" end="3" step="2">${out} _</c:forEach></h3><%Map map = new HashMap();map.put("0","1");map.put("1","2");pageContext.setAttribute("mp",map);%><h3>Map循环输出<c:forEach items="${mp}" var="out">${out.key} _</c:forEach></h3><!-- 7.跳转(客户端跳转) --><c:redirect url="xxx.jsp" /></body></html> 











原创粉丝点击