1、访问值栈中的action的普通属性:

请求:

<a href="ognl.action?username=u&password=p">访问属性</a>

对应的action代码,getter和setter方法一定要加上:

     private String username;

     private String password;

         public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}

页面进行访问:

 username = <s:property value="username" /> | password = <s:property value="password" />

2、访问值栈中对象的普通属性(get set方法)

如果请求没有将 user的值传过去,那么在值栈就会看到 user=null,只有为user赋值了,struts2才会构造一个User对象然后赋给user。

只有传才会构造。

想初始化 domain model,可以 new,也可以传参数值,但这时候需要提供参数为空的构造方法。

请求:

action 中的代码:

         private User user;

     public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
}

属性访问:

<s:property value="user.age" />

3、案例三

现在定义了 Dog 类:

 package com.bjsxt.struts2.ognl;

 public class Dog {

     private String name;

     public Dog(){}

     public Dog(String name){
this.name = name;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public String toString(){
return "dog:" + this.name;
} }

Cat 类,类中有个friend 是 Dog 类

 package com.bjsxt.struts2.ognl;

 public class Cat {

     private Dog friend;

     public Dog getFriend() {
return friend;
} public void setFriend(Dog friend) {
this.friend = friend;
} public String miaomiao(){
return "miaomiao";
}
}

在 Action 中定义了 Cat 对象

     private Cat cat;

     public Cat getCat() {
return cat;
} public void setCat(Cat cat) {
this.cat = cat;
}

请求:

显示:

4、案例四:OGNL表达式调用值栈中对象的方法

action中是有定义password属性的。

类似案例:

Cat类中是由miaomiao()方法的。

5、调用action的普通方法

action 先定义一个普通方法:

public String m(){
return "hello";
}

页面调用:

<s:property value="m()"/>

========================================================================

访问静态方法:

先在struts.xml设置允许访问静态方法

<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>

定义的示例类:

 package com.bjsxt.struts2.ognl;

 public class S {

     public static String STR = "STATIC STRING";

     public static String s(){
return "this is a static method().";
} }

访问代码:

 访问静态方法:<s:property value="@com.bjsxt.struts2.ognl.S@s()"/>
访问静态变量:<s:property value="@com.bjsxt.struts2.ognl.S@STR"/>
访问Math类的静态方法:<s:property value="@@max(2,3)"/>

6、访问普通类的构造方法

访问普通类的构造方法:<s:property value="new com.bjsxt.struts2.ognl.User(8)" />

结果:

7、访问集合元素

数组访问方式跟访问List一样

8、投影

User 代码:

 package com.bjsxt.struts2.ognl;

 public class User {

     private Integer age = 10;

     public User(){}

     public User(int age){
this.age = age;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String toString(){
return "user" + age;
} }

名称为 users 的List

 private List<User> users = new ArrayList<User>();
users.add(new User(1));
users.add(new User(2));
users.add(new User(3));
users.add(new User(4));

页面代码:

 <li>投影(过滤):<s:property value="users.{?#this.age==1}.{age}"/></li><!-- ?#后面接过滤条件 这句话是取所有age大于1的所有值 -->
<li>投影:<s:property value="users.{^#this.age>1}.{age}" /></li><!-- ^#开头 这句话是取age大于1的第一个值 -->
<li>投影:<s:property value="users.{$#this.age>1}.{age}" /></li><!-- $#结尾 这句话是取age大于1的最后那个值-->
<li>投影:<s:property value="users.{^#this.age>1}.{age} == null" /></li>

结果:

9、[]

[] 访问的是值栈

[索引] 访问对应位置的对象

[索引].对象名 访问索引对象包含的Property

<s:property value="[0].locale"/>

小问题:一个请求从一个Action容器内跳转到另一个Action,那么在值栈中就会有两个Action。例子如下:

struts.xml

TestAction

 package com.bjsxt.struts2.ognl;

 import com.opensymphony.xwork2.ActionSupport;

 public class TestAction extends ActionSupport {

     private static final long serialVersionUID = 7420682335394931465L;

     public String execute(){
return SUCCESS;
}
}

OgnlAction

     public OgnlAction(){
users.add(new User(1));
users.add(new User(2));
users.add(new User(3));
users.add(new User(4)); dogs.add(new Dog("dog1"));
dogs.add(new Dog("dog2"));
dogs.add(new Dog("dog3")); dogMap.put("dog100", new Dog("100"));
dogMap.put("dog101", new Dog("101"));
dogMap.put("dog102", new Dog("102"));
} public String execute() throws Exception {
return super.execute();
}

调用了两个Action后,ValueStack中的内容 TestAction 先进栈,OgnlAction 后进栈:

OGNL 常用的就上面这些。

链接: http://pan.baidu.com/s/1c8Hima 密码: gkch

最新文章

  1. windows核心编程 - 线程基础
  2. 从Evernote迁移到Wiz
  3. codeforces 723B:Text Document Analysis
  4. Find Minimum in Rotated Sorted Array leetcode java
  5. 锋利的jQuery-1--解决jquery库和其他库的冲突
  6. Linux 查看CPU信息、机器型号等硬件信息
  7. web的各种前端打印方法之CSS控制网页打印样式
  8. 请慢慢移动……由于操作快慢导致的bug
  9. inline-block 前世今生
  10. Hibernate 二级缓存配置出现的异常
  11. 【IE6的疯狂之十一】CSS的优先级及!important在IE6下的BUG
  12. Eclipse debug 调试快捷键
  13. Flex和Java通信报错
  14. 【SSH系列】深入浅出SpringMvc+入门Demo
  15. fashion datasets图像检索实践project
  16. STS中如何使用lombok
  17. 一步步分析为什么B+树适合作为索引的结构
  18. memcmp与strncmp函数【转】
  19. Error: Apache shutdown unexpectedly --解决
  20. bash基本命令速查表

热门文章

  1. C语言中的副作用、序列点、完整表达式
  2. 【笔记】Django基础(一)
  3. Codeforces Round #532 (Div. 2)- C(公式计算)
  4. void类型指针的基本用法
  5. linux虚拟机管理
  6. LeeCode(No4 - Median of Two Sorted Arrays)
  7. 正确理解ThreadLocal:ThreadLocal中的值并不一定是完全隔离的
  8. MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建
  9. volatile的作用和原理
  10. jndi理解