JavaWeb-EL入门

一.EL表达式

  1.EL是JSP内置的表达式语言

  2.从JSP2.0开始,不让再使用java脚本,用EL和动态标签来代替java脚本

  3.EL替代的是java脚本的<%= ....  %>,没错只能用于输出

  4.最常用的是:

    (1)全域查找;比如:request.setAttribute("xxx","XXX"),而通过 ${ xxx }就可以查找到XXX

    (2)指定域查找;比如:${ requestScope.xxx },在request域中查找

  5.JavaBean导航,上代码:

package cn.itcase.domain;

//员工类
public class Employee { private String name; private String age; private Address addr; public Employee() { super(); // TODO Auto-generated constructor stub } public Employee(String name, String age, Address addr) { super(); this.name = name; this.age = age; this.addr = addr; } @Override public String toString() { return "Employee [name=" + name + ", age=" + age + ", addr=" + addr + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Address getAddr() { return addr; } public void setAddr(Address addr) { this.addr = addr; } }
package cn.itcase.domain;

//地址类
public class Address { private String country; private String city; public Address() { super(); // TODO Auto-generated constructor stub } public Address(String country, String city) { super(); this.country = country; this.city = city; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="cn.itcase.domain.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        //设置城市
        Address addr = new Address();
        addr.setCity("广州");
        addr.setCountry("中国");
    
        //设置员工
        Employee emp =  new Employee();
        emp.setName("张三");
        emp.setAge("14");
        emp.setAddr(addr);
        
        //保存到request域中
        request.setAttribute("emp", emp);    
    %>
    
    //最终得到city的值
    ${ requestScope.emp.addr.city }
    
    
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/ibcdwx/p/12325700.html
今日推荐