vjame

          優(yōu)化代碼是無(wú)止境的
          隨筆 - 65, 文章 - 9, 評(píng)論 - 26, 引用 - 0
          數(shù)據(jù)加載中……

          el表達(dá)式


          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");
                  
                  
          //結(jié)構(gòu)
                  Group group = new Group();
                  group.setName(
          "尚學(xué)堂");
                  
                  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);
                  
                  
          //字符串?dāng)?shù)組
                  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頁(yè)面
          <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            
          <head>
              
              
          <title>測(cè)試el表達(dá)式</title>
              
          <!--
              <link rel="stylesheet" type="text/css" href="styles.css">
              
          -->
            
          </head>
            
            
          <body>
              
          <h1>測(cè)試EL表達(dá)式</h1><br>
              
          <hr>
              
          <li>普通字符串</li><br>
              hello(jsp腳本):
          <%=request.getAttribute("hello"%><br>
              hello(el表達(dá)式,el表達(dá)式的使用方法$和{}):${hello }
          <br>
              hello(el表達(dá)式,el的隱含對(duì)象pageScope,requestScope,sessionScope,applicationScope,
          <br> 如果未指定scope,它的搜索順序?yàn)閜ageScope~applicationScope):${requestScope.hello }<br>
              hello(el表達(dá)式,scope=session):${sessionScope.hello }
          <br>
              
          <p>
              
          <li>結(jié)構(gòu),采用.進(jìn)行導(dǎo)航,也稱存取器</li><br>
              姓名:${user.username }
          <br>
              年齡:${user.age }
          <br>
              所屬組:${user.group.name }
          <br>
              
          <p>
              
          <li>輸出map,采用.進(jìn)行導(dǎo)航,也稱存取器</li><br>
              mapvalue.key1:${mapvalue.key1 }
          <br>
              mapvalue.key2:${mapvalue.key2 }
          <br>
              
          <p>
              
          <li>輸出數(shù)組,采用[]和下標(biāo)</li><br>
              strarray[2]:${strarray[1] }
          <br>
              
          <p>
              
          <li>輸出對(duì)象數(shù)組,采用[]和下標(biāo)</li><br>
              userarray[3].username:${users[2].username }
          <br>
              
          <p>
              
          <li>輸出list,采用[]和下標(biāo)</li><br>
              userlist[5].username:${userlist[4].username }
          <br>
              
          <p>
              
          <li>el表達(dá)式對(duì)運(yùn)算符的支持</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>測(cè)試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 閱讀(253) 評(píng)論(0)  編輯  收藏 所屬分類: Java Web

          主站蜘蛛池模板: 镇康县| 汤阴县| 灵宝市| 申扎县| 民县| 巴彦淖尔市| 天津市| 莱芜市| 蒲江县| 彭阳县| 兴仁县| 阜南县| 虎林市| 绥芬河市| 花莲市| 韶关市| 安化县| 磐石市| 墨玉县| 石门县| 琼中| 梓潼县| 乌恰县| 邓州市| 九龙坡区| 建德市| 白玉县| 万荣县| 邹城市| 克什克腾旗| 察雅县| 孟连| 忻州市| 乐东| 浮山县| 酒泉市| 恭城| 宜都市| 蒙城县| 天祝| 揭阳市|