JSP的EL表达式应用(IDEA)

霸气de小男生 提交于 2020-01-24 15:57:06

JSP的设计目的就是为了解决Servlet在视图层的复杂应用,因此要尽可能的少出现java代码,EL(Expression Language)表达式的出现很好的解决了这个问题:

格式:${表达式}

1.基本用法

新建一个实体类User

package com.qianqian.practice.entity;

/**
 * 用户实体
 */
public class User {
    private String userId;
    private String userName;
    private String userPassword;
    private String userGender;

    public User(String userId, String userName, String userPassword, String userGender) {
        this.userId = userId;
        this.userName = userName;
        this.userPassword = userPassword;
        this.userGender = userGender;
    }    
}

然后再TestServlet.java新建对象并转发

package com.qianqian.practice.servlet;

import com.qianqian.practice.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "TestServlet", value = "/test")
public class TestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = new User("4646794654", "小可爱", "4455455", "女");
        request.setAttribute("user1", user);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
}

接下来就要在index.jsp中来接受此对象了,如下:

<%@ page import="com.qianqian.practice.entity.User" %><%--
  Created by IntelliJ IDEA.
  User: Swing
  Date: 2020/1/20
  Time: 9:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<html>
<head>
    <title>Expression Language</title>
</head>
<body>
<%--如果未声明作用域,则更具作用域从小到大查找--%>
userId:${user1.userId}<br>
userName:${user1.userName}<br>
userPassword:${user1.userPassword}<br>
<%--如果声明了作用域,则在此作用域中查找--%>
userGender:${requestScope.user1.userGender}<br>
</body>
</html>

2.常用表达式

基本表达式与Java类似,如下举例:

算术运算
	${10+20 }<br/>
	${10-20 }<br/>
	${10*20 }<br/>
	${3%5}<br/>
	关系运算
	${10==9 }<br/>
	${"abc"=="abc" }<br/>
	${s1==s2 } equals<br/>
	${s1 eq s2 }<br/>
	${10>20}<br/>
	${10 gt 20}<br/>
	${5 < 10}<br/>
	${5 lt 10}<br/>
	${5 != 10}<br/>
	${5 ne 10}<br/>
	

	三目运算
	${score>80?"优秀":"一般" }<br/>  
	${flag==0?"没有激活":flag==1?"激活":"删除" }<br/>  
	${sex==0?"男":"女" }<br/>  
	
     逻辑运算
	${true &&  false}<br/> 
	${true || false}<br/> 
	${!true}<br/> 
	${true and  false}<br/> 
	${true or false}<br/> 
	${not true}<br/> 

此处要特别注意empty关键字

<%
    String s1 = "";
    String s2 = null;
    String s3 = "122222";
    List list1 = new ArrayList();

    pageContext.setAttribute("s1", s1);
    pageContext.setAttribute("s2", s2);
    pageContext.setAttribute("s3", s3);
    pageContext.setAttribute("list1", list1);
%>
<!-- empty关键只要内容是空true -->
${empty s1}<br>
${empty s2}<br>
${empty  s3}<br>
${empty  list1}<br>

3.EL表达式操作集合

<%
    List<String> stringList=new ArrayList<>();
    stringList.add("a");
    stringList.add("b");
    stringList.add("c");
    application.setAttribute("list",stringList);

    Map<String,String> stringStringMap=new HashMap<>();
    stringStringMap.put("A","abstract");
    stringStringMap.put("B","bool");
    stringStringMap.put("C","character");
    application.setAttribute("map",stringStringMap);
%>
List:<br>
${list}<br>
${list[0]}<br>
${list.get(1)}<br>

<br>Map:<br>
${map}<br>
${map.keySet()}<br>
${map.values()}<br>
${map.A}<br>
${map["B"]}<br>

4.EL表达式的隐式对象

大部分对应的是JSP的内置对象,由于不提倡在JSP中过多的使用Java代码,所有将内置对象映射到EL表达式的隐式对象

pageContext:页面上下文,可以获取jsp中其他八个内置对象

pageScope、requestScope、sessionScope、applicationScope表示四个域对象中集合

param 表示request.getParameter("username"); ${param.username}

paramvalues 表示request.getPrameterValues("hobby"); ${paramValues.hobby}

header 表示 request.getHeader("accept"); ${header.accept};

headerValues 表示 request.getHeaderValues("accept-encoding"); ${headerValues"accept-encoding"}

cookie 表示 request.getCookies(); ${cookie.JSESSIONID}

initParam 表示 ServletContext(application)初始化参数

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!