Struts2框架(三) ------ OGNL表达式
程序员文章站
2022-06-02 14:30:31
...
一 ognl基本语法
1.核心思想
2.基本语法
2.1 创建一个User类
public class User {
private String name;
private Integer 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;
}
public User(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public User() {
super();
}
}
2.2 创建一个工具类public class StaticUtils {
public static Object echo(Object o){
return o;
}
}
2.3 通过构造函数获取属性的值 @Test
public void fun1() throws OgnlException{
//1.初始化root
User rootUser = new User("AMYSKY",3);
//2.初始化ognlcontext
Map<String,User> context = new HashMap<String,User>();
context.put("user1",new User("tom",18));
context.put("user2",new User("lucy",22));
OgnlContext oc = new OgnlContext();
//3.封装ognl中的root部分
oc.setRoot(rootUser);
//4.封装Context部分
oc.setValues(context);
//5.书写ognl表达式 获取root部分的值
Object name = Ognl.getValue("name", oc, oc.getRoot());
Object age = Ognl.getValue("age", oc, oc.getRoot());
System.out.println("name ="+name+",age="+age);
//6.获取context部分的值
Object user1name = Ognl.getValue("#user1.name", oc, oc.getRoot());
Object user1age = Ognl.getValue("#user1.age", oc, oc.getRoot());
System.out.println("user1name ="+user1name+",user1age="+user1age);
}
2.4 为属性赋值
@Test
public void fun2() throws OgnlException{
//1.初始化root
User rootUser = new User("AMYSKY",3);
//2.初始化ognlcontext
Map<String,User> context = new HashMap<String,User>();
context.put("user1",new User("tom",18));
context.put("user2",new User("lucy",22));
OgnlContext oc = new OgnlContext();
//3.封装ognl中的root部分
oc.setRoot(rootUser);
//4.封装Context部分
oc.setValues(context);
//5.书写ognl表达式 ,为root属性赋值
Object name = Ognl.getValue("name='zhangsan'", oc, oc.getRoot());
Object age = Ognl.getValue("age= 30", oc, oc.getRoot());
System.out.println("name ="+name+",age="+age);
//6.为context部分的属性先赋值后获取
Object user1name = Ognl.getValue("#user1.name='天元陆兵',#user1.name", oc, oc.getRoot());
Object user1age = Ognl.getValue("#user1.age=3,#user1.age", oc, oc.getRoot());
System.out.println("user1name ="+user1name+",user1age="+user1age);
}
2.5 通过setxx()和getxx()方法为属性赋值
@Test
public void fun3() throws OgnlException{
//1.初始化root
User rootUser = new User("AMYSKY",3);
//2.初始化ognlcontext
Map<String,User> context = new HashMap<String,User>();
context.put("user1",new User("tom",18));
context.put("user2",new User("lucy",22));
OgnlContext oc = new OgnlContext();
//3.封装ognl中的root部分
oc.setRoot(rootUser);
//4.封装Context部分
oc.setValues(context);
//5.书写ognl表达式 ,调用set()方法,为root属性赋值
Ognl.getValue("setName('lisi')", oc, oc.getRoot());
Object name = Ognl.getValue("getName()", oc, oc.getRoot());
Ognl.getValue("setAge(20)", oc, oc.getRoot());
Object age = Ognl.getValue("getAge()", oc, oc.getRoot());
System.out.println("name ="+name+",age="+age);
//6.为context部分的属性调用set()方法赋值
Object user1name = Ognl.getValue("#user1.setName('天元神兵'),#user1.getName()", oc, oc.getRoot());
Object user1age = Ognl.getValue("#user1.setAge(3),#user1.getAge()", oc, oc.getRoot());
System.out.println("user1name ="+user1name+",user1age="+user1age);
}
2.6 使用静态方法赋值
@Test
public void fun4() throws OgnlException{
//1.初始化root
User rootUser = new User("AMYSKY",3);
//2.初始化ognlcontext
Map<String,User> context = new HashMap<String,User>();
context.put("user1",new User("tom",18));
context.put("user2",new User("lucy",22));
OgnlContext oc = new OgnlContext();
//3.封装ognl中的root部分
oc.setRoot(rootUser);
//4.封装Context部分
oc.setValues(context);
//5.书写ognl表达式 ,使用静态方法为root属性赋值
Object name = Ognl.getValue("@aaa@qq.com('乔峰')", oc, oc.getRoot());
Object age = Ognl.getValue("@aaa@qq.com(34)", oc, oc.getRoot());
System.out.println("name ="+name+",age="+age);
//6.为context部分的属性调用静态方法赋值 如果是Math类可以省略
Object user1name = Ognl.getValue("@aaa@qq.com", oc, oc.getRoot());
Object user1age = Ognl.getValue("@@PI", oc, oc.getRoot());
System.out.println("user1name ="+user1name+",user1age="+user1age);
}
2.7 list和Map对象赋值
@Test
public void fun5() throws OgnlException{
//1.初始化root
User rootUser = new User("AMYSKY",3);
//2.初始化ognlcontext
Map<String,User> context = new HashMap<String,User>();
context.put("user1",new User("tom",18));
context.put("user2",new User("lucy",22));
OgnlContext oc = new OgnlContext();
//3.封装ognl中的root部分
oc.setRoot(rootUser);
//4.封装Context部分
oc.setValues(context);
//5.书写ognl表达式 ,创建list对象
Integer size = (Integer) Ognl.getValue("{'tom','jim','lilei','lucy','lili'}.size()", oc, oc.getRoot());
String name = (String) Ognl.getValue("{'tom','jim','lilei','lucy','lili'}[3]", oc, oc.getRoot());
String name1 = (String) Ognl.getValue("{'tom','jim','lilei','lucy','lili'}.get(1)", oc, oc.getRoot());
System.out.println("size ="+size+",name="+name+",name1="+name1);
//6.创建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 ="+size2+",name3="+name3+",age="+age);
}
二.OGNL与Struct2结合
1.原理
2.属性驱动接收参数原理
3.对象驱动接收参数原理
4.模型驱动接收参数原理
/**
* * 下面的两个接口在真正开发的时候我们只用其一就可以
* 一般是实现ModelDrive接口
*/
public class Demo2Action extends ActionSupport implements Preparable,ModelDriven<User>{
private User user = new User();
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
/**
* 查看Preparable接口的源码可以现模型驱动方式接收数据需要我们手动压栈
*/
@Override
public void prepare() throws Exception {
//压入栈顶
//1获得值栈
ValueStack vs = ActionContext.getContext().getValueStack();
//2将u压入栈顶
vs.push(user);
}
/**
* ModelDriven的源码已经实现了压栈操作
* 我们只需返回就可以了。
* @return
*/
@Override
public User getModel() {
return user;
}
}
5.配置文件中使用ognl表达式获取调转的动态参数
public class Demo3Action extends ActionSupport{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() throws Exception {
name ="amysky";
return SUCCESS;
}
}
三.Struts2源码扩展
request的查找顺序
推荐阅读
-
Struts2 OGNL表达式、ValueStack
-
Struts2 (三) — OGNL与值栈
-
记录后端之旅:初学S2SH框架,整合struts2、spring、Hibernate三大框架
-
Struts2框架与Spring框架初解三
-
Struts2深入学习----OGNL表达式原理
-
Struts2,Hibernate,Sring三种基本框架整合方法(一)
-
Struts2 OGNL表达式、ValueStack
-
Java EE SSH框架之Struts2 (5)—— Struts2与OGNL表达式的结合
-
【JavaEE笔记】Struts2 (三) ognl、值栈操作等
-
Struts2框架(三) ------ OGNL表达式