龙空技术网

在jsp里面如何不写java代码展示数据(EL表达式的使用)

滴滴滴程序员 45

前言:

而今我们对“el表达式java”大约比较看重,看官们都需要分析一些“el表达式java”的相关文章。那么小编也在网上汇集了一些对于“el表达式java””的相关资讯,希望各位老铁们能喜欢,看官们一起来学习一下吧!

EL表达式可以简化jsp中写的java代码

EL如果没有获取到值会显示空字符串而不是显示null

<%@ 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>

标签: #el表达式java #el表达式 jsp