EL表達式能夠簡化jsp中寫的java代碼
在jsp裏面如何不寫java代碼展現數據(EL表達式的使用)html
在jsp裏面如何不寫java代碼展現數據(EL表達式的使用)java
在jsp裏面如何不寫java代碼展現數據(EL表達式的使用)session
在jsp裏面如何不寫java代碼展現數據(EL表達式的使用)dom
在jsp裏面如何不寫java代碼展現數據(EL表達式的使用)jsp
EL若是沒有獲取到值會顯示空字符串而不是顯示null
在jsp裏面如何不寫java代碼展現數據(EL表達式的使用)this
在jsp裏面如何不寫java代碼展現數據(EL表達式的使用)orm
在jsp裏面如何不寫java代碼展現數據(EL表達式的使用)htm
在jsp裏面如何不寫java代碼展現數據(EL表達式的使用)對象
在jsp裏面如何不寫java代碼展現數據(EL表達式的使用)字符串
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>el的隱式對象</title>
</head>
<body>
${pageContext.request}<br>
<h4>在jsp頁面動態獲取虛擬目錄</h4>
${pageContext.request.contextPath}<br>
</body>
</html>
package cn.itcast.domain;
import java.text.SimpleDateFormat;
import java.util.Date;
public class User {
private String name;
private int age;
private Date birthday;
/*
* 邏輯視圖
*/
public String getBirStr() {
if(birthday!=null) {
//1.格式化日期對象
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//2.返回字符串便可
return sdf.format(birthday);
}else {
return "";
}
}
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;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>el2獲取域中的數據</title>
</head>
<body>
<%
//在域中存儲數據
request.setAttribute("name", "張三");
session.setAttribute("age", "23");
%>
<h3>el獲取值</h3>
${requestScope.name};
${sessionScope.age};
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="cn.itcast.domain.User" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
User user=new User();
user.setName("張三");
user.setAge(23);
user.setBirthday(new Date());
request.setAttribute("u", user);
List list=new ArrayList();
list.add("aaa");
list.add("bbb");
list.add(user);
request.setAttribute("list", list);
Map map=new HashMap();
map.put("sname","李四");
map.put("gender","男");
map.put("user",user);
%>
<h3>el獲取對象中的值</h3>
${requestScope.u };<br>
<%--
經過的是對象的屬性來獲取
setter或getter方法,去掉set或get,再將剩餘部分,首字母變爲小寫。
setName-->Name-->name
--%>
${requestScope.u.name };<br>
${requestScope.u.age};<br>
${requestScope.u.birthday };<br>
${requestScope.u.birthday.month };<br>
${u.birStr}<br>
<h3>el獲取List的值</h3>
${list};<br>
${list[0]};<br>
${list[1]};<br>
${list[2].name};<br>
<h3>el獲取Map的值</h3>
${map.gender};<br>
${map["gender"]};<br>
<!-- 這裏的user指的是鍵的名稱 -->
${map.user.name};<br>
</body> </html>