EL和JSTL(1)
##首先看一张图:比较一下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; public Integer 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>