OGNL表达式语言学习


<?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 />
<!-- 必须在struts.xml中加入 <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>,其默认为FALSE,要给为true -->
<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><!-- 因为set中没有顺序,所以不能用下标访问其值 -->
<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><!-- ?#表示判断条件, 循环到那个,那个就是this,拿出age=1的那个user对象,此处取得集合的第一个,在结果中,加了中括号的就是集合 -->
<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>
<hr />
<!-- 在值栈里从上往下一直数,一直到栈底的Action对象, 如果是第一个Action中没有username这个属性,则找下一个-->
<li>[]:<s:property value="[0].username"/></li>
</ol>
<s:debug></s:debug>
</body>
</html>



<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="ognl" extends="struts-default">

<action name="ognl" class="com.hugui.struts2.ognl.OgnlAction">
<result>/ognl.jsp</result>
</action>
<action name="test" class="com.hugi.struts2.ognl.TestAction">
<result type="chain">ognl</result>
</action>

</package>
</struts>




//其中用的一些class 就不写了
package com.hugui.struts2.ognl;

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;

public class OgnlAction extends ActionSupport {
private Cat cat;
private Map<String, Dog> dogMap = new HashMap<String, Dog>();

private Set<Dog> dogs = new HashSet<Dog>();

private String password;

//(domain model)只有往User里面传递参数(User.age),且User 一定要提供一个参数为空的构造方法,才会给你构造User对像,也可以手动new ,private User user= new User();
private User user;
private String username;

private List<User> users = new ArrayList<User>();

public OgnlAction() {
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("dog100", new Dog("dog100"));
dogMap.put("dog101", new Dog("dog101"));
dogMap.put("dog102", new Dog("dog102"));

}

public String execute() {
return SUCCESS;
}

public Cat getCat() {
return cat;
}

public Map<String, Dog> getDogMap() {
return dogMap;
}

public Set<Dog> getDogs() {
return dogs;
}

public String getPassword() {
return password;
}

public User getUser() {
return user;
}

public String getUsername() {
return username;
}

public List<User> getUsers() {
return users;
}

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

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

public void setDogMap(Map<String, Dog> dogMap) {
this.dogMap = dogMap;
}

public void setDogs(Set<Dog> dogs) {
this.dogs = dogs;
}

public void setPassword(String password) {
this.password = password;
}

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

public void setUsername(String username) {
this.username = username;
}

public void setUsers(List<User> users) {
this.users = users;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值