JavaWeb17.2【EL&JSTL:MVC开发模式、EL表达式】
EL表达式
1 <%@ page import="java.util.List" %> 2 <%@ page import="java.util.ArrayList" %><%-- 3 Created by IntelliJ IDEA. 4 User: yubaby 5 Date: 2021/7/3 6 Time: 16:32 7 To change this template use File | Settings | File Templates. 8 --%> 9 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 10 11 <%--<%@ page isELIgnored="true" %>--%> 12 <%--isELIgnored默认值为flase; 13 设为true时会忽略当前页面所有EL表达式, 14 即EL表达式不会被解析,会原样输出在页面--%> 15 16 <html> 17 <head> 18 <title>EL表达式的注意事项和运算符</title> 19 </head> 20 <body> 21 22 ${3 > 4} 23 <%-- 24 http://localhost:8080/day17/el/el1.jsp 25 false 26 --%> 27 \${3 > 4} 28 <%--EL表达式前加\会忽略当前EL表达式,原样输出在页面--%> 29 <hr> 30 31 <h3>算数运算符</h3> 32 ${3 + 4}<br> 33 ${3 - 4}<br> 34 ${3 * 4}<br> 35 ${3 / 4}<br> 36 ${3 div 4}<br> 37 ${3 % 4}<br> 38 ${3 mod 4}<br> 39 <hr> 40 <%-- 41 7 42 -1 43 12 44 0.75 45 0.75 46 3 47 3 48 --%> 49 50 <h3>算数运算符</h3> 51 ${3 == 4}<br> 52 ${3 != 4}<br> 53 <hr> 54 <%--false 55 true--%> 56 57 <h3>逻辑运算符</h3> 58 ${3>4 && 3<4}<br> 59 ${3>4 and 3<4}<br> 60 ${3>4 || 3<4}<br> 61 ${3>4 or 3<4}<br> 62 ${!(3>4)}<br> 63 ${not(3>4)}<br> 64 <hr> 65 <%--false--%> 66 <%--false--%> 67 <%--true--%> 68 <%--true--%> 69 <%--true--%> 70 <%--true--%> 71 72 73 <h3>empty运算符</h3> 74 <% 75 String str = "abcd"; 76 String str1 = null; 77 String str2 = ""; /*空字符串长度为0*/ 78 request.setAttribute("str", str); 79 request.setAttribute("str1", str1); 80 request.setAttribute("str2", str2); 81 82 List list = new ArrayList(); 83 request.setAttribute("list", list); 84 %> 85 ${empty str} 86 ${empty str1} 87 ${empty str2} 88 <%--false true true--%> 89 ${not empty str} 90 <%--true--%> 91 ${not empty list} 92 <%--false--%> 93 </body> 94 </html>
1 <%-- 2 Created by IntelliJ IDEA. 3 User: yubaby 4 Date: 2021/7/3 5 Time: 18:12 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <title>EL获取域中的数据</title> 12 </head> 13 <body> 14 15 <% 16 //在域中存储数据 17 session.setAttribute("name", "李四"); 18 request.setAttribute("name", "张三"); 19 session.setAttribute("age", "24"); 20 %> 21 22 <h3>EL表达式获取值</h3> 23 ${requestScope.name} 24 ${sessionScope.age} 25 ${sessionScope.name} 26 ${sessionScope.heihei} <%--有值则输出到页面,无值返回空字符串""--%> 27 28 ${name} <%--张三--%> <%--依次从最小的域中查找是否有该键对应的值,直到找到为止--%> 29 30 </body> 31 </html>
1 package com.haifei.domain; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 public class User { 7 private String name; 8 private int age; 9 private Date birthday; 10 11 /** 12 * 逻辑视图 13 * (并没有专门对应的成员属性birthdayStr,而仅仅是处理一下birthday方便页面展示) 14 * @return 15 */ 16 public String getBirthdayStr(){ 17 //格式化日期对象 18 if (birthday != null){ 19 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 20 /*String birthdayStr = sdf.format(birthday); 21 return birthdayStr;*/ 22 return sdf.format(birthday); 23 }else{ 24 return ""; 25 } 26 } 27 28 public String getName() { 29 return name; 30 } 31 32 public int getAge() { 33 return age; 34 } 35 36 public Date getBirthday() { 37 return birthday; 38 } 39 40 public void setName(String name) { 41 this.name = name; 42 } 43 44 public void setAge(int age) { 45 this.age = age; 46 } 47 48 public void setBirthday(Date birthday) { 49 this.birthday = birthday; 50 } 51 }
1 <%@ page import="com.haifei.domain.User" %> 2 <%@ page import="java.util.*" %> 3 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 4 <html> 5 <head> 6 <title>EL表达式获取对象、List集合、Map集合的值</title> 7 </head> 8 <body> 9 10 <% 11 User user = new User(); 12 user.setName("zhansan"); 13 user.setAge(23); 14 user.setBirthday(new Date()); 15 request.setAttribute("u", user); 16 17 List list = new ArrayList(); 18 list.add("a"); 19 list.add("bb"); 20 list.add("ccc"); 21 list.add(user); 22 request.setAttribute("list", list); 23 24 Map map = new HashMap(); 25 map.put("sname", "李四"); 26 map.put("gender", "男"); 27 map.put("uu", user); 28 request.setAttribute("map", map); 29 %> 30 31 32 <h1>获取对象中的值</h1> 33 ${requestScope.u}<br> <%--com.haifei.domain.User@4cd12ab8--%> 34 ${u}<br> <%--com.haifei.domain.User@4cd12ab8--%> 35 36 <%-- 37 * 通过对象的属性来获取 38 * setter或getter方法,去掉set或get,再将剩余部分的首字母变为小写 39 * setName --> Name --> name 40 * 即 是通过setter或getter方法得到属性,而不是直接通过成员变量 41 --%> 42 ${requestScope.u.name}<br> 43 ${u.age}<br> 44 ${u.birthday}<br> 45 ${u.birthday.year + 1900}<br> <%--year默认减去了元年1900--%> 46 ${u.birthday.month + 1}<br> <%--month月份从0开始--%> 47 ${u.birthdayStr}<br> 48 <hr> 49 50 51 <h1>获取List中的值</h1> 52 ${list}<br> <%--[a, bb, ccc]--%> 53 ${list[0]}<br> 54 ${list[1]}<br> 55 ${list[5]}<br> <%--EL表达式中下标越界不会报错,返回空字符串""--%> 56 ${list[3].name}<br> 57 <hr> 58 59 60 <h1>获取Map中的值</h1> 61 ${map}<br> 62 ${map.sname}<br> 63 ${map["gender"]}<br> 64 ${map.uu.name} 65 </body> 66 </html>
1 <%-- 2 Created by IntelliJ IDEA. 3 User: yubaby 4 Date: 2021/7/3 5 Time: 12:32 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <title>Title</title> 12 13 <script> 14 window.onload = function () { 15 document.getElementById("img").onclick = function () { 16 var timeStamp = new Date().getTime(); 17 this.src = "/day16/checkCodeServlet?time=" + timeStamp; 18 //this即通过document.getElementById("img")获取到的对象 19 } 20 } 21 </script> 22 <style> 23 div{ 24 color: red; 25 } 26 </style> 27 </head> 28 <body> 29 30 <%--<form action="/day16/LoginServlet" method="post">--%> 31 <form action="${pageContext.request.contextPath}/LoginServlet" method="post"> 32 <table> 33 <tr> 34 <td>用户名</td> 35 <td><input type="text" name="username"></td> 36 </tr> 37 <tr> 38 <td>密码</td> 39 <td><input type="password" name="password"></td> 40 </tr> 41 <tr> 42 <td>验证码</td> 43 <td><input type="text" name="checkCode"></td> 44 </tr> 45 <tr> 46 <td colspan="2"><img id="img" src="/day16/checkCodeServlet"></td> 47 </tr> 48 <tr> 49 <td colspan="2"><input type="submit" value="登录"></td> 50 </tr> 51 </table> 52 </form> 53 54 <%--<div><%=request.getAttribute("cc_error")==null ? "" : request.getAttribute("cc_error")%></div>--%> 55 <%--<div><%=request.getAttribute("login_error")==null ? "" : request.getAttribute("login_error")%></div>--%> 56 ${requestScope.cc_error} 57 ${requestScope.login_error} 58 59 </body> 60 </html>
1 <%-- 2 Created by IntelliJ IDEA. 3 User: yubaby 4 Date: 2021/7/3 5 Time: 12:32 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <title>Title</title> 12 13 <script> 14 window.onload = function () { 15 document.getElementById("img").onclick = function () { 16 var timeStamp = new Date().getTime(); 17 this.src = "/day16/checkCodeServlet?time=" + timeStamp; 18 //this即通过document.getElementById("img")获取到的对象 19 } 20 } 21 </script> 22 <style> 23 div{ 24 color: red; 25 } 26 </style> 27 </head> 28 <body> 29 30 <%--<form action="/day16/LoginServlet" method="post">--%> 31 <form action="${pageContext.request.contextPath}/LoginServlet" method="post"> 32 <table> 33 <tr> 34 <td>用户名</td> 35 <td><input type="text" name="username"></td> 36 </tr> 37 <tr> 38 <td>密码</td> 39 <td><input type="password" name="password"></td> 40 </tr> 41 <tr> 42 <td>验证码</td> 43 <td><input type="text" name="checkCode"></td> 44 </tr> 45 <tr> 46 <td colspan="2"><img id="img" src="/day16/checkCodeServlet"></td> 47 </tr> 48 <tr> 49 <td colspan="2"><input type="submit" value="登录"></td> 50 </tr> 51 </table> 52 </form> 53 54 <%--<div><%=request.getAttribute("cc_error")==null ? "" : request.getAttribute("cc_error")%></div>--%> 55 <%--<div><%=request.getAttribute("login_error")==null ? "" : request.getAttribute("login_error")%></div>--%> 56 ${requestScope.cc_error} 57 ${requestScope.login_error} 58 59 </body> 60 </html>