zoukankan      html  css  js  c++  java
  • Struts2>OGNL

    OGNL (object graph navigation language)

    1、想初始化domain model,可以自己new,也可以传值。如果不传值的话,则Struts不会自动初始化domain model

    2、传值的时候必须要定义空的构造方法。否则Struts2不知道调用哪个构造函数。

    http://localhost:8080/Struts2_OGNL/ognl/ognl.action?user.age=8

    如果你定义了如下的构造方法

    	public User(int age) {
    		super();
    		this.age = age;
    	}

    则不会自动产生无参的构造函数了,即使请求中user.age=8,但Struts2不知道调用什么构造方法。ValueStack中user也是null,

    <s:property value="user.age"/> 也取不到

    3、访问静态属性和方法

    <li>访问静态方法:<s:property value="@com.bjsxt.struts2.ognl.S@s()"/></li>

    Struts.xml必须配置 <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>

    四、例子

    <?xml version="1.0" encoding="GB18030" ?>
    <%@ page language="java" contentType="text/html; charset=GB18030"
        pageEncoding="GB18030"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
    <title>OGNL表达式语言学习</title>
    </head>
    <body>
     <ol>
      <li>访问值栈中的action的普通属性: username = <s:property value="username"/> </li>
      <li>访问值栈中对象的普通属性(get set方法):<s:property value="user.age"/> | <s:property value="user['age']"/> 
          | <s:property value="user[\"age\"]"/> | wrong: <%--<s:property value="user[age]"/>--%></li>
      <li>访问值栈中对象的普通属性(get set方法): <s:property value="cat.friend.name"/></li>
      <li>访问值栈中对象的普通方法:<s:property value="password.length()"/></li>
      <li>访问值栈中对象的普通方法:<s:property value="cat.miaomiao()" /></li>
      <li>访问值栈中action的普通方法:<s:property value="m()" /></li>
      <hr />
      <li>访问静态方法:<s:property value="@com.bjsxt.struts2.ognl.S@s()"/></li>
      <li>访问静态属性:<s:property value="@com.bjsxt.struts2.ognl.S@STR"/></li>
      <li>访问Math类的静态方法:<s:property value="@@max(2,3)" /></li>
      <hr />
      <li>访问普通类的构造方法:<s:property value="new com.bjsxt.struts2.ognl.User(8)"/></li>
      <hr />
      <li>访问List:<s:property value="users"/></li>
      <li>访问List中某个元素:<s:property value="users[1]"/></li>
      <li>访问List中元素某个属性的集合:<s:property value="users.{age}"/></li>
      <li>访问List中元素某个属性的集合中的特定值:<s:property value="users.{age}[0]"/> | <s:property value="users[0].age"/></li>
      <li>访问Set:<s:property value="dogs"/></li>
      <li>访问Set中某个元素:<s:property value="dogs[1]"/></li>
      <li>访问Map:<s:property value="dogMap"/></li>
      <li>访问Map中某个元素:<s:property value="dogMap.dog101"/> | <s:property value="dogMap['dog101']"/> | <s:property value="dogMap[\"dog101\"]"/></li>
      <li>访问Map中所有的key:<s:property value="dogMap.keys"/></li>
      <li>访问Map中所有的value:<s:property value="dogMap.values"/></li>
      <li>访问容器的大小:<s:property value="dogMap.size()"/> | <s:property value="users.size"/> </li>
      <hr />
      <li>投影(过滤):<s:property value="users.{?#this.age==1}[0]"/></li>
      <li>投影:<s:property value="users.{^#this.age>1}.{age}"/></li>
      <li>投影:<s:property value="users.{$#this.age>1}.{age}"/></li>
      <li>投影:<s:property value="users.{$#this.age>1}.{age} == null"/></li>
      <hr />
      <li>[]:<s:property value="[0].username"/></li>
      
     </ol>
     
     <s:debug></s:debug>
    </body>
    </html>

    结果如下:

    1. 访问值栈中的action的普通属性: username = u
    2. 访问值栈中对象的普通属性(get set方法): | | | wrong:
    3. 访问值栈中对象的普通属性(get set方法): wsz
    4. 访问值栈中对象的普通方法:1
    5. 访问值栈中对象的普通方法:miaomiao
    6. 访问值栈中action的普通方法:hello
    7. 访问静态方法:
    8. 访问静态属性:
    9. 访问Math类的静态方法:3
    10. 访问普通类的构造方法:
    11. 访问List:[]
    12. 访问List中某个元素:
    13. 访问List中元素某个属性的集合:[]
    14. 访问List中元素某个属性的集合中的特定值: |
    15. 访问Set:[dog: dog3, dog: dog1, dog: dog2]
    16. 访问Set中某个元素:
    17. 访问Map:{dog102=dog: dog102, dog101=dog: dog101, dog100=dog: dog100}
    18. 访问Map中某个元素:dog: dog101 | dog: dog101 | dog: dog101
    19. 访问Map中所有的key:[dog102, dog101, dog100]
    20. 访问Map中所有的value:[dog: dog102, dog: dog101, dog: dog100]
    21. 访问容器的大小:3 | 0
    22. 投影(过滤):
    23. 投影:[]
    24. 投影:[]
    25. 投影:false
    26. []:u
  • 相关阅读:
    Hihocoder 1275 扫地机器人 计算几何
    CodeForces 771C Bear and Tree Jumps 树形DP
    CodeForces 778D Parquet Re-laying 构造
    CodeForces 785E Anton and Permutation 分块
    CodeForces 785D Anton and School
    CodeForces 785C Anton and Fairy Tale 二分
    Hexo Next 接入 google AdSense 广告
    如何统计 Hexo 网站的访问地区和IP
    Design and Implementation of Global Path Planning System for Unmanned Surface Vehicle among Multiple Task Points
    通过ODBC接口访问人大金仓数据库
  • 原文地址:https://www.cnblogs.com/xqzt/p/5637209.html
Copyright © 2011-2022 走看看