zoukankan      html  css  js  c++  java
  • struts2----ognl

    OGNL表达式

           OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目。 Struts2框架使用OGNL作为默认的表达式语言。

           OGNL 有一个上下文(Context)概念,说白了上下文就是一个MAP结构,它实现了  java.utils.Map 的接口。 OgnlContext对象

      

      Struts框架默认就支持Ognl表达式语言。

        (struts必须引用的包:ognl.jar)

           作用:页面取值用。

      分析:

          El表达式语言,用于页面取值,jsp页面取值的标准。(默认直接可以使用) (应用范围更广。)

          Ognl表达式语言, struts标签默认支持的表达式语言。

                                           必须配置struts标签用,不能离开struts标签直接用。

    OgnlContext对象:

    1. Ognl表达式语言语言取值,取非根元素的值,必须用#号

     1 // OgnlContext用法
     2 public class OgnlDemo1 {
     3 
     4     /**
     5      * 1. Ognl表达式语言语言取值,取非根元素的值,必须用#号
     6      * @throws Exception
     7      */
     8     @Test
     9     public void testOgnl() throws Exception {
    10         // 创建一个Ognl上下文对象
    11         OgnlContext context = new OgnlContext();
    12         // 放入数据
    13         User user = new User();
    14         user.setId(100);
    15         user.setName("Jack");
    16         // 【往非根元素放入数据, 取值的时候表达式要用"#"】
    17         context.put("user", user);
    18         
    19         // 获取数据(map)
    20         // 先构建一个Ognl表达式, 再解析表达式
    21         Object ognl = Ognl.parseExpression("#user.name");
    22         Object value = Ognl.getValue(ognl, context, context.getRoot());
    23         
    24         System.out.println(value);
    25     }

    2. Ognl表达式语言语言取值,取根元素的值,不用带#号

     1 public void testOgn2() throws Exception {
     2         // 创建一个Ognl上下文对象
     3         OgnlContext context = new OgnlContext();
     4         // 放入数据
     5         User user = new User();
     6         user.setId(100);
     7         user.setName("Jack");
     8         // 【往根元素放入数据】
     9         context.setRoot(user);
    10         
    11         // 获取数据(map)
    12         // 先构建一个Ognl表达式, 再解析表达式
    13         Object ognl = Ognl.parseExpression("address.province");
    14         Object value = Ognl.getValue(ognl, context, context.getRoot());
    15         
    16         System.out.println(value);
    17     }

    3.Ognl对 静态方法调用的支持

     1 public void testOgn3() throws Exception {
     2         // 创建一个Ognl上下文对象
     3         OgnlContext context = new OgnlContext();
     4         
     5         // Ognl表单式语言,调用类的静态方法
     6         //Object ognl = Ognl.parseExpression("@Math@floor(10.9)");
     7         // 由于Math类在开发中比较常用,所以也可以这样写
     8         Object ognl = Ognl.parseExpression("@@floor(10.9)");
     9         Object value = Ognl.getValue(ognl, context, context.getRoot());
    10         System.out.println(value);
    11     }

    struts的数据流转:

      1.从action中传递数据到jsp页面

                 action:

                  

     1 public class OgnlDemo3 extends ActionSupport{
     2     
     3     
     4     @Override
     5     public String execute() throws Exception {
     6         
     7         // 测试迭代标签
     8         
     9         List<User> list = new ArrayList<User>();
    10         Map<Integer,User> map = new HashMap<Integer, User>();
    11         
    12         // 初始化
    13         for (int i=1; i<11; i++) {
    14             User user = new User(i,"Jack" + i);
    15             
    16             list.add(user);
    17             map.put(user.getId(), user);
    18         }
    19         
    20         // 保存  
    21         ActionContext.getContext().getContextMap().put("list", list);
    22         ActionContext.getContext().getContextMap().put("map", map);
    23         
    24         return super.execute();
    25     }
    26     
    27     
    28     
    29 }

       jsp页面:

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%@taglib uri="/struts-tags" prefix="s" %>
     3 
     4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     5 <html>
     6   <head>
     7     <title>My JSP 'index.jsp' starting page</title>
     8     <meta http-equiv="pragma" content="no-cache">
     9     <meta http-equiv="cache-control" content="no-cache">
    10     <meta http-equiv="expires" content="0">    
    11     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    12     <meta http-equiv="description" content="This is my page">
    13     <style type="text/css" >
    14        .odd{
    15                background-color: red; 
    16               }
    17        .even{
    18                background-color:blue;}
    19       </style>
    20 
    21   </head>
    22   
    23   <body>
    24     <br/>一、. list迭代</br>
    25     <table border="1">
    26         <tr>
    27             <td>编号</td>
    28             <td>名称</td>
    29         </tr>
    30           <s:iterator var="user" value="#request.list" status="st">
    31               <tr class=<s:property value="#st.even?'even':'odd'"/> >
    32                   <td><s:property value="#user.id"/></td>
    33                   <td><s:property value="#user.name"/></td>
    34               </tr>
    35           </s:iterator>
    36       </table>
    37       
    38        <br/>二、迭代map</br>
    39     <table border="1">
    40         <tr>
    41             <td>编号</td>
    42             <td>名称</td>
    43         </tr>
    44           <s:iterator var="en" value="#request.map" status="st">
    45               <tr>
    46                   <td><s:property value="#en.key"/></td>
    47                   <td><s:property value="#en.value.name"/></td>
    48               </tr>
    49           </s:iterator>
    50       </table>
    51       
    52       
    53       <!-- Ognl表达式可以取值,也可以动态构建集合 -->
    54   </body>
    55 </html>
  • 相关阅读:
    委托与事件的关系
    分布式存储ceph——(1)部署ceph
    neutron二
    openstack第五章:cinder
    openstack第六章:dashboard
    openstack第一章:keystone
    openstack第二章:glance
    openstack第三章:nova
    openstack第四章:neutron— 网络服务
    openstack安装
  • 原文地址:https://www.cnblogs.com/edxiscoming/p/4934135.html
Copyright © 2011-2022 走看看