本文主要是介绍Java基础入门day58,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
day58
EL表达式
概念
EL:expression language
EL使得JSP写起来更简单、简介,主要用于获取作用域中的数据
语法
可以使用${}获取指定作用域中的数据
用于替换作用域.getAttribute("name")
${scope.name}获取指定的某个作用域中的数据
${name}获取作用域中的数据,安装作用域从小到大的顺序获取(pageContext, request, session, application)
EL和JSP脚本的区别
<%=request.getAttribute(“name”)>如果没有找到name, 则返回为null值
${requestScope.name}没找到则返回“”
应用
<%@ page import="com.saas.entity.Student" %><%-- Created by IntelliJ IDEA. User: Administrator Date: 2024/5/24 Time: 9:08 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>EL</title> </head> <body> <%Student s = new Student(); s.setId("9527");s.setName("zhouxingxing");s.setPassword("123456");s.setAge(18);s.setSex(true);s.setAddress("huafu");s.setEmail("zhouxing@qq.com");s.setPhone("110");s.setScore(100.0); pageContext.setAttribute("s", s); %> <%--EL表达式1. 获取对象中的属性值2. 获取对象的方法3. 获取数组中的元素4. 获取集合中的元素5. 获取request域中的属性6. 获取session域中的属性7. 获取application域中的属性8. 获取cookie9. 获取ServletContext10. 获取ServletRequest11. 获取ServletResponse12. 获取ServletConfig13. 获取Servlet14.获取ServletContext --%> <form action="ELServlet" method="post">id:<input type="text" name="id" value="${s.id}"><p />name:<input type="text" name="name" value="${s.name}"><p />age:<input type="text" name="age" value="${s.age}"><p />password:<input type="text" name="password" value="${s.password}"><p />score:<input type="text" name="score" value="${s.score}"><p />email:<input type="text" name="email" value="${s.email}"><p />sex: <input type="radio" name="sex" value="true" checked="checked">男phone:<input type="text" name="phone" value="${s.phone}"><p />address:<input type="text" name="address" value="${s.address}"><p /><input type="submit" value="submit"> </form> </body> </html><%@ page import="com.saas.entity.Student" %><%--Created by IntelliJ IDEA.User: AdministratorDate: 2024/5/24Time: 9:08To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>EL</title> </head> <body> <%int[] arrs = new int[]{100, 22, 333, 44, 505}; pageContext.setAttribute("arrs", arrs); %> arrs[0] = ${arrs[0]} arrs[1] = ${arrs[1]} arrs[2] = ${arrs[2]} arrs[3] = ${arrs[3]} arrs[4] = ${arrs[4]} <hr /> </body> </html><%@ page import="com.saas.entity.Student" %> <%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %><%--Created by IntelliJ IDEA.User: AdministratorDate: 2024/5/24Time: 9:08To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>EL</title> </head> <body> <%List<Student> stus = new ArrayList<>(); stus.add(new Student("9527", "zhouxingxing", "123456", 19, true, "huafu", "zhouxing@qq.com", "110", 100.0));stus.add(new Student("9528", "qiuxiang", "123456", 18, false, "huafu", "qiuxiang@qq.com", "110", 100.0));stus.add(new Student("9529", "shiliujie", "123456", 20, false, "huafu", "zhouxing@qq.com", "110", 100.0));stus.add(new Student("9530", "chuanxiang", "123456", 18, false, "huafu", "chuanxiang@qq.com", "110", 100.0));stus.add(new Student("9531", "jiangjun", "123456", 38, true, "huafu", "zhouxing@jiangjun.com", "110", 100.0)); pageContext.setAttribute("stus", stus); %> <table border="1"><tr><td>id</td><td>name</td><td>password</td><td>age</td><td>sex</td><td>address</td><td>phone</td><td>email</td><td>score</td><td>操作</td></tr><tr><td>${stus[0].id}</td><td>${stus[0].name}</td><td>${stus[0].password}</td><td>${stus[0].age}</td><td>${stus[0].sex}</td><td>${stus[0].address}</td><td>${stus[0].phone}</td><td>${stus[0].email}</td><td>${stus[0].score}</td><td><a href="">修改</a><a href="">删除</a></td></tr> </table> <hr /> </body> </html><%@ page import="com.saas.entity.Student" %> <%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.Map" %> <%@ page import="java.util.HashMap" %><%--Created by IntelliJ IDEA.User: AdministratorDate: 2024/5/24Time: 9:08To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>EL</title> </head> <body> <%Map<String, String> names = new HashMap<>(); names.put("jack", "杰克");names.put("rose", "肉丝儿"); pageContext.setAttribute("names", names); %> jack : ${names["jack"]} </body> </html><%--Created by IntelliJ IDEA.User: AdministratorDate: 2024/5/24Time: 9:33To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>EL</title> </head> <body> <%request.setAttribute("num", 1234); %> <h3>算数运算</h3> num + 1 = ${num + 1}<br /> num - 1 = ${num - 1}<br /> num * 2 = ${num * 2}<br /> num / 2 = ${num / 2}<br /> num / 1 = ${num div 2}<br /> num % 2 = ${num % 2}<br /> num % 2 = ${num mod 2}<br /> <h3>关系运算</h3> num > 1 = ${num > 1}<br /> num < 1 = ${num < 1}<br /> num >= 1 = ${num >= 1}<br /> num <= 1 = ${num <= 1}<br /> num == 1 = ${num == 1}<br /> num != 1 = ${num != 1}<br /> <h3>逻辑运算</h3> num > 1 && num < 10 = ${num > 1 && num < 10}<br /> num > 1 || num < 10 = ${num > 1 || num < 10}<br /> !(num > 1) = ${!(num > 1)}<br /> num > 1 and num < 10 = ${num > 1 and num < 10}<br /> num > 1 or num < 10 = ${num > 1 or num < 10}<br /> !(num > 1) = ${!(num > 1)}<br /> num > 1 and num < 10 = ${num > 1 and num < 10}<br /> num > 1 or num < 10 = ${num > 1 or num < 10}<br /> </body> </html>
JSTL
现有问题
EL表达式主要用于获取作用域中的数据,虽然可以做运算判断,但是得到的都是一个结果,用来做展示
EL不存在流程控制,比如无法做判断
EL表达式可以获取集合中的数据,但是也仅限于获取集合中的某一个元素,但是不能实现遍历操作,比如循环
概念
JSTL: JSP standard tag library: JSP的标准标签库
JSTL是一个JSP的标签集合
作用
可对EL获取的数据进行逻辑操作
与EL合作完成数据的展示
使用
需要两个jar包:standard.jar和jstl.jar
在页面中引入核心标签库
在页面中使用JSTL
核心标签
if条件判断
语法:
<c:if test='条件'></c:if>如果test中的条件成立,执行c:if中的代码,否则不执行里面的代码
<c:if test='${8 > 2}'>8 > 2成立</c:if> <c:if test='${8 < 2}'>8 < 2成立</c:if><%-- Created by IntelliJ IDEA. User: Administrator Date: 2024/5/24 Time: 11:25 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>jstl</title><%@ taglib prefix="ccc" uri="http://java.sun.com/jsp/jstl/core" %> </head> <body><ccc:if test="${8 > 2}">8 > 2 成立</ccc:if><ccc:if test="${8 < 2}">8 < 2 成立</ccc:if> </body> </html>以上结果中,由于8 > 2判断成立,则第一个if里面的代码将执行
8 < 2 不成立,则第二个if里面的代码将 不会执行
多条件判断choose when
语法:
<c:choose><c:when test="条件1"结果1</c:when><c:when test="条件2>结果2</c:when><c:when test="条件3>结果3</c:when><c:otherwise>结果4</c:otherwise> </c:choose><%-- Created by IntelliJ IDEA. User: Administrator Date: 2024/5/24 Time: 11:30 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>jstl</title><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> </head> <body> <c:set var="score" value="100" scope="page" /> <c:choose><c:when test="${score >= 90}">优秀</c:when><c:when test="${score >= 80}">良好</c:when><c:when test="${score >= 60}">及格</c:when><c:otherwise>不及格</c:otherwise> </c:choose> </body> </html>
迭代foreach
语法:
<c:forEach var="i" begin="1" end="10" step="1" items="">循环代码 </c:forEach>var: 变量名
begin:起始下标
end:结束下标
step:间隔长度
items:要遍历的集合
<%@ page import="com.saas.entity.Student" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.List" %><%-- Created by IntelliJ IDEA. User: Administrator Date: 2024/5/24 Time: 11:30 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>jstl</title><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> </head> <body> <%int[] arr = new int[]{1, 2, 3, 4, 5, 99, 33, 11, 7};pageContext.setAttribute("arr", arr);List<Student> stus = new ArrayList<>();stus.add(new Student("9527", "zhouxingxing", "123456", 19, true, "huafu", "zhouxing@qq.com", "110", 100.0));stus.add(new Student("9528", "qiuxiang", "123456", 18, false, "huafu", "qiuxiang@qq.com", "110", 100.0));stus.add(new Student("9529", "shiliujie", "123456", 20, false, "huafu", "zhouxing@jiangjun.com", "110", 100.0));stus.add(new Student("9530", "chuanxiang", "123456", 18, false, "huafu", "chuanxiang@qq.com", "110", 100.0));stus.add(new Student("9531", "jiangjun", "123456", 38, true, "huafu", "zhouxing@jiangjun.com", "110", 100.0));pageContext.setAttribute("stus", stus); %> <c:forEach var="i" begin="2" end="6" step="2" items="${arr}">${i}<p /> </c:forEach><hr><c:forEach items="${stus}" var="s">${s.id}${s.name}${s.password}${s.age}${s.sex}${s.address}${s.phone}${s.email}${s.score}<hr /> </c:forEach> </body> </html>循环items中的元素,每一个元素将用var所对应的变量接收
这篇关于Java基础入门day58的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!