码迷,mamicode.com
首页 > 其他好文 > 详细

EL表达式

时间:2016-11-04 01:17:31      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:ati   first   value   sub   cep   names   提交   orm   int   

其实也就是一些java代码的简写方式 最终也会转化成java代码  知道这个思想 以后多查就会了

 


<%@ page import="com.headFirst.Student,java.util.Date" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="el.jsp" method="post">
username:<input type="text" name="username"/>
<input type="submit" value="submit"/
>


<%
//使用java代码获取提交的值
String str=request.getParameter("username");
out.println(str);
%>
<%--相当于使用如下 el表达式 --%>
${param.username}


<%--使用bean --%>
<jsp:useBean id="student" class="com.headFirst.Student" scope="session"></jsp:useBean>
<jsp:setProperty property="age" value="12" name="student"/>

<%--java代码获取 --%>
age:
<%
Student student2 =(Student)session.getAttribute("student");
out.println(student2.getAge());

%>

<%--或者这样获取 --%>
<jsp:getProperty property="age" name="student"/>

<%--但是如果把数据再传到另一个JSP的话 使用JavaBean就需要再次使用useBean字段才能获取 --%>

<a href="el2.jsp?score=20&name=a&name=b&name=c">To El2 Page</a>

<%
application.setAttribute("time", new Date());
%>
</form>
</body>
</html>


<%@ page import="com.headFirst.Student,java.util.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%--如果这里没有使用useBean是会报错的 --%>
<%--
<jsp:getProperty property="age" name="student"/>
--%>


<%
Student student=new Student();
student.setName("headFirst");
session.setAttribute("com.headFirst",student);
%>
<%--这个使用EL表达式就很好用了 sessionScope是隐含对象 --%>
age: ${sessionScope.student.age}
<%--这个应用场景就是某个属性带类似于.这样的字符 这样才能获取到 --%>
name:${sessionScope["com.headFirst"].name}
<%
//相当于java代码
Student student2=(Student)session.getAttribute("student");
out.print(student2.getAge());
%>

<%--上面的scope其实都可以简写 因为EL表达式可以自动寻找属性
寻找的区域顺序是page->request->session->appliaction 找到了就停止
--%>
<%--比如上面的 ${sessionScope.student.age} 可以写成如下 --%>
<br>auto search
age:${student.age}


<%--对于表单提交的值 可以使用param获取 --%>
score: ${param.score}

<%--相当于如下java代码 --%>
<%

String str=request.getParameter("score");
out.print(str);
%>

<%--但是用EL表达式的一个很大的特点是可以自动类型转换 而用java代码的只是自动连接 --%>
<br> test changeType
score change to 40: ${param.score+20}

<%
//这个只是会变成2020
String str2=request.getParameter("score")+20;
out.print(str2);
%>

<!-- 1 隐含对象详解 sessionScope PageScope requestScope applicationScope-->
time: ${applicationScope.time}
<%--相当于以下代码 --%>
<%=application.getAttribute("time")%>

<%-- 2 与输入有关的隐含对象 param paramValues 也就是request
并且el可以自动转型 一路.下去就可以如param.time.time 而如果是java代码的话 就需要转型了 因为request返回的是Object类型 --%>

score:${param.score}
<%--相当于--%>
<%=request.getParameter("score") %>

<br>
<%--这里的.class代表这getClass()方法 在这里无法遍历 需要使用JSTL才能做到 --%>
names: ${paramValues.name[0]}

<%-- 3其它隐含对象 --%>
JSESSIONID: ${cookie.JSESSIONID.name} -- ${cookie.JSESSIONID.value}
<%= request.getCookies()[0].getName() %>
Accept-Language: ${header["Accept-Language"]}

initParam :${initParam.email}

<%--4 PageContext很重要 pageContext即为PageContext类型 但只能读取属性 还有很多很多知道原理就好 需要时查找--%>

contextPath: ${pageContext.request.contextPath}


<%--5 EL的关系运算符--%>
${ param.score>60? "true":"false"}

<%
List<String> names=new ArrayList<String>();
names.add("xx");
request.setAttribute("names",names);
%>

<%--empty可以作用于一个集合 若该集合不存在或集合中没有元素 则返回true 否则返回false --%>
name is empty: ${empty requestScope.names}

</body>
</html>

EL表达式

标签:ati   first   value   sub   cep   names   提交   orm   int   

原文地址:http://www.cnblogs.com/HJL085/p/6028732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!