JSP:JSTL的表达式标签详解

JSTL的表达式标签包含:<c:out>,<c:set>,<c:remove>,<c:catch>,<c:import>,<c:import>,<c:redirect>,<c:url>,<c:if>,

                                         <.c:choose>,<c:when>,<c:otherwise>

1,<c:out>

<c:out>用于将表达式的值输出到JSP页面,类似<%=%>,EL表达式${expression},以及out.print();

表达式:1 <c:out value="expression" escapeXml="false or true" default="默认内容"/>   

               2<c:out value="expression" escapeXml="true or false">默认内容</c:out>

escapeXml可有可无表示是否执行html标签,当为false时,执行html标签,否则将value内的全部当作字符串

default可有可无表示输出的默认内容

<%@ page language="java" contentType="text/html; charset=GB18030"
	pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB18030">
<title>Insert title here</title>
</head>
<body>
	<c:out value="水平写标记<hr>" escapeXml="true"></c:out>
	<c:out value="水平写标记<hr>" escapeXml="false"></c:out>
</body>
</html>

结果:

2,<c:set>

<c:set>用于在指定范围内中定义保存某个值的变量,或为指定的对象设置属性值

表达式:1,<c:set var="name" value="赋值内容" scope="范围"/>

               2,<c:set var="name" scope="范围">赋值内容</c:set>

               3,<c:set value="赋值内容" target="object" property="propName"/>类似于javabean

               4,<c:set target="object" property="propName">赋值内容</c:set>

var用于指定变量名,为你自己定义的变量名

value用于给var赋值,可以使用EL表达式

scope用于指定变量的作用域,默认值为page。可选page,request,session,application。

target用于指定储存变量或者标签体的目标对象

javabeand的类


public class UserInfo {
	private String name = "";

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
<%@ page language="java" contentType="text/html; charset=GB18030"
	pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="Test.UserInfo"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB18030">
<title>Insert title here</title>
</head>
<body>
	<c:set var="username" value="明日科技" scope="request" />
	<c:out value="username的值为:${username} "></c:out>
	
	<jsp:useBean id="userInfo" class="Test.UserInfo"></jsp:useBean>
	<c:set target="${userInfo}" property="name">你好</c:set>
	<c:out value="UserInfo的name属性值为:${userInfo.name }"></c:out>
</body>
</html>

结果:

3,<c:remove>

<c:remove>用于移除指定的JSP范围内的变量

表达式:<c:remove var="name" scope="范围">

var表示将要移除的变量名

scope表示移除变量的有效范围,如上一样。

eg:

<%@ page language="java" contentType="text/html; charset=GB18030"
	pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB18030">
<title>Insert title here</title>
</head>
<body>
	<c:set var="username" value="明日" scope="request"></c:set>
	<c:out value="${username }"></c:out>
	<c:remove var="username" scope="request" />
	<c:out value="${username }" default="空"></c:out>
</body>
</html>

4,<c:catch>

<c:catch>标签用于捕获程序中出现的异常,类似于try...catch...

表达式:<c:catch var="exception">

               .......................................

               </c:catch>

package Test;

public class UserInfo {
	private String name = "";

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
<%@ page language="java" contentType="text/html; charset=GB18030"
	pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB18030">
<title>Insert title here</title>
</head>
<body>
	<c:catch var="error">
		<jsp:useBean id="userInfo" class="Test.UserInfo"></jsp:useBean>
		<c:set target="${userInfo }" property="pwd">111</c:set>
	</c:catch>
	<c:out value="${error }"></c:out>
</body>
</html>

5<c:import>

<c:import>标签可以导入站内或其他网页的静态和动态文件到Web页面中。类似HTML导入CSS

表达式:1,<c:import url="url" context="context" var="name" scope="范围" charEncoding="encoding">

              2,<c:import url="url" varReader="name" context="context" charEncoding="encoding">

url用于指定被导入的文件爱你资源的URL地址

context上下文路径,用于访问同一个服务器的其他Web应用

var用于指定变量名称

scope用于指定的变量的存在范围

varReader用于指定一个变量名

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<c:set var="typeName" value="流行金曲|经典老歌|街舞DJJ|欧美金曲|少儿歌曲|轻音乐|最新上榜"></c:set>
	<c:import url="navigation.jsp" charEncoding="UTF-8">
		<c:param name="typeList" value="${typeName }"></c:param>
	</c:import>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table width="901" height="126" border="0" align="center" cellpadding="0" cellspacing="0">
		<tr>
			<td width="16" height="91">&nbsp;</td>
			<td width="885">&nbsp;</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td style="font-size:11pt; color:red"><b>${param.typeList }</b></td>
		</tr>
	</table>
</body>
</html>

结果:

6,<c:url>

用于生成一个URL路径的字符串,可以赋予HTML的<a>标记实现URL的连接

表达式:1,<c:url value="url" var="name" scope="范围" context="context"/>

              2,<c:url value="url" var="name" scope="范围" context="context"><c:param/>

value用于指定将要处理URL地址,可以用EL

context上下文路径用于访问同一个服务器的其他Web工程。

var用于指定变量名称

scope用于指定变量的存在范围

<%@ page language="java" contentType="text/html; charset=GB18030"
	pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB18030">
<title>Insert title here</title>
</head>
<body>
	<c:catch var="error">
		<jsp:useBean id="userInfo" class="Test.UserInfo"></jsp:useBean>
		<c:set target="${userInfo }" property="pwd">111</c:set>
	</c:catch>
	<c:out value="${error }"></c:out>
</body>
</html>

7,<c:redirect>

可以向客户端发出的request请求重定向到其他URL地址

表达式: 1, <c:redirect url="url" context="/ontext"/>

             2, <c:redirect url="rul" context="/context"><c:param/></c:redirect>

url用于指定待定向资源URL,可以用EL.

context用于在使用相对路径访问 外部context资源时,指定资源的名字.

<%@ page language="java" contentType="text/html; charset=GB18030"
	pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB18030">
<title>Insert title here</title>
</head>
<body>
	<c:redirect url="UserListServlet">
		<c:param name="action" value="query"></c:param>
	</c:redirect>
</body>
</html>

8,<c:param>

只用为其他标签提供参数信息

表达式:  <c:param name="paramName" value="paramValue"/>

name用于指定参数名

value用于指定参数

<%@ page language="java" contentType="text/html; charset=GB18030"
	pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB18030">
<title>Insert title here</title>
</head>
<body>
	<c:redirect url="main.jsp">
		<c:param name="user" value="wgh"></c:param>
	</c:redirect>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
	pageEncoding="GB18030"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB18030">
<title>Insert title here</title>
</head>
<body>
	[${param.user}]您好,欢迎访问我公司的网站
</body>
</html>

9,<c:if>

条件判断可以根据不同条件处理不同条件处理不同的业务

test用于指定表达式条件,可以用EL

var用于指定变量名,保存test属性值

<%@ page language="java" contentType="text/html; charset=GB18030"
	pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB18030">
<title>Insert title here</title>
</head>
<body>
	<c:if  var="result" test="${empty param.username }">
		<form name="form1" method="post" action="">
			用户名:<input name="username" type="text" id="username">
			<br>
			<br>
			<input type="submit" name="Submit" value="登录">			
		</form>
	</c:if>
	<c:if test="${!result }">
		[${param.username }]欢迎光临我们的公司网站
	</c:if>
</body>
</html>

结果:

10,<c:choose>

<c:choose>标签可以根据不同条件完成制定业务逻辑,如果没有符合条件就执行默认条件的业务逻辑

<c:choose>标签题</c:choose>

<%@ page language="java" contentType="text/html; charset=GB18030"
	pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB18030">
<title>Insert title here</title>
</head>
<body>
	<c:choose>
		<c:when test="${empty param.username }">
			<form name="form1" method="post" action="">
				用户名:<input name="username" type="text" id="username"> <br>
				<br> <input type="submit" name="Submit" value="登录">
			</form>
		</c:when>
		<c:otherwise>
			[${param.username}]欢迎访问我公司网站!
		</c:otherwise>
	</c:choose>
</body>
</html>

11,<c:when>

条件测试标签,根据不同的条件执行相应的业务逻辑,类似switch.

<c:when test="condition">标签体</c:when>

<%@ page language="java" contentType="text/html; charset=GB18030"
	pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB18030">
<title>Insert title here</title>
</head>
<body>
	<c:set var="hours">
		<%=new java.util.Date().getHours() %>
	</c:set>
	<c:set var="second">
		<%=new java.util.Date().getMinutes() %>
	</c:set>
	<c:choose>
		<c:when test="${hours>1&&hours<6 }">早上好</c:when>
		<c:when test="${hours>6&&hours<11 }">上午好</c:when>
		<c:when test="${hours>11&&hours<17 }">下午好</c:when>
		<c:when test="${hours>17&&hours<24 }">晚上好</c:when>
	</c:choose>
	现在的时间是:${hours }:${second }
</body>
</html>

结果:

12,<c:otherwise>

特别简单,类似在if中的else

表达式:  <c:otherwise>标签体</c:otherwise>

<%@page import="java.util.Random"%>
<%@ page language="java" contentType="text/html; charset=GB18030"
	pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB18030">
<title>Insert title here</title>
</head>
<body>
	<%
		Random rnd = new Random();
	%>
	<c:set var="luck">
		<%=rnd.nextInt(10)%>
	</c:set>
	<c:choose>
		<c:when test="${luck==6 }">恭喜你,中了一等奖</c:when>
		<c:when test="${luck==7 }">恭喜你,中了二等奖</c:when>
		<c:when test="${luck==8 }">恭喜你,中了三等奖</c:when>
		<c:otherwise>谢谢您的参与</c:otherwise>
	</c:choose>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_42192693/article/details/81407521
今日推荐