JSTL

JSTL:标签函数库
1.核心标签库 core
2.国际化标签 fmt
3.数据库 sql
4.XML xml
5.JSTL el

jsp文件加上:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

输出常量
<c:out value="xxx"/>

输出变量
<%
String name = "无双";
pageContext.setAttribute("name",name);
%>
<c:out value="${name}"></c:out>

输出默认值,从域中找不到值就输出默认值
<%
String addr = "西二旗";
//pageContext.setAttribute("addr",addr);
%>
<c:out value="${addr}" default="北京"></c:out>
${addr == null?"北京" : addr }

HTML转义输出
<a href="#">xxx</a>
<c:out value="<a href='#'>xxx</a>"></c:out>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
${fn:escapeXml('<a href="#">xxx</a>') }

设置修改域中的值
默认是page域
<c:set var="name" value="韦小宝"></c:set>
<c:set var="name" value="阿珂"></c:set>
${name }

修改域中map的值
<%
Map map = new HashMap();
pageContext.setAttribute("map",map);
%>
<c:set target="${map}" property="cellphone" value="10010"></c:set>
<c:set target="${map}" property="cellphone" value="10086"></c:set>
${map.cellphone }

修改域中javaBean的属性的值
<%
Person p = new Person();
pageContext.setAttribute("p",p);
%>
<c:set target="${p}" property="name" value="克林顿"></c:set>
${p.name }

删除域中的值
<%
pageContext.setAttribute("name","蓝精灵");
request.setAttribute("name","伏地魔");
session.setAttribute("name","机器猫");
application.setAttribute("name","蜡笔小新");
%>
<c:remove var="name"/>
${name }

使用catch捕获异常
<c:catch var="e">
<%
int i = 1/0;
%>
</c:catch>

${e.message }

 

c:if标签
<c:if test="${2>1}">
确实是这样的....
</c:if>
<c:if test="${2<=1}">
你确定吗?
</c:if>

c:choose标签
<%
int day = 7;
pageContext.setAttribute("day",day);
%>
<c:choose>
<c:when test="${day == 1}">
星期一
</c:when>
<c:when test="${day == 2}">
星期二
</c:when>
<c:when test="${day == 3}">
星期三
</c:when>
<c:when test="${day == 4}">
星期四
</c:when>
<c:when test="${day == 5}">
星期五
</c:when>
<c:otherwise>
休息日!
</c:otherwise>
</c:choose>

c:forEeach 实现循环遍历的作用
遍历数组
<%
String [] city = {"北京","上海","广州","铁岭","葫芦岛"};
pageContext.setAttribute("city",city);
%>
<c:forEach items="${city}" var="c">
${c }<br>
</c:forEach>
遍历集合中的数据
<%
List list = new ArrayList();
list.add("美国");
list.add("中国");
list.add("俄罗斯");
list.add("印度");
list.add("巴西");
pageContext.setAttribute("list",list);
%>
<c:forEach items="${list}" var="c">
${c }<br>
</c:forEach>

遍历Map中的数据
<%
Map map = new LinkedHashMap();
map.put("name","曹操");
map.put("age","59");
map.put("wife","小乔");
map.put("gender","男");
pageContext.setAttribute("map",map);
%>
<c:forEach items="${map}" var="entry" >
${entry.key }:${entry.value }<br>
</c:forEach>

循环执行指定的内容若干次
<c:forEach begin="0" end="10" step="2" var="i" >
${i },
</c:forEach>

<h1>实验:遍历10到100的偶数,如果数字所在的位置是3的倍数,显示成红色</h1><hr>
<c:forEach begin="10" end="100" step="2" var="i" varStatus="stat">
<c:if test="${stat.count % 3 == 0}">
<font color="red">
${i }
</font>
</c:if>
<c:if test="${stat.count % 3 != 0}">
<font color="blue">
${i }
</font>
</c:if>
</c:forEach>

c:forTokens
用来浏览一字符串中的所有的成员,其成员是由定义符号所分隔的
<c:forTokens items="www.itheima.com" delims="." var="str">
${str }<br>
</c:forTokens>

c:imort
实现页面包含
from xxxxx....
<c:import url="/index.jsp"/>
将index.jsp显示出来
<c:import url="/index.jsp" var="p" scope="page"></c:import>
将index里面的内容存入p里面 ,在page域中

c:redirect
实现请求重定向

<c:redirect url="/index.jsp" context="${pageContext.request.contextPath}">
<c:param name="name" value="zhang"></c:param>
</c:redirect>

c:url
实现url重写的标签
<%
String url = response.encodeURL(request.getContextPath()+"/index.jsp");
%>
<a href="<%= url %>">hhhh</a>

<c:url value="/index.jsp" context="${pageContext.request.contextPath}" var="url" scope="page"></c:url>
<a href="${url }">xxx</a>

c:param

 

posted @ 2015-03-08 12:15  superPerfect  阅读(351)  评论(0编辑  收藏  举报