vjame

优化代码是无止境的
随笔 - 65, 文章 - 9, 评论 - 26, 引用 - 0
数据加载中……

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");
    }
}

struts配置
        <action path="/jstlel"
            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>

posted on 2009-07-27 10:34 lanjh 阅读(244) 评论(0)  编辑  收藏 所属分类: Java Web


只有注册用户登录后才能发表评论。


网站导航: