EL表达式和JSTL标签

EL表达式:获取数据,在jsp页面可使用 jsppageContext.findAttribute()4nullEL使 {customerBean.address}的形式来访问javaBean对象的属性。
结合JSTL标签,EL表达式也可以获取各种集合如List、Map中的元素。
EL表达式也可使用如${1==1}形式进行简单逻辑判断。
:创建一个javaBean:Person.java

package cn.sun.domain;

import java.util.Date;

public class Person {
    private String name;
    private int age;
    private Date birthday;
    private Address address;
    public Person() {
        super();
    }
    public Person(String name) {
        super();
        this.name = name;
    }       
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}

在Person类中定义了属性address,类型为Address,所以创建Address.java:

package cn.sun.domain;

public class Address {
    private String city;

    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }   
}

在3.jsp中使用EL表达式:

<%@page import="cn.sun.domain.Person"%> 
<%@page import="cn.sun.domain.Address"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


<html>
  <head>   
    <title>My JSP '3.jsp' starting pagetitle>     
  head>

  <body>
    
    <%
        String data="abcd";
        request.setAttribute("data", data);
     %>
     ${data }  
     <hr>

     
     <%
        Person p=new Person();
        p.setName("aaaaaaa");

        request.setAttribute("person", p);
      %>      
      ${person.name }  
      <hr>

      <%
        Person p1=new Person();
        Address a=new Address();
        a.setCity("上海");
        p1.setAddress(a);

        request.setAttribute("p1", p1);
       %>      
       ${p1.address.city }
       <hr>

       
       <%
            List list=new ArrayList();
            list.add(new Person("aaa"));    
            list.add(new Person("bbb"));    
            list.add(new Person("ccc"));

            request.setAttribute("list", list);     
       %>
       ${list[1] }       <br>
       ${list[1].name }
       <hr>

       
       <%
            Map map=new HashMap();
            map.put("aa", new Person("aaaaa"));
            map.put("bb", new Person("bbbbb"));
            map.put("cc", new Person("ccccc"));
            map.put("dd", new Person("ddddd"));
            map.put("111", new Person("eeeee"));       
            request.setAttribute("map1", map);          
        %>          
        ${map1.bb.name}
        ${map1.dd.name }
        ${map1['111'].name } 
        <%--用el表达式取数据时,通常用‘.’号,‘.’号取不出来时,用‘[]’ --%>
        <hr>

                当前web应用的名称 :
        ${pageContext.request.contextPath }  
        <a href="${pageContext.request.contextPath }/index.jsp">点击到首页a>
  body>
html>

运行http://localhost:8080/day09/3.jsp:
EL表达式和JSTL标签_第1张图片


JSTL标签库: 可在页面实现一些简单的逻辑,来替换页面中的脚本代码。
在页面使用JSTL标签需要2个步骤:

  1. 导入jstl.jar和standerd.jar这两个JSTL的jar文件
  2. 在jsp页面使用<%@ taglib uri=”” prefix=”” %>元素导入标签库

JSTL标签库常用标签:

foreach var="" items="">  :迭代
if test=""> :测试某个条件是否成立

jstl和el完成list集合和map集合迭代:

<%@page import="cn.sun.domain.Person"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 

<html>
  <head>
    <title>My JSP '4.jsp' starting pagetitle>   
  head>

  <body>
        <%
            List list=new ArrayList();
            list.add(new Person("aaa"));    
            list.add(new Person("bbb"));    
            list.add(new Person("ccc"));

            request.setAttribute("list", list);     
       %>

       <c:forEach var="person" items="${list}"> 
            ${person.name }<br>
       c:forEach>
       <hr>


       
       <%
            Map map=new HashMap();
            map.put("aa", new Person("aaaaa"));
            map.put("bb", new Person("bbbbb"));
            map.put("cc", new Person("ccccc"));
            map.put("dd", new Person("ddddd"));
            map.put("111", new Person("eeeee"));       
            request.setAttribute("map", map);           
        %>  
        <c:forEach var="entry"  items="${map }"> 
            ${entry.key } : ${ entry.value.name }<br>  
        c:forEach>
        <hr>

        <c:if test="${ user!=null}">    
            欢迎您: ${user.username }
        c:if>

        <c:if test="${ user==null}">    
            用户名:<input type="text">
            密码:<input type="text">
        c:if>
  body>
html>

运行http://localhost:8080/day09/4.jsp:
EL表达式和JSTL标签_第2张图片

你可能感兴趣的:(JSP)