jsp_JSTL标签
来源:互联网 发布:中兴手机mac地址 编辑:程序博客网 时间:2024/06/08 00:16
1: JSTL标签 1): 使用 首先需要导入两个jar(jstl-1.2.jar, standard-1.1.2.jar) 然后在我们的jsp页面,导入JSTL的核心标签库 如: <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 2): 分类 a): 核心标签库 (c标签)--重点 b): 国际化标签库 (fmt标签) c): EL函数库 (fn标签) d): xml标签库 (x标签) e): sql标签库 (sql标签)2: JSTL核心标签库 1): 表达式相关 2): 循环判断相关 3): url相关
<%@page import="com.tz.servlet.bean.User"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ page import="java.util.*"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";pageContext.setAttribute("path",path);request.setCharacterEncoding("UTF-8");%><!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>Examples</title> <meta name="description" content=""> <meta name="keywords" content=""> <link href="" rel="stylesheet"> <style type="text/css" media="screen"> </style> </head> <body> <%-- 表达式相关 --%> <%-- <c:set> --%> <c:set var="name1" value="小红" scope="page"></c:set> ${pageScope.name1 } <br> <% User user1 = new User("小红","123"); pageContext.setAttribute("user1", user1); %> <c:set target="${pageScope.user1 }" property="password" value="123456"></c:set> ${pageScope.user1 } <hr> <%-- <c:out> --%> <c:out value="小红小妹子"></c:out> <br> <c:out value="${pageScope.user2 }" default="<h4>没有值 </h4>" escapeXml="false"></c:out> <%--escapeXml的默认值是true,是不会去解析上一行代码中的h4标签的;是false的时候,才会去解析上一行中的h4标签。--%> <hr> <%-- <c:remove> 删除属性--%> <c:out value="${pageScope.user1 }" default="<h4>没有值 </h4>" escapeXml="true"></c:out> <c:remove var="user1" scope="session"/> <c:out value="${pageScope.user1 }" default="<h4>没有值 </h4>" escapeXml="true"></c:out> <hr> <%-- <c:catch> 可以 用来捕获异常 --%> <c:catch var="message"> <% int i = 1/0; %> </c:catch> ${pageScope.message } <hr> <%-- 循环判断相关 --%> <%-- <c:if test=""> --%> <c:if test="${5>8 }" > 条件为真的 </c:if><br> <% User user2 = new User("小明","123"); pageContext.setAttribute("user2", user2); %> <c:if test="${pageScope.user2.password == 123 }">小明的密码正确</c:if> <br> <c:if test="${pageScope.user2.password == 321 }" var="result" scope="session">小明的密码正确</c:if> ${sessionScope.result } <%--把if判断的结果,赋值到result属性中来,然后把这个属性设置到session域中来。--%> <hr> <%-- <c:choose>,<c:when test="">,<c:otherwise> --%> <% pageContext.setAttribute("age", 68); %> <c:choose> <c:when test="${pageScope.age>60 }"> 你已经老了...... </c:when> <c:when test="${pageScope.age>40 }"> 你马上就要老了...... </c:when> <c:when test="${pageScope.age>18 }"> 你也快要老了....... </c:when> <c:otherwise> 还不抓紧努力学习,等老了怎么办...... </c:otherwise> </c:choose> <hr> <%-- <c:forEach> --%> <%-- 遍历List集合 --%> <% User user3 = new User("小梅","456"); User user4 = new User("小芳","789"); List<User> userList = new ArrayList<User>(); userList.add(user1); userList.add(user2); userList.add(user3); userList.add(user4); pageContext.setAttribute("userList", userList); %> <%--items的值表示当前要遍历的集合,var="user"表示当前遍历到的这一个元素赋值给user--%> <c:forEach items="${pageScope.userList }" var="user"> ${user } <br> </c:forEach> <hr> <%--begin="1" end="3" step="2",表示遍历的起点和结束点,以及步长。status.index表示当前遍历到的元素下标。--%> <c:forEach items="${pageScope.userList }" var="user" begin="1" end="3" step="2" varStatus="status"> ${status.count }--${status.index }-- ${user }---${user.userName }: ${user.password } <br> </c:forEach> <hr> <%-- 遍历数组 --%> <% User[] users = new User[]{user1,user2,user3,user4}; pageContext.setAttribute("users", users); %> <c:forEach items="${pageScope.users }" var="user" varStatus="status"> ${status.count }-- ${user }<br> </c:forEach> <hr> <%-- 遍历Map --%> <% Map<String, User> userMap = new HashMap<String,User>(); userMap.put("user1", user1); userMap.put("user2", user2); userMap.put("user3", user3); userMap.put("user4", user4); pageContext.setAttribute("userMap", userMap); %> <c:forEach items="${pageScope.userMap }" var="user"> <%--获取map中的值--%> ${user }-- ${user.key }-- ${user.value }----${user.value.userName }: ${user.value.password }<br> </c:forEach> <hr> <%--分割处理,分割符号可以设置多个。--%> <%-- <c:forTokens items="" delims=""> --%> <c:forTokens items="a,b,c,d;e-f_g" delims=",;-_" var="item"> ${item } </c:forTokens> <hr> <%-- url相关 重定向可以调到其他的站点,而转发的话,只能转发到当前应用下的某一个页面。--%> <c:redirect> <c:redirect url="jstl_demo2.jsp" context="/day12/"> <c:param name="name" value="javaScript"></c:param> <c:param name="password" value="123456"></c:param> </c:redirect> <c:redirect url="http://www.baidu.com"> <c:param name="name" value="javaScript"></c:param> <c:param name="password" value="123456"></c:param> </c:redirect> <%-- <c:import url=""> <jsp:include page="http://www.baidu.com"></jsp:include> jsp中的包含,只能包含当前应用下的页面,不能包含其他站点的页面。 --%> <%--包含--%> <c:import url="http://www.baidu.com" charEncoding="utf-8"></c:import> <hr> <%-- <c:url> 动态生成url --%> <c:url value="jstl_demo2.jsp" var="url_1" scope="page"> <c:param name="userName" value="${pageScope.user1.userName }"></c:param> <c:param name="password" value="${pageScope.user1.password }"></c:param> </c:url> ${pageScope.url_1 } <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script type="text/javascript"></script> </body></html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ page import="java.util.*"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";pageContext.setAttribute("path",path);request.setCharacterEncoding("utf-8");%><!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>Examples</title> <meta name="description" content=""> <meta name="keywords" content=""> <link href="" rel="stylesheet"> <style type="text/css" media="screen"> </style> </head> <body> <h4> jstl_demo2.jsp页面 </h4> 用户名: ${param.name } <br> 密码: ${param.password } <script src="https://code.jquery.com/jquery-3.2.1.min.js "></script> <script type="text/javascript"> </script> </body></html>
阅读全文
0 0
- jsp_JSTL标签
- java自学之路-----JSP_jstl、el
- 标签
- 标签
- 标签
- 标签
- 标签
- 标签
- 标签
- 标签
- 标签
- 标签
- 标签
- 标签
- 标签
- 标签
- 标签
- 标签
- PyQt5开发DSP SCI串口通讯
- Unity3d 切换场景过度动画
- android 简单的实现视频的播放 基于 ijkplayer的封装更加简单 方便快捷
- python2.7实现简单日记本,兼容windows和linux
- OpenCV_处理视频序列
- jsp_JSTL标签
- TCP面试常见题:张小方的知乎Live:轻松搞定技术面试中常见的网络通信问题
- 17.12.19,web学习第二十九天,还有一年,努力吧青年ajax,cookie,分类分页显示
- 如何将tomcat8的运行日志框架切换到log4j2
- opencv imread读取不了数据
- Logger.getLogger()和LogFactory.getLog()的区别
- 中位数
- 关于java GUI方面用到的知识
- spring5.0解决Log4jConfigListener过期问题