JavaWeb - EL and JSTL

1.EL expression

1.1.EL concept

  • EL:Expression Language,表达式语言
  • 作用: Replace and simplify JSP pages written in Java code

1.2.EL grammar

${表达式}
Here Insert Picture Description


!!! Note: JSP默认支持EL, so if you want to output "3> 4" string style how to do it? There are two ways:

  • 使用isELIgnoreBut this approach would be that all the pages of EL expressions are ignored
    Here Insert Picture Description
  • 使用“\”忽略个别
    Here Insert Picture Description

1.3.EL use

1.3.1. Operation

Operator:

  • Arithmetic operators: +, -, *, / (div),% (mod)
  • Comparison operators:>, <, =,> =, <=, ==,! =
  • Logical operators: && (and), || (or), | (not)
    Here Insert Picture Description
  • Air operators: empty
  • $ {Empty list}: for判断字符串、集合、数组对象是否为null或者长度是否为0
  • $ {Not empty str}: Analyzing string represented, set array object is not null and the length is greater than 0

1.3.2. Getting value

!!! Note: EL表达式只能从域对象中获取值
Syntax:

  • 1. $ {domain name} key city:从指定域中获取指定的键值

Domain Name City:

  • pageContext:pageScope
  • request:requestScope
  • session:sessionScope
  • application(ServletContext):applicationScope
    Here Insert Picture Description
  • 2 $ {name} key: indicates a从最小的域中去查找是否有该键对应的值,直到找到为止
  • 3. Get 对象、List集合、Map集合value
  • Target: $ {. The domain name of the city of key attribute name}

go getter method calls the object's essence

  • List collection: $ {domain key city name [index]}
  • Map set: $ {name} .key domain key name or $ {name domain key [ "key name"]}

java.class

package xpu.edu.web.servlet.EL;

import java.util.Date;

public class User {
    private String name;
    private int age;
    private Date birthday;

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

JSP page (Markdown seemingly does not support JSP code highlighting, all a bit ugly, will look)

<%@ page import="xpu.edu.web.servlet.EL.User" %>
<%@ page import="java.util.*" %><%--
 Created by IntelliJ IDEA.
 User: 15291
 Date: 2019/6/4
 Time: 21:27
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
   <title>获取数据</title>
</head>
<body>

<%
   User user = new User();
   user.setAge(18);
   user.setName("邹鸡儿");
   user.setBirthday(new Date());

   request.setAttribute("u",user);

   List list = new ArrayList();
   list.add("邹鸡儿");
   list.add("邹大炮");

   request.setAttribute("list",list);

   Map hashMap = new HashMap();
   hashMap.put("name","邹鸡儿");
   hashMap.put("age","19");

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

<h3>获取对象中的值</h3>
${requestScope.u}<br/>

<%--
   通过对象的属性来获取
   * setter或getter方法,去掉set或get,在剩余部分首字母变小写
   * setName——>Name——>name
--%>

${requestScope.u.name}<br/>
${requestScope.u.age}<br/>
${requestScope.u.birthday}<br/>

<h3>获取List的值</h3>
${requestScope.list}<br/>
${requestScope.list[0]}
${requestScope.list[1]}
<%--
   越界时会输出空字符串""
--%>
${requestScope.list[10]}

<h3>获取Map的值</h3>
${requestScope.hashMap.name}<br/>
${requestScope.hashMap["name"]}

</body>
</html>

Here Insert Picture Description

1.4.EL implicit target

EL表达式有11个隐式对象, Focus on understanding pageContext: 可以获取JSP其他八个内置对象
$ {} pageContext.request.contextPath: dynamically acquire virtual directory
Here Insert Picture Description

2.JSTL

2.1.JSTL concept

  • JSTL: JavaServer Pages Tag Library, JSP Standard Tag Library is a free open source JSP tags provided by the Apache organization
  • effect:用于简化和替换JSP页面的Java代码

2.2.JSTL use the steps

  • Related jar package 1. Import
  • 2. Introduction tag library: taglib directive: <% taglib%>
  • 3. Use tag

2.3. Commonly used labels JSTL

  • if: The equivalent Java code if statement
  • Properties: test as an essential attribute, receiving boolean expression, if the expression is true, the tag is displayed if the content thereof, if false, the content is not displayed tag body
  • note:一般test属性会结合EL表达式一起使用
    Here Insert Picture Description
  • choose: The equivalent Java code switch statement
  • Choose to use the label statement: the equivalent switch statement
  • Label use when making a judgment: the equivalent case
  • Otherwise use the label to do otherwise declare: the equivalent of d
    Here Insert Picture Description
  • foreach: The equivalent Java code for statement

1. Complete Repeat

Properties:
1.begin: start value
2.end: end value
3.var: temporary variables
4.step: step
5.varStatus: circulation state of the object

index: the index container element, from zero
count: number of cycles, starting with 1

2. traverse the container

Properties:
1.item: container objects
2.var: temporary variable elements in the container
3.varStatus: circulation state of the object

index: the index container element, from zero
count: number of cycles, starting with 1

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/LiLiLiLaLa/article/details/90815272