博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2--OGNL语法详解(取值、赋值、调用普通方法、调用静态方法、创建List和Map)
阅读量:2441 次
发布时间:2019-05-10

本文共 5852 字,大约阅读时间需要 19 分钟。

创建项目并导包

在这里插入图片描述

创建一个User类

package cn.itheima.bean;public class User {
private String name; private Integer age; public User() {
super(); } public User(String name, Integer age) {
super(); this.name = name; this.age = age; } public String getName() {
return name; } public void setName(String name) {
this.name = name; } public Integer getAge() {
return age; } public void setAge(Integer age) {
this.age = age; } @Override public String toString() {
return "User [name=" + name + ", age=" + age + "]"; }}

准备工作

@Test//准备工作public void fun1() throws Exception{
//准备ONGLContext //准备Root User rootUser = new User("tom",18); //准备Context Map
context = new HashMap
(); context.put("user1", new User("jack",18)); context.put("user2", new User("rose",22)); OgnlContext oc = new OgnlContext(); //将rootUser作为root部分 oc.setRoot(rootUser); //将context这个Map作为Context部分 oc.setValues(context); //书写OGNL Ognl.getValue("", oc, oc.getRoot());}

取root中的属性值

@Test//取出root中的属性值public void fun2() throws Exception{
//准备ONGLContext //准备Root User rootUser = new User("tom",18); //准备Context Map
context = new HashMap
(); context.put("user1", new User("jack",18)); context.put("user2", new User("rose",22)); OgnlContext oc = new OgnlContext(); oc.setRoot(rootUser); oc.setValues(context); //书写OGNL //取出root中user对象的name属性 String name = (String) Ognl.getValue("name", oc, oc.getRoot()); Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot()); System.out.println(name); System.out.println(age);}

运行Junit测试,控制台打印:

tom18

取出context中的属性值

public void fun3() throws Exception{
//准备ONGLContext //准备Root User rootUser = new User("tom",18); //准备Context Map
context = new HashMap
(); context.put("user1", new User("jack",18)); context.put("user2", new User("rose",22)); OgnlContext oc = new OgnlContext(); oc.setRoot(rootUser); oc.setValues(context); //书写OGNL //取出context中键为user1对象的name属性 String name = (String) Ognl.getValue("#user1.name", oc, oc.getRoot()); String name2 = (String) Ognl.getValue("#user2.name", oc, oc.getRoot()); Integer age = (Integer) Ognl.getValue("#user2.age", oc, oc.getRoot()); System.out.println(name); System.out.println(name2); System.out.println(age);}

运行Junit测试,控制台打印:

jackrose22

为属性赋值

@Test//基本语法演示//为属性赋值public void fun4() throws Exception{
//准备ONGLContext //准备Root User rootUser = new User("tom",18); //准备Context Map
context = new HashMap
(); context.put("user1", new User("jack",18)); context.put("user2", new User("rose",22)); OgnlContext oc = new OgnlContext(); oc.setRoot(rootUser); oc.setValues(context); //书写OGNL //将root中的user对象的name属性赋值 Ognl.getValue("name='jerry'", oc, oc.getRoot()); String name = (String) Ognl.getValue("name", oc, oc.getRoot()); //将Context中的user对象的name属性赋值 //可以使用(,)将表达式进行串联执行,但是多个表达式串联,如果有多个返回值,只返回最后一个表达式的 String name2 = (String) Ognl.getValue("#user1.name='二狗', #user1.name", oc, oc.getRoot()); System.out.println(name); System.out.println(name2);}

运行Junit测试,控制台打印:

jerry二狗

调用方法

@Test//调用方法public void fun5() throws Exception{
//准备ONGLContext //准备Root User rootUser = new User("tom",18); //准备Context Map
context = new HashMap
(); context.put("user1", new User("jack",18)); context.put("user2", new User("rose",22)); OgnlContext oc = new OgnlContext(); oc.setRoot(rootUser); oc.setValues(context); //书写OGNL //调用root中user对象的setName方法 Ognl.getValue("setName('lilei')", oc, oc.getRoot()); String name = (String) Ognl.getValue("getName()", oc, oc.getRoot()); //调用context中user对象的setName方法 String name2 = (String) Ognl.getValue("#user1.setName('lucy'),#user1.getName()", oc, oc.getRoot()); System.out.println(name); System.out.println(name2);}

运行Junit测试,控制台打印:

lileilucy

调用静态方法

创建一个工具类,定义一个静态方法:

package pers.zhang.Utils;public class HahaUtils {
//回音方法 public static Object echo(Object o){
return o; }}
@Test//基本语法演示//调用静态方法public void fun6() throws Exception{
//准备ONGLContext //准备Root User rootUser = new User("tom",18); //准备Context Map
context = new HashMap
(); context.put("user1", new User("jack",18)); context.put("user2", new User("rose",22)); OgnlContext oc = new OgnlContext(); oc.setRoot(rootUser); oc.setValues(context); //书写OGNL //@+完整类名+@+方法 String name = (String) Ognl.getValue("@pers.zhang.Utils@echo('hello 强勇!')", oc, oc.getRoot()); //Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot()); //访问Math的简写 Double pi = (Double) Ognl.getValue("@@PI", oc, oc.getRoot()); System.out.println(name); System.out.println(pi);}

运行Junit测试,控制台打印:

hello 二狗3.141592653589793

ognl创建对象(List,Map)

@Test//基本语法演示//ognl创建对象-list|mappublic void fun7() throws Exception{
//准备ONGLContext //准备Root User rootUser = new User("tom",18); //准备Context Map
context = new HashMap
(); context.put("user1", new User("jack",18)); context.put("user2", new User("rose",22)); OgnlContext oc = new OgnlContext(); oc.setRoot(rootUser); oc.setValues(context); //书写OGNL //创建list对象 Integer size = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, oc.getRoot()); String name = (String) Ognl.getValue("{'tom','jerry','jack','rose'}[0]", oc, oc.getRoot()); String name2 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}.get(1)", oc, oc.getRoot()); /*System.out.println(size); System.out.println(name); System.out.println(name2);*/ //创建Map对象 Integer size2 = (Integer) Ognl.getValue("#{'name':'tom','age':18}.size()", oc, oc.getRoot()); String name3 = (String) Ognl.getValue("#{'name':'tom','age':18}['name']", oc, oc.getRoot()); Integer age = (Integer) Ognl.getValue("#{'name':'tom','age':18}.get('age')", oc, oc.getRoot()); System.out.println(size2); System.out.println(name3); System.out.println(age);}

运行Junit测试,控制台输出:

2tom18

转载地址:http://dasqb.baihongyu.com/

你可能感兴趣的文章
使用CentOS 8进行初始服务器设置
查看>>
ecmascript v3_节点v12中的新ECMAScript模块简介
查看>>
盖茨比乔布斯_通过盖茨比使用Airtable
查看>>
mern技术栈好处?_如何开始使用MERN堆栈
查看>>
路由器接路由器_路由器之战:到达路由器vsReact路由器
查看>>
rxjs 搜索_如何使用RxJS构建搜索栏
查看>>
如何在Debian 10上安装MariaDB
查看>>
react-notifications-component,一个强大的React Notifications库
查看>>
如何在Ubuntu 18.04上安装Apache Kafka
查看>>
如何为Python 3设置Jupyter Notebook
查看>>
express中间件_创建自己的Express.js中间件
查看>>
如何在Ubuntu 18.04上使用Docker和Caddy远程访问GUI应用程序
查看>>
Apache配置错误AH00558:无法可靠地确定服务器的标准域名
查看>>
apache 证书配置_Apache配置错误AH02572:无法配置至少一个证书和密钥
查看>>
web设置字体粗细css_Web上使用CSS的可变字体
查看>>
css 垂直对齐_CSS垂直对齐属性
查看>>
为您的网站提供动力的100种Jamstack工具,API和服务
查看>>
api restful_构建RESTful API的13种最佳实践
查看>>
wordpress用途_8个热门WordPress多用途主题及其炫酷功能
查看>>
用于Angular,React和Vue.js的Bootstrap UI库
查看>>