EL和JSTL
阿新 • • 發佈:2019-02-26
src ted com xtend doctype amp 年齡 setname 表達
##首先看一張圖:比較一下EL、JSTL和JSP的對應關系
案例:
流程圖
EL表達式的作用:
- 訪問Bean的屬性
- 支持運算
- 獲取請求參數值
package entity; import java.io.Serializable; public class Course implements Serializable { //對象的屬性 private Integer courseId; private String name; private Integer days; publicInteger getId() { return courseId; } public void setId(Integer courseId) { this.courseId = courseId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getDays() {return days; } public void setDays(Integer days) { this.days = days; } }
package entity; import java.io.Serializable; public class Student implements Serializable { private String name; private Integer age; private String sex; private String[] interests;private Course course; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String[] getInterests() { return interests; } public void setInterests(String[] interests) { this.interests = interests; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } }
<%@ page pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>EL/JSTL</title> </head> <body> <h1>EL</h1> <!-- 1.獲取Bean屬性 --> <!-- request.getAttribute("messages").getName() --> <p>姓名:${messages.name}</p> <!-- request.getAttribute("messages").getAge() --> <p>年齡:${messages["age"]}</p> <!-- request.getAttribute("messages").getCourse().getId() --> <p>課程:${messages.course.id}</p> <!-- EL的取值範圍: --> <!-- 1).EL默認從如下4個對象中依次取值 page,request,session,applicate 2).也可以指定取值範圍 requestScope.messages.name sessionScope.messages.name --> <p>性別:${sessionScope.messages.sex }${requestScope.messages.sex }</p> <!-- 2.支持運算 --> <p>年齡+5:${messages.age+5 }</p> <p>年20-30間:${messages.age>20 && messages.age<30 }</p> <p>判空:${empty messages }</p> <!-- 3.獲取請求參數 --> <p>參數:${param.user }</p> </body> </html>
package web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import entity.Course; import entity.Student; public class FindStudentServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { //1.獲取參數 //2.處理業務 Student stu = new Student(); stu.setName("張三"); stu.setAge(18); stu.setSex("男"); stu.setInterests(new String[] {"琴","棋","書","畫"}); Course c = new Course(); c.setId(1); c.setName("java"); c.setDays(80); stu.setCourse(c); System.out.println("dd"); //3.轉發或重定向 req.setAttribute("messages", stu); req.getRequestDispatcher("find_student.jsp").forward(req, res); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>jsp3</display-name> <servlet> <servlet-name>findStudent</servlet-name> <servlet-class>web.FindStudentServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>findStudent</servlet-name> <url-pattern>/findStudent</url-pattern> </servlet-mapping> </web-app>
EL和JSTL