码迷,mamicode.com
首页 > 编程语言 > 详细

JavaWeb_EL表达式存储数据及获得项目路径

时间:2019-01-23 14:33:02      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:hash   传送门   分享图片   分享   ssi   info   str   判断   this   

 

  菜鸟教程  传送门

  EL表达式【百度百科】:EL(Expression Language) 是为了使JSP写起来更加简单。表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 JSP 中简化表达式的方法,让Jsp的代码更加简化

 

  EL表达式获得当前项目路径可以通过Page作用域去获取

${pageContext.request.contextPath} 

 

 

操作简单数据

技术分享图片

技术分享图片
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
作用域:page request session application
<hr>
<%
request.setAttribute("number",2000);
session.setAttribute("use","Hello Gary!!!");
application.setAttribute("string","nihao");

//同名情况    直接使用${wa}只会从request->session->application
request.setAttribute("wa",1);
session.setAttribute("wa",2);
application.setAttribute("wa",3);
%>

request    :${requestScope.number}<br>
session :${sessionScope.use}<br>
application :${applicationScope.string}<br><br>

${wa}<br>

<%
request.removeAttribute("wa");    //将request域中"wa"给删除
%>

${wa}<br>
</body>
</html>
el.jsp

 

输出:

作用域:page request session application 
request :2000
session :Hello Gary!!!
application :nihao

 1
 2

 

 

el表达式从map集合和对象里面取得数据

技术分享图片

 

技术分享图片
package com.Gary.model;

public class User {
    private String username;
    private String password;
    private int age;
    private String sex;
    private boolean isAdmin=false;    //判断是否是管理员账号
    
    public User(String username, String password, int age, String sex, boolean isAdmin) {
        super();
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
        this.isAdmin = isAdmin;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public boolean isAdmin() {
        return isAdmin;
    }
    public void setAdmin(boolean isAdmin) {
        this.isAdmin = isAdmin;
    }
    
}
User.java

 

技术分享图片
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="com.Gary.model.User"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
作用域:page request session application
<hr>

<%
User u = new User("Gary","123",20,"",false);
request.setAttribute("user",u);
%>

${user.getUsername()}    <br>
${user.username}    <br>

<hr>
<!-- 从Map中存储数据 -->
<%
Map<String,String>map = new HashMap<String,String>();
map.put("name","Gary2");
map.put("age","12");
request.setAttribute("user2",map);
%>

${user2.name}<br>
${user2.age}<br>

</body>
</html>
el.jsp

 

输出:

作用域:page request session application 
Gary 
Gary 

Gary2
 12

 

  如果要使用 ${user.username} 去替代 ${user.getUsername()} 那么User.java中必须要存在 getUsername() 方法

 

el表达式从list集合和对象里面取得数据

技术分享图片

 

技术分享图片
package com.Gary.model;

public class User {
    private String username;
    private String password;
    private int age;
    private String sex;
    private boolean isAdmin=false;    //判断是否是管理员账号
    
    public User(String username, String password, int age, String sex, boolean isAdmin) {
        super();
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
        this.isAdmin = isAdmin;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public boolean isAdmin() {
        return isAdmin;
    }
    public void setAdmin(boolean isAdmin) {
        this.isAdmin = isAdmin;
    }
    
}
User.java

 

技术分享图片
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="com.Gary.model.User"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
作用域:page request session application
<hr>

<%
List<User> list = new ArrayList<User>();
list.add(new User("Gary0","0",10,"",false));
list.add(new User("Gary1","1",20,"",false));
list.add(new User("Gary2","2",30,"",false));
list.add(new User("Gary3","3",40,"",false));
list.add(new User("Gary4","4",50,"",false));
request.setAttribute("list", list);
%>

${list[2].username}    <br>
${list[4].username}    <br>

加减运算    ${list[2].age+list[4].age}    <br>

判空list ${empty list}    <br>
判空list2 ${empty list2}    <br>

${pageContext.request.contextPath} <br>
</body>
</html>
el.jsp

 

输出

作用域:page request session application 
Gary2 
 Gary4 
加减运算 80 
判空list false 
判空list true 
 /Gary04 

 

  获得当前项目路径可以通过EL表达式Page作用域去获取

${pageContext.request.contextPath}

 

JavaWeb_EL表达式存储数据及获得项目路径

标签:hash   传送门   分享图片   分享   ssi   info   str   判断   this   

原文地址:https://www.cnblogs.com/1138720556Gary/p/10308641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!