struts2之OGNL表达式

OGNL表达式一般用来取值,下面通过这个例子程序,来看看ognl表达式

(1)使用ognl表达式的时候需要在struts.xml文件中配置这句话

<span style="font-size:18px;"> <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant></span>

struts.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<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/dqpi/eonline/action/ognl.xml"/>
</struts></span>
ognl.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="ognl"  extends="struts-default">
        <action name="ognl" class="com.dqpi.eonline.action.OgnlAction">
			<result>/ognl.jsp</result>        
        </action>
    </package>
</struts></span>
OgnlAction.java

<span style="font-size:18px;">package com.dqpi.eonline.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 Cat cat;
	private Map<String, Dog> dogMap = new HashMap<String, Dog>();

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

	private String name;

	private String password;

	private User user;
	// private User user = new User();

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

	@Override
	public String execute() throws Exception {
		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"));
		return SUCCESS;
	}

	public Cat getCat() {
		return cat;
	}

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

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

	public String getName() {
		return name;
	}

	public String getPassword() {
		return password;
	}

	public User getUser() {
		return user;
	}

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

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

	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 setName(String name) {
		this.name = name;
	}

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

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

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

<span style="font-size:18px;">package com.dqpi.eonline.action;

public class Dog {
	private String name;

	public Dog() {
		super();
	}

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

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		return "dog" + name;
	}
}
</span>
Cat.java

<span style="font-size:18px;">package com.dqpi.eonline.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";
	}
}
</span>
S.java

<span style="font-size:18px;">package com.dqpi.eonline.action;

public class S {
	public static String STR = "STATIC STRING";
	public static String s(){
		return "static method";
	}
}
</span>
User.java

<span style="font-size:18px;">package com.dqpi.eonline.action;

public class User {
	private int age=9;
	
	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;
	}

	@Override
	public String toString() {
		return "user" + age;
	}

}
</span>
index.jsp

<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>resultType.jsp</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
	<a href="ognl?name=a&password=p">访问OGNL属性</a><br/>
  </body>
</html>
</span>
ognl.jsp

<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'ognl.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   		访问值栈中action的普通属性:username=<s:property value="name"/><br/>
   		访问值栈中对象的普通属性(get/set方法):<s:property value="user.age"/><br/>
   		访问值栈中对象的普通属性(get/set方法):<s:property value="cat.friend.name"/><br/>
   		访问值栈中的普通方法:<s:property value="password.length()"/><br/>
   		访问值栈中对象的普通方法:<s:property value="cat.miaomiao()"/><br/>
   		访问action中的普通方法:<s:property value="m()"/>
   		<hr/>
   		<!--第一个@后面加包名类名,第二个@后面加方法名  -->
   		访问静态方法:<s:property value="@com.dqpi.eonline.action.S@s()"/><br/>
   		<!--第一个@后面加包名类名,第二个@后面加属性名  -->
   		访问静态属性:<s:property value="@com.dqpi.eonline.action.S@STR"/><br/>
   		访问Math类的静态方法:<s:property value="@@max(2,3)"/>
   		<hr/>
   		访问普通类的构造方法:<s:property value="new com.dqpi.eonline.action.User(8)"/>
   		<hr/>
   		访问List:<s:property value="users"/><br/>
   		访问List中的某个元素:<s:property value="users[1]"/><br/>
   		访问List中某个属性的集合:<s:property value="users.{age}"/><br/>
   		访问List中某个属性集合中特定的值:<s:property value="users.{age}[0]"/>|<s:property value="users.[0].age"/><br/>
   		访问set:<s:property value="dogs"/><br/>
   		访问set中某个元素:<s:property value="dogs[1]"/><br/>
   		访问map:<s:property value="dogMap"/><br/>
   		访问map中某个元素:<s:property value="dogMap.dog101" /><br/>
   		访问map中的所有Key:<s:property value="dogMap.keys"/><br/>
   		访问map中的所有value:<s:property value="dogMap.values"/><br/>
   		访问容器的大小:<s:property value="dogMap.size()"/>
   		<hr/>
   		投影:<s:property value="users.{?#this.age==1}.{age}"/><br/>
   		投影:<s:property value="users.{?#this.age==1}"/><br/>
   		投影:<s:property value="users.{?#this.age==1}[0]"/><br/>
   		投影:<s:property value="users.{^#this.age>1}.{age}"/><br/>
   		投影:<s:property value="users.{$#this.age>1}.{age}"/><br/>
   		投影:<s:property value="users.{$#this.age>1}.{age}==null"/>
   		<hr/>
   		[]:<s:property value="[0]"/>
   		<s:debug></s:debug>
		
  </body>
</html>
</span>










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OGNL(Object-Graph Navigation Language,对象图导航语言)是一个强大的表达式语言,可以用于Java的各种应用程序中,包括Struts、JavaServer Faces、JavaServer Pages等。在Mybatis中,OGNL表达式可以用于Mapper XML文件中的各种标签中,例如`<if>`、`<where>`、`<set>`、`<foreach>`等标签中。 OGNL表达式可以用于获取Java对象的属性值、调用Java对象的方法、进行算术运算、比较运算、逻辑运算等。例如: ```xml <select id="selectByCondition" resultType="com.example.User"> select * from user where name like #{keyword} and age >= #{minAge} <if test="maxAge != null"> and age <= #{maxAge} </if> </select> ``` 在这个例子中,`#{keyword}`、`#{minAge}`、`#{maxAge}`都是OGNL表达式,表示获取Java对象中的属性值。例如,如果传入的参数对象是一个`User`对象,那么`#{keyword}`可以表示`user.getKeyword()`方法的返回值,`#{minAge}`可以表示`user.getMinAge()`方法的返回值。 在OGNL表达式中,还可以进行算术运算、比较运算、逻辑运算等。例如,`age >= #{minAge}`表示将`age`和`#{minAge}`进行比较,判断`age`是否大于等于`#{minAge}`;`age <= #{maxAge}`表示将`age`和`#{maxAge}`进行比较,判断`age`是否小于等于`#{maxAge}`。 除了基本的运算符和表达式OGNL还提供了丰富的函数和操作符,例如`in`操作符、`not`操作符、`contains`函数、`size`函数等。这些函数和操作符可以方便地进行集合操作、字符串操作等。在使用OGNL表达式时,需要注意语法的正确性和安全性,以避免可能的安全漏洞。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值