用el表达式如何在jsp中获得系统当前时间

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用el表达式如何在jsp中获得系统当前时间相关的知识,希望对你有一定的参考价值。

首先el表达式,只能拿到有带get开头的方法;
所有你可以先用request.setAttribute("date",new Date());
然后在返回的jsp页面上用$date,就能那到服务器上的时间,

如果你仅仅只想在获得本地上的时间,你只要用js就可以获得了
<script>
var date =new Date();
//这里的date 是你打开jsp页面的 本地时间,和服务器时间是有区别的
</script>

还有不懂的地方,可以直接找我聊
参考技术A 1、jsp中获得系统当前时间
代码如下:
<%@ page language="java"%>
<%@ page contentType="text/html; charset=utf-8"%>
<%@ page import="java.util.*"%> //获取系统时间必须导入的
<%@ page import="java.text.*"%> //获取系统时间必须导入的
<%
String datetime=new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); //获取系统时间
request.setAttribute("currentTime",datetime);
%>
2、用el表达式获取时间:
<c:out>$currentTime</c:out>

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

 

  菜鸟教程  传送门

  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}

 

以上是关于用el表达式如何在jsp中获得系统当前时间的主要内容,如果未能解决你的问题,请参考以下文章

jsp中怎么获得当前时间

jsp中怎么获得当前时间?

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

在JSP中如何获得当前日期(急)!!!

EL概述和EL运算符

JSP中 EL表达式 JSTL表达式