Struts2 OGNL表达式

1、index.jsp

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


2.struts.xml

<struts>
 <constant name="struts.enable.DynamicMethodInvocation" value="true" />
 <constant name="struts.devMode" value="true" />
 <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
 <include file="/com/wxh/action/ognl.xml"/>
</struts>

3.ognl.xml

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
 <constant name="struts.devMode" value="true" />
	<package name="ognl" extends="struts-default">
		<action name="ognl" class="com.wxh.action.OgnlAction">
			<result>/ognl.jsp</result>
		</action>
		<action name="test" class="com.wxh.action.TestAction">
			<result type="chain">ognl</result>
		</action>
	</package>
</struts>

4.ognl.jsp

<body>
	<ol>
		<li>访问值栈中的action的普通属性:username=<s:property value="username"/></li>
		<li>访问值栈中的对象的普通属性(get set 方法):<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/>
		<li>访问静态方法:<s:property value="@com.wxh.action.S@s()"/></li>
		<li>访问静态属性:<s:property value="@com.wxh.action.S@STR"/></li>
		<li>访问Math类的静态方法:<s:property value="@@max(2,3)"/></li>
		<hr/>
		<li>访问普通类的构造方法:<s:property value="new com.wxh.action.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>
		<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}.{age}"/></li>
		<li>投影:<s:property value="users.{^#this.age>1}.{age}"/></li>
		<li>投影:<s:property value="users.{$#this.age>1}.{age}"/></li>
		<li>投影:<s:property value="users.{$#this.age>1}.{age}==null"/></li>
		<hr/>
		<li>[]:<s:property value="[0].username"/></li>
	</ol>
	<s:debug></s:debug>
</body>

5.OgnlAction.java

package com.wxh.action;


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 static final long serialVersionUID = 1L;
	private String password;
	private String username;
	private User user;
	private Cat cat;
	
	private List<User> users=new ArrayList<User>();
	private Set<Dog> dogs=new HashSet<Dog>();
	private Map<String,Dog> dogMap=new HashMap<String,Dog>();
	
	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 List<User> getUsers() {
		return users;
	}



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



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



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



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



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



	public String m(){
		return "hello";
	}
	
	public Cat getCat() {
		return cat;
	}

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

	public User getUser() {
		return user;
	}

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

	public String getPassword() {
		return password;
	}

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

	public String getUsername() {
		return username;
	}

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

6.Cat.java

package com.wxh.action;

public class Cat {
	private Dog friend;

	public Dog getFriend() {
		return friend;
	}

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

}

7.Dog.java

package com.wxh.action;

public class Dog {
	public Dog(){
		
	}
	public Dog(String anme){
		super();
		this.name=name;
	}
	private String name;

	public String getName() {
		return name;
	}

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

}

8.S.java

package com.wxh.action;

public class S {
	public static String STR="STATIC STRING";
	
	public static String s(){
		return "static method";
	}
}


9.User.java

package com.wxh.action;

public class User {	
	private int age;
	
	public User(){
		System.out.println("user.constructor!");
	}
	public User(int age){
		super();
		this.age=age;
	}
	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String toString(){
		return "user"+age;
	}

}


思考:在做一些实验的时候某些功能并没有展现。

你可能感兴趣的:(struts2.0)