欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Struts2 的OGNL使用简介

程序员文章站 2022-04-18 11:58:09
...
<div align="center">
	  	<ol>
	  		<li>访问值栈中的Action的普通属性:username=<s:property value="username" /> | password=<s:property value="password" />  </li>	
	  		<li>Action的user属性值=<s:property value="user"/></li>
	  		<li>访问值栈中的对象的普通属性:Action的user属性的name属性值=<s:property value="user.name" /> | =<s:property value="user['name']" />  </li>	
	  		<li>访问值栈中的对象的普通属性:Action->cat->firend->name=<s:property value="cat.firend.name" /> </li>
	  		<hr>
	  		<li>
	  			访问值栈中的对象的普通方法username.length():<s:property value="username.length()"/>
	  		</li>
	  		<li>
	  			访问值栈中的对象的普通方法cat.miao():<s:property value="cat.miao()"/>
	  		</li>
	  		<li>
	  			访问Action的普通方法myTest():<s:property value="myTest()"/>
	  		</li>
	  		<hr>
	  		<li>
	  			访问静态方法: <s:property value="@[email protected]()"/>
	  		</li>
	  		<li>
	  			访问静态属性: <s:property value="@[email protected]"/>
	  		</li>
	  		<li>
	  			访问Math类的静态方法: <s:property value="@@max(2,3)"/>
	  		</li>
	  		<hr>
	  		<li>
	  			访问普通类的构造方法: <s:property value="new com.mengya.bean.User()"/>
	  		</li>
	  		<li>
	  			访问普通类的构造方法: <s:property value="new com.mengya.bean.User().setName('mengya')"/>
	  		</li>
	  		<hr>
	  		<li>
	  			访问List:<s:property value="userList" />
	  		</li>
	  		<li>
	  			访问List中的某个元素:<s:property value="userList[1]" />
	  		</li>
	  		<li>
	  			访问List中元素的某个属性的集合:<s:property value="userList.{name}" />
	  		</li>
	  		<li>
	  			访问List中元素的某个属性的集合中的特定值:<s:property value="userList.{name}[1]" /> | <s:property value="userList[1].name" />
	  		</li>
	  		<li>
	  			访问Set:<s:property value="dogSet" />
	  		</li>
	  		<li>
	  			访问Set中的某个元素:<s:property value="dogSet[1]" /><!-- set没有顺序故取不到值 -->
	  		</li>
	  		<li>
	  			访问Map:<s:property value="catMap" />
	  		</li>
	  		<li>
	  			访问Map中某个元素:<s:property value="catMap.cat1" /> | <s:property value="catMap['cat1']" />
	  		</li>
	  		<li>
	  			访问Map中的所有keys:<s:property value="catMap.keys" />
	  		</li>
	  		<li>
	  			访问Map中的所有values:<s:property value="catMap.values" />
	  		</li>
	  		<li>
	  			访问容器的大小:<s:property value="catMap.size()" /> | <s:property value="userList.size()" />
	  		</li>
	  		<hr>
	  		<li>
	  			投影(过滤)user的name为aaa的集合:<s:property value="userList.{?#this.name=='aaa'}" /> | <s:property value="userList.{?#this.name=='aaa'}.{name}" />
	  			<!-- 得到的是一个[[email protected]] | [aaa] 都是集合 --> | <s:property value="userList.{?#this.name=='aaa'}.{name}[0]" />
	  		</li>
	  		<li>
	  			投影(过滤)user的age大于21的第一个元素:<s:property value="userList.{^#this.age>21}" /> | <s:property value="userList.{^#this.age>21}.{name}" />
	  		</li>
	  		<li>
	  			投影(过滤)user的age大于21的第最后一个元素:<s:property value="userList.{$#this.age>21}" /> | <s:property value="userList.{$#this.age>21}.{name}" />
	  		</li>
	  		<li>
	  			投影(过滤)判断:<s:property value="userList.{$#this.age>21}.{name} == null " />
	  		</li>
	  		<hr>
	  		<li>
	  			[0]表示获得的是第一个Action:<s:property value="[0]" />	| <s:property value="[0].username" />
	  		</li>
	  	</ol>
  	</div>

 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.FilterDispatcher
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

struts.xml:

<?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>
	<!-- 允许访问静态方法 -->
	<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
	
	<package name="ognl" extends="struts-default">
		<action name="ognl" class="com.mengya.action.OgnlAction">
			<result name="success">ognl.jsp</result>
		</action>
	</package>
</struts>

 

OgnlAction:

public class OgnlAction extends ActionSupport {

	private String username;

	private String password;

	/**
	 * 说明:在这里若user对象没有实例化,在页面访问action时给user的属性赋值了,struts2会自动帮我们实例化该user对象
	 * 前提是User对象提供了无参的构造方法。若页面没有给user的属性赋值则user为null(针对本实例,因为execute方法没有任何操作)
	 * 在这里若对user对象实例化了,则无论在页面访问action时是否也user的属性赋值,user都不会是null
	 */
	private User user = null;

	// private User user = new User();

	private Cat cat = null;

	private Common common = null;

	private List<User> userList = null;

	private Set<Dog> dogSet = null;

	public Map<String, Cat> catMap = null;

	@Override
	public String execute() throws Exception {
		// cat = new Cat();
		// Dog dog =new Dog();
		// dog.setName("Dog");
		// cat.setFirend(dog);
		userList = new ArrayList<User>();
		User u = new User();
		u.setName("aaa");
		u.setAge(21);
		userList.add(u);
		u = new User();
		u.setName("bb");
		u.setAge(22);
		userList.add(u);
		u = new User();
		u.setName("cc");
		u.setAge(23);
		userList.add(u);

		dogSet = new HashSet<Dog>();
		Dog d = new Dog();
		d.setName("dog1");
		dogSet.add(d);
		d = new Dog();
		d.setName("dog2");
		dogSet.add(d);
		d = new Dog();
		d.setName("dog3");
		dogSet.add(d);

		catMap = new HashMap<String, Cat>();
		catMap.put("cat1", new Cat());
		catMap.put("cat2", new Cat());
		catMap.put("cat3", new Cat());
		return SUCCESS;
	}

	public String myTest() {
		return "myTest方法";
	}

	public Cat getCat() {
		return cat;
	}

	public String getPassword() {
		return password;
	}

	public User getUser() {
		return user;
	}

	public String getUsername() {
		return username;
	}

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

	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 Common getCommon() {
		return common;
	}

	public void setCommon(Common common) {
		this.common = common;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public Set<Dog> getDogSet() {
		return dogSet;
	}

	public void setDogSet(Set<Dog> dogSet) {
		this.dogSet = dogSet;
	}

	public Map<String, Cat> getCatMap() {
		return catMap;
	}

	public void setCatMap(Map<String, Cat> catMap) {
		this.catMap = catMap;
	}
}

 Action中所引用的Bean:

public class Cat {
	private String name;

	private Dog firend;

	public Dog getFirend() {
		return firend;
	}

	public void setFirend(Dog firend) {
		this.firend = firend;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String miao(){
		return "cat的普遍方法调用";
	}
	
}

 

public class Dog {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

 

public class User {
	private String name;

	private int age;

	public String getName() {
		return name;
	}

	public String setName(String name) {
		this.name = name;
		return toString();
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

 

测试: