el表達式
JSTL 1.0 introduced the notion of an expression language (EL) to make it easy for
page authors to access and manipulate application data without having to master
the complexity associated with programming languages such as Java and JavaScript.
Starting with JSP 2.0 / JSTL 1.1, the EL has become the responsibility of the JSP
specification and is now formally defined there.
This chapter provides a simple overview of the key features of the expression
language, it is therefore non-normative. Please refer to the JSP specification for the
formal definition of the EL.
The EL is invoked exclusively via the construct ${expr}.
例子:
Action
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.lanjh.struts.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.lanjh.struts.po.Group;
import com.lanjh.struts.po.User;
/**
* MyEclipse Struts
* Creation date: 07-17-2009
*
* XDoclet definition:
* @struts.action
*/
public class JstlElAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
//普通字符串
request.setAttribute("hello", "hello world");
//結構
Group group = new Group();
group.setName("尚學堂");
User user = new User();
user.setUsername("張三");
user.setAge(18);
user.setGroup(group);
request.setAttribute("user", user);
//map
Map mapValue = new HashMap();
mapValue.put("key1", "value1");
mapValue.put("key2", "value2");
request.setAttribute("mapvalue", mapValue);
//字符串數組
String[] strArray = new String[]{"a", "b", "c"};
request.setAttribute("strarray", strArray);
User[] users = new User[10];
for (int i=0; i<10; i++) {
User u = new User();
u.setUsername("U_" + i);
users[i] = u;
}
request.setAttribute("users", users);
List userList = new ArrayList();
for (int i=0; i<10; i++) {
User uu = new User();
uu.setUsername("UU_" + i);
userList.add(uu);
}
request.setAttribute("userlist", userList);
//empty
request.setAttribute("value1", null);
request.setAttribute("value2", "");
request.setAttribute("value3", new ArrayList());
request.setAttribute("value4", "123456");
return mapping.findForward("success");
}
}
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.lanjh.struts.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.lanjh.struts.po.Group;
import com.lanjh.struts.po.User;
/**
* MyEclipse Struts
* Creation date: 07-17-2009
*
* XDoclet definition:
* @struts.action
*/
public class JstlElAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
//普通字符串
request.setAttribute("hello", "hello world");
//結構
Group group = new Group();
group.setName("尚學堂");
User user = new User();
user.setUsername("張三");
user.setAge(18);
user.setGroup(group);
request.setAttribute("user", user);
//map
Map mapValue = new HashMap();
mapValue.put("key1", "value1");
mapValue.put("key2", "value2");
request.setAttribute("mapvalue", mapValue);
//字符串數組
String[] strArray = new String[]{"a", "b", "c"};
request.setAttribute("strarray", strArray);
User[] users = new User[10];
for (int i=0; i<10; i++) {
User u = new User();
u.setUsername("U_" + i);
users[i] = u;
}
request.setAttribute("users", users);
List userList = new ArrayList();
for (int i=0; i<10; i++) {
User uu = new User();
uu.setUsername("UU_" + i);
userList.add(uu);
}
request.setAttribute("userlist", userList);
//empty
request.setAttribute("value1", null);
request.setAttribute("value2", "");
request.setAttribute("value3", new ArrayList());
request.setAttribute("value4", "123456");
return mapping.findForward("success");
}
}
struts配置
<action path="/jstlel"
type="com.lanjh.struts.action.JstlElAction" validate="false">
<forward name="success" path="/jstl_el.jsp"></forward>
</action>
type="com.lanjh.struts.action.JstlElAction" validate="false">
<forward name="success" path="/jstl_el.jsp"></forward>
</action>
JSP頁面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>測試el表達式</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>測試EL表達式</h1><br>
<hr>
<li>普通字符串</li><br>
hello(jsp腳本):<%=request.getAttribute("hello") %><br>
hello(el表達式,el表達式的使用方法$和{}):${hello }<br>
hello(el表達式,el的隱含對象pageScope,requestScope,sessionScope,applicationScope,<br> 如果未指定scope,它的搜索順序為pageScope~applicationScope):${requestScope.hello }<br>
hello(el表達式,scope=session):${sessionScope.hello }<br>
<p>
<li>結構,采用.進行導航,也稱存取器</li><br>
姓名:${user.username }<br>
年齡:${user.age }<br>
所屬組:${user.group.name }<br>
<p>
<li>輸出map,采用.進行導航,也稱存取器</li><br>
mapvalue.key1:${mapvalue.key1 }<br>
mapvalue.key2:${mapvalue.key2 }<br>
<p>
<li>輸出數組,采用[]和下標</li><br>
strarray[2]:${strarray[1] }<br>
<p>
<li>輸出對象數組,采用[]和下標</li><br>
userarray[3].username:${users[2].username }<br>
<p>
<li>輸出list,采用[]和下標</li><br>
userlist[5].username:${userlist[4].username }<br>
<p>
<li>el表達式對運算符的支持</li><br>
1+2=${1+2 }<br>
10/5=${10/5 }<br>
10 div 5=${10 div 5 }<br>
10%3=${10 % 3 }<br>
10 mod 3=${10 mod 3 }<br>
<!--
==/eq
!=/ne
</lt
>/gt
<=/le
>=/ge
&&/and
||/or
!/not
//div
%/mod
-->
<li>測試empty</li><br>
value1:${empty value1 }<br>
value2:${empty value2 }<br>
value3:${empty value3 }<br>
value4:${empty value4 }<br>
value4:${!empty value4 }<br>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>測試el表達式</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>測試EL表達式</h1><br>
<hr>
<li>普通字符串</li><br>
hello(jsp腳本):<%=request.getAttribute("hello") %><br>
hello(el表達式,el表達式的使用方法$和{}):${hello }<br>
hello(el表達式,el的隱含對象pageScope,requestScope,sessionScope,applicationScope,<br> 如果未指定scope,它的搜索順序為pageScope~applicationScope):${requestScope.hello }<br>
hello(el表達式,scope=session):${sessionScope.hello }<br>
<p>
<li>結構,采用.進行導航,也稱存取器</li><br>
姓名:${user.username }<br>
年齡:${user.age }<br>
所屬組:${user.group.name }<br>
<p>
<li>輸出map,采用.進行導航,也稱存取器</li><br>
mapvalue.key1:${mapvalue.key1 }<br>
mapvalue.key2:${mapvalue.key2 }<br>
<p>
<li>輸出數組,采用[]和下標</li><br>
strarray[2]:${strarray[1] }<br>
<p>
<li>輸出對象數組,采用[]和下標</li><br>
userarray[3].username:${users[2].username }<br>
<p>
<li>輸出list,采用[]和下標</li><br>
userlist[5].username:${userlist[4].username }<br>
<p>
<li>el表達式對運算符的支持</li><br>
1+2=${1+2 }<br>
10/5=${10/5 }<br>
10 div 5=${10 div 5 }<br>
10%3=${10 % 3 }<br>
10 mod 3=${10 mod 3 }<br>
<!--
==/eq
!=/ne
</lt
>/gt
<=/le
>=/ge
&&/and
||/or
!/not
//div
%/mod
-->
<li>測試empty</li><br>
value1:${empty value1 }<br>
value2:${empty value2 }<br>
value3:${empty value3 }<br>
value4:${empty value4 }<br>
value4:${!empty value4 }<br>
</body>
</html>
posted on 2009-07-27 10:34 lanjh 閱讀(254) 評論(0) 編輯 收藏 所屬分類: Java Web