一、实验目的

1. 掌握OGNL表达式的语法结构

2. 掌握OGNL表达式获取Action中的相关值的方法。

3. 理解投影的含义。

二、实验内容

创建业务控制类OgnlAction,测试基本属性、基本方法、类对象、静态属性、静态方法、List列表、

Set集合、Map映射等各种类型参数采用OGNL表达式获取的办法。

三、实验步骤及代码

1、创建一些javaBean

a、User类 属性int age,带参构造方法

package com.tj.struts.bean;public class User {private int age;public User(){}/*** @param age*/public User(int age) {this.age = age;}/*** @return the age*/public int getAge() {return age;}/*** @param age the age to set*/public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "User"+age;}}

b、Dog类 属性 String  name 带参构造方法

package com.tj.struts.bean;public class Dog {private String name ;/*** */public Dog() {// TODO Auto-generated constructor stub}/*** @param name*/public Dog(String name) {this.name = name;}/*** @return the name*/public String getName() {return name;}/*** @param name the name to set*/public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Dog"+name;}
}

c、Cat类  属性Dog类对象 friend 成员方法miaomiao

package com.tj.struts.bean;public class Cat {private Dog friend;public Cat(){}/*** @param friend*/public Cat(Dog friend) {this.friend = friend;}/*** @return the friend*/public Dog getFriend() {return friend;}/*** @param friend the friend to set*/public void setFriend(Dog friend) {this.friend = friend;}public String miaomiao(){return "miaomiaoMethod";}@Overridepublic String toString() {return "Cat"+friend ;}
}

d、静态内容类S  静态属性STR  静态方法s()

package com.tj.struts.bean;public class S {public static String STC="static String ";public static String s(){return STC+"static mathod";}
}

2、创建业务控制类

a、包含基本属性username、password

b、类成员User作为属性

c、类成员Cat(Cat的属性为Dog对象)作为属性

d、保存User对象的列表users(存储三个对象1,2,3)

e、保存dog对象的集合dogs(存储三个对象dog1,dog2,dog3)

f、保存dog键值对的映射dogMap(存储三组数据dog100:Dog(100) dog101: Dog(101) dog102:Dog(102))

g、包含基本方法m(返回字符串actionMethod)

package com.tj.struts.contoller;import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;import com.opensymphony.xwork2.ActionSupport;
import com.tj.struts.bean.Cat;
import com.tj.struts.bean.Dog;
import com.tj.struts.bean.User;public class OgnlAction extends ActionSupport{private String username;private String password;private Cat cat;private User user;private List<User> users=new ArrayList<User>();private Set<Dog> dogs=new HashSet<Dog>();private Map<String ,Dog> dogmap=new HashMap<String,Dog>();/*** @return the username*/public String getUsername() {return username;}/*** @param username the username to set*/public void setUsername(String username) {this.username = username;}/*** @return the password*/public String getPassword() {return password;}/*** @param password the password to set*/public void setPassword(String password) {this.password = password;}/*** @return the cat*/public Cat getcat() {return cat;}/*** @param cat the cat to set*/public void setcat(Cat cat) {this.cat = cat;}/*** @return the user*/public User getUser() {return user;}/*** @param user the user to set*/public void setUser(User user) {this.user = user;}/*** @return the users*/public List<User> getUsers() {return users;}/*** @param users the users to set*/public void setUsers(List<User> users) {this.users = users;}/*** @return the dogs*/public Set<Dog> getDogs() {return dogs;}/*** @param dogs the dogs to set*/public void setDogs(Set<Dog> dogs) {this.dogs = dogs;}/*** @return the dogmap*/public Map<String, Dog> getDogmap() {return dogmap;}/*** @param dogmap the dogmap to set*/public void setDogmap(Map<String, Dog> dogmap) {this.dogmap = dogmap;}public String m(){return "actionMethod";}public OgnlAction(){username="ognltester";password="123456789";user=new User();cat=new Cat(new Dog("dog0"));users.add(new User(1));users.add(new User(2));users.add(new User(3));dogs.add(new Dog("dog1"));dogs.add(new Dog("dog2"));dogs.add(new Dog("dog3"));dogmap.put("dog1200", new Dog("dog1200"));dogmap.put("dog1201", new Dog("dog1201"));dogmap.put("dog1202", new Dog("dog1202"));}}

3、配置

a、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"><display-name></display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>
</web-app>

b、struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts><package name="default" namespace="/" extends="struts-default"><action name="OGNL" class="com.tj.struts.contoller.OgnlAction"><result>/Ognltest.jsp</result></action></package><!-- 允许调用静态方法 --><constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
</struts>

4、测试

a、index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><a href="OGNL">ognltest</a></body>
</html>

b、Ognltest.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'Ognltest.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><%@taglib uri="/struts-tags" prefix="s" %><body ><center>这是ognl测试界面<br><br>1、访问值栈中action的普通属性:<br>username= <s:property value="username"/><br>2、访问值栈中的对象的普通属性:<br>user.age=<s:property value="user.age"/><br>user.age=<s:property value="user['age']"/><br>user.age=<s:property value="user[\"age\"]"/><br>3、访问值栈中对象的普通属性:<br><s:property value="cat.friend.name"/><br>4、访问值栈中对象的普通方法:<br>password长度=<s:property value="password.length()"/><br>5、访问值栈中对象的普通方法:<br><s:property value="cat.miaomiao()"/><br>6、访问action中的普通方法:<br><s:property value="m()"/><br>7、访问静态方法:(*)<br><s:property value="@com.tj.struts.bean.S@s()"/><br>8、访问静态属性:<br><s:property value="@com.tj.struts.bean.S@STC"/><br>9、访问Math类的静态方法:<br><s:property value="@@max(5,10)"/><br>10、访问普通类的构造方法:<br><s:property value="new com.tj.struts.bean.User(10)"/><br>11、访问list:<br><s:property value="users"/><br>12、访问list中的某个元素:<br><s:property value="users[1]"/><br>13、访问list元素中某个属性的集合:<br><s:property value="users.{age}"/><br>14、访问list元素中某个属性的集合的特定值:<br><s:property value="users.{age}[0]"/><br>15、访问Set:<br><s:property value="dogs"/><br>16、访问Set中的某个元素:<br><s:property value="dogs[0]"/><br>17、访问Map:<br><s:property value="dogmap"/><br>18、访问Map中的某个元素:<br><s:property value="dogmap.dog1202"/><br>19、访问Map中所有的Key:<br><s:property value="dogmap.keys"/><br>20、访问Map中所有的value:<br><s:property value="dogmap.values"/><br>21、访问容器的大小:<br><s:property value="dogmap.size()"/><br>22、投影:users集合中年龄为1的第一个元素<br><s:property value="users.{?#this.age==1}[0]"/><br>23、投影:users集合中年龄大于1的user的年龄的集合<br><s:property value="users.{?#this.age>1}.{age}"/><br>24、投影:users集合中年龄》1的user集合的最后一个user的age<br><s:property value="users.{$#this.age>1}.{age}"/><br>25、投影:判断users集合中年龄>1的user集合的最后一个user的age是否为空<br><s:property value="users.{$#this.age>1}.{age}==null"/><br>26、根对象:“[0]代表值栈”<br><s:property value="[0].password"/><br></center></body>
</html>

Struts2 ognl表达式相关推荐

  1. Struts2 OGNL表达式注入漏洞解决

    Struts2 OGNL表达式注入漏洞解决 线上项目使用Struts2 版本2.3,需要升级版本,记录解决步骤,不确保其它项目都可以 pom.xml <struts.version>2.5 ...

  2. struts2 OGNL表达式

    一.OGNL OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,可以任意存取对象的属性或者调用对 ...

  3. java ognl表达式 与struts2标签_Struts2 OGNL表达式实例详解

    Object Graph Navigation Language:对象图导航语言,就是用点来访问成员变量 例1: struts.xml: /ognl.jsp OgnlAction1.java: pac ...

  4. 深入理解Struts2中的OGNL表达式

    Struts 2中的表达式语言 Struts 2支持以下几种表达式语言: OGNL(Object-Graph Navigation Language),可以方便地操作对象属性的开源表达式语言: JST ...

  5. Struts2漏洞分析之Ognl表达式特性引发的新思路

    摘要 在Ognl表达式中,会将被括号"()"包含的变量内容当做Ognl表达式执行.Ognl表达式的这一特性,引发出一种新的攻击思路.通过将恶意代码存储到变量中,然后在调用Ognl表 ...

  6. struts2教程(9)--OGNL表达式使用

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

  7. struts2的OGNL表达式理解(一)

    一,什么是OGNL表达式     OGNL是Object-Graph Navigation Language(对象图导航语言)的缩写,它是一种功能强大的表达式语言(Expression Languag ...

  8. Struts2框架--学习笔记(下):OGNL表达式、值栈操作、拦截器、struts2标签、文件上传

    一.OGNL概述:OGNL是一种表达式 (1)在struts2中操作值栈数据. (2)一般把ognl在struts2中操作,和struts2标签一起使用操作值栈. (3)ognl不是strut2的一部 ...

  9. OGNL表达式struts2标签“%,#,$”

    http://www.blogjava.net/parable-myth/archive/2010/10/28/336353.html 一.什么是OGNL,有什么特点? OGNL(Object-Gra ...

最新文章

  1. 编程语言发展70年,用50种不同语言输出「Hello World」
  2. Powershell管理系列(二十五)PowerShell操作之获取AD账号及邮箱信息
  3. vue 如何获取图片的原图尺寸_公众号封面图片尺寸是多少?如何在公众号里制作封面图?...
  4. 常用负载均衡策略分析
  5. gtx1660是什么级别的_GTX1660Ti到底属于什么系列?Nvidia一句话定性了
  6. java环境变量代表的含义_java 环境变量的涵义
  7. /proc/sys/net/ipv4 详解2
  8. Syslink Control使用技巧
  9. python学习 day22 (3月29日)----(生成器推导式)
  10. 能识别nvme的pe启动_【腾讯WeTest干货分享】机器学习在启动耗时测试中的应用及模型调优...
  11. C# 英文系统上中文 string 显示乱码
  12. php set_time_limit()的作用是什么
  13. run (简单DP)
  14. Atitit java解析yml文件 以及使用 spel ognl 读取 v4 u77.docx Atitit java解析yml文件 目录 1.1. Springboot use snak
  15. 一次搞定this和闭包
  16. 笔记《深入浅出数据分析》上
  17. 单片机很简单?我们来聊聊如何进阶
  18. 一键进入高通9008模式_高通3040芯片?游戏模式超低延迟?南卡lite pro全新升级!...
  19. HiveSQL小练习--求连续消费的天数
  20. 流氓软件和骚扰电话是时候该清理下了

热门文章

  1. KnockoutJS 3.X API 第一章 简介
  2. Xcode6 itunes完美打包api 方法
  3. Apple 预计于内华达州雷诺市再盖一个数据中心
  4. torch复现论文简明笔记
  5. linux创建表空间 没有权限,Linux oracle数据库创建表空间、用户并赋予权限
  6. Linux内核源码行数,Linux源代码已超过1000万行 价值达5亿美金
  7. php实现电话拨打,jquery mobile实现拨打电话功能的几种方法_jquery
  8. 搜狐2012.9.15校园招聘会笔试题
  9. 信息系统开发平台OpenExpressApp - 功能权限
  10. Insomni'hack teaser 2019 - Misc - curlpipebash