首先介绍下el表达式是什么东东呢?根据本人的肤浅认识,表达式就是能够取出servlet各种域中的数据哈,譬如,嗯,pageContext,request,session,application,param等....,然后呢,el表达式还支持很多计算,嗯,比如,算数运算,加、减、乘、除、求余等....还有逻辑运算(真、假),三目运算...上代码吧,不然说着我自己都嫌弃我自己了。还有个重要的地方,不吐不快哈,就是以上的各个域是平级关系哈,取值的时候都一样的取法。反正咋样说简单就咋说说哈,说多了看着会睡觉的,说那么多干嘛?整得过理解一句话要几个小时(夸张咯哈),在说我也这水平,很多还是看了人家的博客或者是教程来搞的,别打我,我都坦白了。
1.往各种内置对象中赛数据,然后拿出来,于是知道了el表达式是用来拿内置对象里面的数据,貌似是这样?我想想,嗯,真的是这样
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><% pageContext.setAttribute("info1","page范围的值"); request.setAttribute("info1","request范围的值"); session.setAttribute("info1","session范围的值"); application.setAttribute("info1","application范围的值");%><h1>${info1 }</h1><h1>${info2 }</h1><h1>${info3 }</h1><h1>${info4 }</h1></body></html>2.页面之间的参数传递数据不会呢?反正我不想在存入什么session、request..这些乱七八糟的地方咯,于是好像有个叫param的东东?我们看看呢。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form action="el2.jsp" method="post"><input type="text" name="name"/><input type="submit" value="提交el2.jsp"/></form><a href="el2.jsp?age=12">提交el2.jsp</a><form action="el2.jsp" method="post"><input type="checkbox" name="hobby" value="java语言"/>java语言<input type="checkbox" name="hobby" value="C#语言"/>C#语言<input type="checkbox" name="hobby" value="php语言"/>php语言<input type="submit" value="提交el2.jsp"/></form></body></html>这是我们的el2.jsp喔,
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><% request.setCharacterEncoding("utf-8");%><h1>姓名:${param.name }</h1><h1>年龄:${param.age }</h1><h1>爱好一:${paramValues.hobby[0] }</h1><h1>爱好二:${paramValues.hobby[1] }</h1><h1>爱好三:${paramValues.hobby[2] }</h1></body></html>喔,原来param是存放页面之间参数的域,和session、request、pageContext、application等域是一样的,慢,还有一点大家注意下,你们看,我们的复选框存在了一个组里面了,快看,是真的哎,于是我们先取出该组的名字,然后根据下标一个一个在把他们弄出来,喔原来是这样
那对象咋取呢?孩子如果这是你问的?那还真的很有觉悟~我只想问你。满十八岁了吗?好好读书:你会一直单着的,除非你能变成大神。
好吧,我们来取对象
1.这就是我们的对象,你喜欢吗?喜欢那个属性?
package com.java.model;
public class People {
private int id;
private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } 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; }}2.喜欢什么就把什么取出,全都喜欢就一起取出来
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><%@ page import="com.java.model.People" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><% People zhangsan=new People(); zhangsan.setId(1); zhangsan.setName("小妹妹"); zhangsan.setAge(18); request.setAttribute("zhangsan",zhangsan);%><h1>编号:${zhangsan.id }</h1><h1>姓名:${zhangsan.name }</h1><h1>年龄:${zhangsan.age }</h1></body></html>
好了对象会取了我们看看咋样去集合里面的数据呢?没错,我们首先需要把集合赛到内置对象里面哈,大家貌似明白了什么.....
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><%@ page import="java.util.*" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><% List all=new LinkedList(); all.add(0,"元素一"); all.add(1,"元素二"); all.add(2,"元素三"); request.setAttribute("all",all);%><h1>${all[0] }</h1><h1>${all[1] }</h1><h1>${all[2] }</h1></body></html>然后就和取内置对象一样的取法咯。。
说好的各种运算呢?咋没有呢?其实咋下边,叫你不继续往下翻,你会看到你想看的【/阴险】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><%@ page import="java.util.*" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><% request.setAttribute("num1",10); request.setAttribute("num2",3); request.setAttribute("flag1",true); request.setAttribute("flag2",false);%><h1>算数运算符</h1><h2>num1=${num1 },num2=${num2 }</h2><h2>num1+num2=${num1+num2 }</h2><h2>num1-num2=${num1-num2 }</h2><h2>num1*num2=${num1*num2 }</h2><h2>num1/num2=${num1/num2 }</h2><h2>num1%num2=${num1%num2 }</h2><h2>num1*(num1-num2)=${num1*(num1-num2) }</h2><h1>关系运算符</h1><h2>flag1=${flag1 },flag2=${flag2 }</h2><h2>与操作flag1 && flage2:${flag1 && flage2 }</h2><h2>或操作flag1 || flage2:${flag1 || flage2 }</h2><h2>非操作!flag1:${!flag1}</h2><h1>三目运算符</h1><h2>三目操作:num1>num2:${num1>num2?"yes":"no" }</h2><h1>empty关键字</h1><h2>判断空操作:${empty num1 }</h2></body></html>
大清早的起来写博客,我也是醉了,但愿长醉不复醒(没有挂哈)