程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> Struts2 OGNL表達式

Struts2 OGNL表達式

編輯:關於JSP

Struts2 OGNL表達式


1、index.jsp


訪問屬性 ognl
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">
</include></constant></constant></struts>
3.ognl.xml
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true">
 <constant name="struts.devMode" value="true">
	<package extends="struts-default" name="ognl">
		<action class="com.wxh.action.OgnlAction" name="ognl">
			<result>/ognl.jsp</result>
		</action>
		<action class="com.wxh.action.TestAction" name="test">
			<result type="chain">ognl</result>
		</action>
	</package>
</constant></constant></struts>

4.ognl.jsp






  1. 訪問值棧中的action的普通屬性:username=
  2. 訪問值棧中的對象的普通屬性(get set 方法):|
  3. 訪問值棧中的對象的普通屬性(get set 方法):
  4. 訪問值棧中的對象的普通方法:
  5. 訪問值棧中的對象的普通方法:
  6. 訪問值棧中action的普通方法:
  7. 訪問靜態方法:
  8. 訪問靜態屬性:
  9. 訪問Math類的靜態方法:
  10. 訪問普通類的構造方法:
  11. 訪問List:
  12.  
  13. 訪問List中某個元素:
  14.  
  15. 訪問List中元素某個屬性的集合:
  16.  
  17. 訪問List中元素某個屬性的集合中的特定值:|
  18.  
  19. 訪問Set:
  20. 訪問Set中的某個元素:
  21. 訪問Map:
  22. 訪問Map中某個元素:||
  23. 訪問Map中所有的key:
  24. 訪問Map中所有的value:
  25. 訪問容器的大小:|
  26. 投影(過濾):
  27. 投影:
  28. 投影:
  29. 投影:
  30. []:

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 users=new ArrayList();
	private Set dogs=new HashSet();
	private Map dogMap=new HashMap();
	
	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 getUsers() {
		return users;
	}



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



	public Set getDogs() {
		return dogs;
	}



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



	public Map getDogMap() {
		return dogMap;
	}



	public void setDogMap(Map 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;
	}

}
思考:在做一些實驗的時候某些功能並沒有展現。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved