Day16 --- EL and JSTL

First, the content of today

1. EL表达式
	1. EL表达式概述
	2. EL表达式使用
2. JSTL
	1. jstl概述
	2. 常用的JSTL标签
3. 显示所有用户信息案例

Two, EL expressions

1. EL Expressions Overview
1. 概念:Expression Language 表达式语言
2. 作用:替换和简化jsp页面中java代码的编写
3. 语法:${表达式}
4. 注意:
	* jsp默认支持el表达式的。如果要忽略el表达式
		1. 设置jsp中page指令中设置属性:isELIgnored="true" 忽略当前jsp页面中所有的el表达式
		2. \${表达式} :忽略当前这个el表达式
2. EL expression uses
1. 运算:
		* 运算符:和java的运算符一样
			1. 算数运算符: + - * /(div) %(mod)
			2. 比较运算符: > < >= <= == !=
			3. 逻辑运算符: &&(and) ||(or) !(not)
			4. 空运算符: empty
				* 功能:用于判断字符串、集合、数组对象是否为null或者长度是否为0
				* ${empty list}:判断字符串、集合、数组对象是否为null或者长度为0
				* ${not empty str}:表示判断字符串、集合、数组对象是否不为null 并且 长度>0
2. 获取值
	1. el表达式只能从域对象中获取值
	2. 语法:
		1. ${域名称.键名}:从指定域中获取指定键的值
			* 域名称:
				1. pageScope		--> pageContext
				2. requestScope 	--> request
				3. sessionScope 	--> session
				4. applicationScope --> application(ServletContext)
			* 举例:在request域中存储了name=张三
			* 获取:${requestScope.name}

		2. ${键名}:表示依次从最小的域中查找是否有该键对应的值,直到找到为止。
		
3. 获取对象、List集合、Map集合的值
	1. 对象:${域名称.键名.属性名}
		* 本质上会去调用对象的getter方法
			* 注意:这里是属性名不是类的成员变量。
			* 小结:EL表达式和BeanUtils本质上都是根据类属性名来调用getter/setter方法。所以,当需要写多行代码时,可以构造带get或者set的方法,将代码写在方法里面。比如:
				在jsp页面上有requestScope.user对象, user对象有成员变量brithday:"Tue Jun 12 15:39:11 CST 2019",现在需要显示中文,可以在User类中编写下面方法,再写${requestScope.user.StrDate}:			
					public String getStrDate() {
				        if (brithday != null) {
				            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
				            return sdf.format(this.brithday);
				        } else {
				            return "";
				        }
				    }
	2. List集合:${域名称.键名[索引]}

	3. Map集合:
		* ${域名称.键名.key名称}
		* ${域名称.键名["key名称"]}    * 和python类似

Three, JSTL

1. jstl Overview
1. 概念:JavaServer Pages Tag Library  JSP标准标签库
		* 是由Apache组织提供的开源的免费的jsp标签		<标签>

2. 作用:用于简化和替换jsp页面上的java代码		

3. 使用步骤:
	1. 导入jstl相关jar包
	2. 引入标签库:taglib指令:  <%@ taglib %>  导入含jsp、jstl、core的那个资源
		* <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
	3. 使用标签

jar package download jstl connections required: link: https://pan.baidu.com/s/1OjSsS5Qk0utLJPkkCagwsQ
extraction code: smf1

2. Commonly used JSTL tags
1. if:相当于java代码的if语句
		1. 属性:
            * test 必须属性,接受boolean表达式
                * 如果表达式为true,则显示if标签体内容,如果为false,则不显示标签体内容
                * 一般情况下,test属性值会结合el表达式一起使用
   		2. 注意:
       		 * c:if标签没有else情况,想要else情况,则可以在定义一个c:if标签
		* 举例:
				<c:if test="true">我会被显示</c:if>
2. choose:相当于java代码的switch语句
	1. 使用choose标签声明         			相当于switch声明
       2. 使用when标签做判断         			相当于case
       3. 使用otherwise标签做其他情况的声明    	相当于default
	* 举例:
		<c:choose>
			<c:when test="1">星期一</c:when>
			<c:when test="2">星期二</c:when>
			<c:otherwise>错误</c:otherwise>
		</choose>
3. foreach:相当于java代码的for语句
	1. 完成重复操作:
		1. 属性:
			1. begin:开始值
			2. end:结束值  (左闭右闭,包含结束值)
			3. var:临时变量
			4. step:步长
			5. varStatus:循环状态对象
				1. varStatus.index: 容器中元素的索引(数字)从0开始
				2. varStatus.count:当前循环次数,从1开始
		2. 举例:
			* Java实现
				for (int i = 1; i <=10; i++) {
					System.out.println(i)
				}
			* jstl实现:
				<c:forEach begin="1" end="10" var="i" step="1">
			        ${i} <br>
			    </c:forEach>
	2. 遍历容器:
		1. 属性:
			1. items:容器对象
			2. var:容器中元素的临时变量
			3. varStatus:循环状态对象
				1. varStatus.index: 容器中元素的索引(数字)从0开始
				2. varStatus.count:当前循环次数,从1开始
		2. 举例:
			* java代码:
				for (User user: list) {
			         System.out.println(user.getName());
			    }
			* hstl代码:
				<c:forEach items="list" var="user">
					${user.name}
				</c:forEach>

Third, the display case all user information

demand:

  1. In the field there is a request there User List collection of objects. Requires jstl + el data show the list to set the table in a page table jsp
  2. Each row of the table (except for the header) are alternately background color.

Effect:
Here Insert Picture Description
JSP Code:

<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="cn.wanghao.web.domain.User" %><%--
  Created by IntelliJ IDEA.
  User: Dream^hao`
  Date: 2020/2/11
  Time: 17:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <%
        List list = new ArrayList();
        list.add(new User("张三", "123"));
        list.add(new User("李四", "111"));
        list.add(new User("王五", "333"));
        request.setAttribute("list", list);
    %>

    <table border="1" width="500px">
        <tr>
            <th>编号</th>
            <th>账号</th>
            <th>密码</th>
        </tr>
        <c:forEach items="${list}" var="user" varStatus="s">
            <c:if test="${s.count%2==1}">
                <tr bgcolor="#ffc0cb">
                    <td>${s.count}</td>
                    <td>${user.userName}</td>
                    <td>${user.passwd}</td>
                </tr>
            </c:if>
            <c:if test="${s.count%2==0}">
                <tr bgcolor="#adff2f">
                    <td>${s.count}</td>
                    <td>${user.userName}</td>
                    <td>${user.passwd}</td>
                </tr>
            </c:if>
        </c:forEach>
    </table>

  </body>
</html>
Published 138 original articles · won praise 224 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_43546676/article/details/104311520