JSTL标准标签库

必须引入jstl.jar和standard.jar

c:out输出

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<body>
<c:out value="jstl"></c:out>
</body>
<body>
<%
	pageContext.setAttribute("people","张三");
%>
<h2><c:out value="${people}"></c:out></h2>
<h2><c:out value="${people2}" default="某人"></c:out></h2>
</body>

c:set设置

target 目标对people对象的id属性设置值

<body>
<c:set var="people" value="张三" scope="request"></c:set>
<h2><c:out value="${people}"></c:out></h2>
<jsp:useBean id="people2" class="com.model.People" scope="page"></jsp:useBean>
<c:set property="id" target="${people2 }" value="007"></c:set>
<c:set property="name" target="${people2 }" value="王二小"></c:set>
<c:set property="age" target="${people2 }" value="16"></c:set>
<h2>编号:${people2.id }</h2>
<h2>姓名:${people2.name }</h2>
<h2>年龄:${people2.age }</h2>
</body>

c:remove 删除

<body>
<c:set var="people" value="张三" scope="request"></c:set>
<h2><c:out value="${people}" default="没人啊"></c:out></h2>
<c:remove var="people" scope="request"/>
<h2><c:out value="${people}" default="没人啊"></c:out></h2>
</body>

c:catch 异常

<body>
<c:catch var="errMsg">
	<%
		int a=1/0;
	%>
</c:catch>
<h2>异常信息:${errMsg }</h2>
</body>

条件判断

<c:if test="${people.name=='王二小' }" var="r" scope="page">
	<h2>是王二小</h2>
</c:if>
<c:if test="${people.age<18 }">
	<h2>是未成年</h2>
</c:if>
</body>

多条件判断

<c:choose>
	<c:when test="${people.age<18 }">
		<h2>小于18</h2>
	</c:when>
	<c:when test="${people.age==18 }">
		<h2>等于18</h2>
	</c:when>
	<c:otherwise>
		<h2>大于18</h2>
	</c:otherwise>
</c:choose>

c:forEach遍历数组

step 跳2步

索引从0开始 begin 1位第二小黄

<body>
<%
	String dogs[]={"小黑","小黄","小白","小小"};
    pageContext.setAttribute("dogs",dogs);
%>
<c:forEach var="dog" items="${dogs }">
	${dog }
</c:forEach>
<hr/>
<c:forEach var="dog" items="${dogs }" step="2">
	${dog }
</c:forEach>
<hr/>
<c:forEach var="dog" items="${dogs }" begin="1" end="2">
	${dog }
</c:forEach>
<hr/>
<%
	List<People> pList=new ArrayList<People>();
    pList.add(new People(1,"张三",10));
    pList.add(new People(2,"李四",20));
    pList.add(new People(3,"王五",30));
    pageContext.setAttribute("pList",pList);
%>
<table>
	<tr>
		<th>编号</th>
		<th>姓名</th>
		<th>年龄</th>
	</tr>
	<c:forEach var="p" items="${pList }">
		<tr>
			<td>${p.id }</td>
			<td>${p.name }</td>
			<td>${p.age }</td>
		</tr>
	</c:forEach>
</table>
</body>

分隔输出

<body>
<%
	String str1="www.baidu.com";
	String str2="张三,李四,王五";
    pageContext.setAttribute("str1",str1);
    pageContext.setAttribute("str2",str2);
%>
<c:forTokens items="${str1 }" delims="." var="s1">
	${s1 }
</c:forTokens>
<hr/>
<c:forTokens items="${str2 }" delims="," var="s2">
	${s2 }
</c:forTokens>
</body>

导入页面

<body>
<c:import url="c_forEach.jsp"></c:import>
<c:import url="c_if.jsp"></c:import>
</body>

url

<body>
<c:url value="http://www.baidu.com" var="url">
	<c:param name="name" value="王二小"></c:param>
	<c:param name="age" value="26"></c:param>
</c:url>
<a href="${url }">百度</a>
</body>

重定向跳转

<body>
<c:redirect url="target.jsp">
	<c:param name="name" value="wangerxiao"></c:param>
	<c:param name="age" value="26"></c:param>
</c:redirect>
</body>

内置对象param取值

<body>
<h2>姓名:${param.name }</h2>
<h2>年龄:${param.age }</h2>
</body>

国际化标签fmt

设置用户所在区域

<body>
<%
	pageContext.setAttribute("date",new Date());
%>
中文日期:
<fmt:setLocale value="zh_CN"/>
<fmt:formatDate value="${date }"/>
<hr/>
英文日期:
<fmt:setLocale value="en_US"/>
<fmt:formatDate value="${date }"/>
</body>

去读国际化资源

info为property文件

info.properties

{0}动态塞入参数

name=wangerxiao
info=Current user{0}:Welcome to use our
<body>
<fmt:setLocale value="zh_CN"/>
<fmt:bundle basename="info">
	<fmt:message key="name" var="userName"/>
</fmt:bundle>
<h2>姓名:${userName }</h2>
<fmt:bundle basename="info">
	<fmt:message key="info" var="infomation">
		<fmt:param value="<font color='red'>wangerxiao</font>"/>
	</fmt:message>
</fmt:bundle>
<h2>信息:${infomation }</h2>
<hr/>
<fmt:setLocale value="en_US"/>
<fmt:bundle basename="info">
	<fmt:message key="name" var="userName"/>
</fmt:bundle>
<h2>姓名:${userName }</h2>
<fmt:bundle basename="info">
	<fmt:message key="info" var="infomation">
		<fmt:param value="<font color='red'>wangerxiao</font>"/>
	</fmt:message>
</fmt:bundle>
<h2>信息:${infomation }</h2>
</body>

格式化数字

<body>
<!-- value:数值 ;  type:数值类型;  pattern:格式 -->
<fmt:formatNumber value="12" type="currency" pattern="$.00"/> 
<fmt:formatNumber value="12" type="currency" pattern="$.0#"/>
<fmt:formatNumber value="1234567890" type="currency"/> 
<fmt:formatNumber value="123456.7891" pattern="#,#00.0#"/>
</body>

格式化日期

<body>
<!-- value:数值 ;  type:数值类型;  pattern:格式 -->
<%
	Date date=new Date();
	pageContext.setAttribute("date",date);
%>
<fmt:formatDate value="${date }" pattern="yyyy-MM-dd HH:mm:ss"/>
<hr/>
<fmt:formatDate value="${date }" pattern="yyyy-MM-dd"/>
</body>

设置时区,默认系统的当前日期(北京)

<body>
<!-- value:数值 ;  type:数值类型;  pattern:格式 -->
<%
	Date date=new Date();
	pageContext.setAttribute("date",date);
%>
当前时间:<fmt:formatDate value="${date }" pattern="yyyy-MM-dd HH:mm:ss"/>
<hr/>
格林尼治时间:
<fmt:timeZone value="GMT">
   <fmt:formatDate value="${date }" pattern="yyyy-MM-dd HH:mm:ss"/>
</fmt:timeZone>
</body>

sql标签

引入mysql-connector-java.jar 驱动包

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<body>
<h1>设置JDBC连接</h1>
<sql:setDataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/db_jstl" user="root" password="123456" />
<sql:query var="result">
	select * from t_student;
</sql:query>
<h2>总记录数:${result.rowCount }</h2>
<table>
	<tr>
		<th>编号</th>
		<th>学号</th>
		<th>姓名</th>
		<th>出生日期</th>
		<th>性别</th>
	</tr>
	<c:forEach var="student"  items="${result.rows }">
	<tr>
		<td>${student.id }</td>
		<td>${student.stuNo }</td>
		<td>${student.stuName }</td>
		<td>${student.birthday }</td>
		<td>${student.sex }</td>
	</tr>
	</c:forEach>
</table>
</body>

sql update

<body>
<h1>设置JDBC连接</h1>
<sql:setDataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/db_jstl" user="root" password="123456"/>
<h1>添加数据</h1>
<sql:update var="result" >
	insert into t_student values(null,"008","xxx","1991-1-1","男");
</sql:update>
<h1>修改数据</h1>
<sql:update var="result" >
	update t_student set stuNo="010",sex="未知" where id=6
</sql:update>
<h1>删除数据</h1>
<sql:update var="result" >
	delete from t_student where id=6
</sql:update>
</body>

事物

<body>
<h1>设置JDBC连接</h1>
<sql:setDataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/db_jstl" user="root" password="123456"/>
<h1>事务</h1>
<sql:transaction>
	<sql:update var="result" >
		insert into t_student values(null,"008","xxx","1991-1-1","男");
	</sql:update>
</sql:transaction>
</body>

JSTL 的XML标签库

xml文件

select 前面是var的对象,usersInfoXml  后面是xml文件的层次路径

<?xml version="1.0" encoding="UTF-8"?>
<users>
	<user>
		<name id="n1">张三</name>
		<birthday>2011-1-1</birthday>
	</user>
</users>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
<body>
<c:import var="usersInfo" url="usersInfo.xml" charEncoding="UTF-8"/>
<x:parse var="usersInfoXml" doc="${usersInfo }"/>
<h2>姓名:<x:out select="$usersInfoXml/users/user/name"/>
(ID:<x:out select="$usersInfoXml/users/user/name/@id"/>)</h2>
<h2>出生日期:<x:out select="$usersInfoXml/users/user/birthday"/></h2>
</body>

set把xml的属性保存到指定的范围

<body>
<c:import var="usersInfo" url="usersInfo.xml" charEncoding="UTF-8"/>
<x:parse var="usersInfoXml" doc="${usersInfo }"/>
<x:set var="userInfoXml" select="$usersInfoXml/users/user"/>
<h2>姓名:<x:out select="$userInfoXml/name"/></h2>
</body>

多条件判断

<body>
<c:import var="usersInfo" url="usersInfo.xml" charEncoding="UTF-8"/>
<x:parse var="usersInfoXml" doc="${usersInfo }"/>
<x:choose>
	<x:when select="$usersInfoXml/users/user/name/@id='n2'">
		<h2>有编号是n2的user信息</h2>
	</x:when>
	<x:otherwise>
		<h2>没有编号是n2的user信息</h2>
	</x:otherwise>
</x:choose>
</body>

遍历

xml文件

<?xml version="1.0" encoding="UTF-8"?>
<users>
	<user>
		<name id="n1">张三</name>
		<birthday>2011-1-1</birthday>
	</user>
	<user>
		<name id="n2">王五</name>
		<birthday>2011-1-2</birthday>
	</user>
	<user>
		<name id="n3">赵六</name>
		<birthday>2011-1-3</birthday>
	</user>
</users>
<body>
<c:import var="usersInfo" url="usersInfo2.xml" charEncoding="UTF-8"/>
<x:parse var="usersInfoXml" doc="${usersInfo }"/>
<x:forEach select="$usersInfoXml/users/user" var="userInfoXml">
	<h2>姓名:<x:out select="$userInfoXml/name"/>&nbsp;出生日期:<x:out select="$userInfoXml/birthday"/></h2>
	<hr/>
</x:forEach>
</body>

jstl函数标签库

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<body>
<%
	pageContext.setAttribute("info","www.baidu.com");
%>
<h2>查找java1234位置:${fn:indexOf(info,"baidu")}</h2>
<h2>判断java1234是否存在:${fn:contains(info,"baidu")}</h2>
<h2>截取:${fn:substring(info,0,5)}</h2>
<h2>拆分:${fn:split(info,".")[1]}</h2>
</body>

猜你喜欢

转载自blog.csdn.net/qq_35029061/article/details/81604729