EL表达式

src/com 下有User.java

package com;

public class User {
private String username;
private String password;
private String email;
private String ipcard;
private String right;
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getIpcard() {
return ipcard;
}
public void setIpcard(String ipcard) {
this.ipcard = ipcard;
}
public String getRight() {
return right;
}
public void setRight(String right) {
this.right = right;
}
}




<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.User"%>
<%@ page isELIgnored ="false" %>
<%
pageContext.setAttribute("pageValue","page");
request.setAttribute("requestValue","request");
session.setAttribute("sessionValue","session");
application.setAttribute("applicationValue","application");

User myuser = new User();
myuser.setUsername("my");
myuser.setPassword("123456");
pageContext.setAttribute("myuser",myuser);

int i [] = {1,2,3};
pageContext.setAttribute("array",i);

ArrayList al = new ArrayList();
al.add(new Date());
al.add("hello world");
al.add(myuser);
pageContext.setAttribute("al",al);

HashMap hm = new HashMap();
hm.put("ex",new Exception("exception"));
hm.put("num",Integer.valueOf(123));
hm.put("user",myuser);
pageContext.setAttribute("hm",hm);
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<jsp:useBean id="user" class = "com.User" scope = "page">
</jsp:useBean>
<jsp:setProperty property="username" name="user" value ="jack" />
<jsp:setProperty property="password" name="user" value ="255262" />

<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>

el表达式类似于表达式的功能。可以在页面直接显示一个语句的结果
也可以作为一个值来使用<br>
<%-- ${} <%= %> --%>
功能:<br>
<h3>1.使用el表达式访问4个作用范围内的属性</h3><br>
<%
String str = pageContext.getAttribute("pageValue").toString();
%>
<%=str%>
${pageValue}<br>

指定范围的搜索<br>
${requestScope.requestValue}<br>
绑定的是自定义对象<br>
${myuser.username}<br>
<h3>2.使用el表达式直接访问javaBean中的属性(实质也是访问作用范围内的属性)</h3><br>
${user.username} ${user.password}<br>

<h3>3.el表达式中的隐式对象:(多数用来处理请求中的参数)</h3><br>
(1)param获得请求中的某一个参数:<br>
${param.arg} ${param.arg2} <br>
(2)paramValues获得请求中的同名多个参数:(复选框)<br>
${paramValues.check[0]} ${paramValues.check[1]} ${paramValues.check[2]}<br>
(3)4个作用域范围对象<br>
pageScope requestScope sessionScope applicationScope<br>

(4)获得请求报文中的属性<br>
${header.host} ${header["user-agent"]}<br>
<h3>4.访问类型为数组,集合的属性:</h3><br>

访问数组元素:${array[0]} ${array[1]} ${array[2]} <br>
访问list集合:${al[0]} ${al[1]} ${al[2].username} <br>
访问Map集合: ${hm.ex} ${hm["num"]} ${hm.user.username} <br>

<h3>注:“[]”和“.” 都可以用来获得某个对象的属性,或者应用于操作集合:</h3><br>
从作用域中获取属性[]和“.”用法一致:${sessionScope["sessionValue"] } <br>
操作javaBean[]和“.”用法一致:${myuser["username"]} ${myuser["password"]} <br>
el隐式对象param获得请求中的某一个参数[]和“.”用法一致:${param["arg"] }<br>
paramValues获得请求中的同名多个参数[]和“.”用法一致:${paramValues["check"][0] }<br>
map集合[]和“.”用法一致:<br>
<br>
数组,list集合只能使用[]: <br>
<br>
对于某些隐式对象中包含的属性名有特殊字符只能用[]:<br>

<br>
注: 操作对象时“.”号操作符只能点属性,不能使用对象中的方法

<br>
<h3>6.操作符:(表达式语言中可以写一个算数表达式,值为该算数表达式的结果,el表达式同样支持这样的形式)</h3>


比较操作符: ${1 lt 2} ${1 gt 2} ${1 ge 2} ${1 le 2} ${1 eq 2} ${1 ne 2} <br>
算数: ${1+2} ${1+"2"} ${"1"+"2"} <br>
空值判断: ${empty param.arg?"null":"not null" } <br>
<h3>7.显示常量</h3>
${"hello"} ${123}

<h3>8.将el表达式放在标签中使用:</h3>
<input value="${pageValue}" />
<a href="index.jsp?arg=${pageValue}" >index</a>
</body>

</html>



<%@ page language="java" import="java.util.*"
pageEncoding="UTF-8"%>
<%
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'MyJsp.jsp' starting page</title>
</head>

<body>
<form action="index.jsp" method = "get">
<input type = checkbox name = check value = ch1 >ch1
<input type = checkbox name = check value = ch2 >ch2
<input type = checkbox name = check value = ch3 >ch3

<input type = submit />
</form>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值