Struts 1 学习笔记-3-1(EL表达式的使用)

来源:互联网 发布:sleep函数 linux 编辑:程序博客网 时间:2024/06/10 12:43

发现EL使用起来挺方便,至少不用<%= %>了,如果单纯使用EL不需要引入jar包,本人是将其与Struts整合使用。

1.首先是我的Struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"
>

<struts-config>
    
<action-mappings>
        
<action path="/jstlel" type="com.codedestiny.struts.JSTLELAction">
            
<forward name="success" path="/jstlel.jsp"></forward>
        
</action>
    
</action-mappings>
    
<message-resources parameter="MessageResources" />
</struts-config>

 

2.JSTLELAction.java

package com.codedestiny.struts;

import java.util.ArrayList;
import java.util.HashMap;
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;

public class JSTLELAction extends Action {

    
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setAttribute(
"hello""Hello World");
        
        Group relatedGroup 
= new Group();
        relatedGroup.setName(
"SUN");
        User u 
= new User();
        u.setUsername(
"CodeDestiny");
        u.setAge(
21);
        u.setRelatedGroup(relatedGroup);
        request.setAttribute(
"user", u);
        
        Map
<String, Integer> hashMap = new HashMap<String, Integer>();
        hashMap.put(
"key1"1);
        hashMap.put(
"key2"2);
        request.setAttribute(
"map", hashMap);
        
        String[] array 
= new String[]{"a""b""c""d"};
        request.setAttribute(
"array", array);
        
        request.setAttribute(
"value1""");
        request.setAttribute(
"value2"new ArrayList());
        request.setAttribute(
"value3"new int[]{12});
        
        
return mapping.findForward("success");
    }

    
}

3.与之对应的jstlel.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
  
</head>
  
  
<body>
    
<h1>测试EL表达式</h1>
    
    
<hr>
    
<li>简单的EL表达式,其用法是 $ 和 { }</li><br>
    
1 + 2 = ${1 + 2} <br>
    
2 × 10 = ${2 * 10} <br>
    
    
<hr>
    
<li>EL表达式隐式对象:pageScope,requestScope,sessionScope,applicationScope等,如果未指定scope,它的搜索顺序是:page~application</li><br>
    hello(jsp):
<%= request.getAttribute("hello"%><br>
    hello(el): $
{hello}<br>
    hello(el scope
=requestScope): ${requestScope.hello}
    
    
<hr>
    
<li>使用.进行导航,也称存储器</li><br>
    姓名: $
{ user.username } <br>
    年龄: $
{ user.age } <br>
    所属组: $
{ user.relatedGroup.name } <br>
    
    
<hr>
    
<li>使用存储器取出map中的值</li><br>
    map.key1 : $
{ map.key1 } <br>
    map.key2 : $
{ map.key2 } <br>
    
    
<hr>
    
<li>用[]对数组中的元素进行访问</li><br>
    array[
1] : ${ array[1] } <br>
    
    
<hr>
    
<li>EL支持的表达式</li><br>
    
10 / 5 = $10 / 5 } <br>
    
10 div 5 = $10 div 5 } <br>
    
10 % 5 = $10 % 5 } <br>
    
10 mod 5 = $10 mod 5 } <br>
    
    
<!-- 
        
== 在EL中可表示为 eq
        !
= 在EL中可表示为 ne
        
< 在EL中可表示为 lt
        
> 在EL中可表示为 gt
        
<= 在EL中可表示为 le
        
>= 在EL中可表示为 ge
        
&& 在EL中可表示为 and
        
|| 在EL中可表示为 or
        
! 在EL中可表示为 not
        
/ 在EL中可表示为 div
        
% 在EL中可表示为 mod
    
-->
    
    
<hr>
    
<li></li>测试empty<br>
    value1 empty 
? : ${ empty value1 } <br>
    value2 empty 
? : ${ empty value2 } <br>
    value3 empty 
? : ${ empty value3 } <br>
    
  
</body>
</html>

 

 

原创粉丝点击