示例:D:/Library/myeclipse/workspace/Struts2_0900_OGNL/
1,Object Graph Navigation Language
2,user.xxx只有传才会构造user对象,想初始化Domain Model,可以自己new,也可以传参数值,但必须保证domain model中有空的构造函数。
示例过程:
采用迭代式软件开发结构;
Start:使用Domain Model来传值:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml
<?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.devMode" value="false" />
<constant name="struts.i18n.encoding" value="GBK"></constant>
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="GBK"></constant>
<!--这里使用了模块包含:include -->
<include file="/com/awei/struts2/ognl/ognl.xml"></include>
<!-- Add packages here -->
</struts>
com.awei.struts2.action包中的OgnlAction:
package com.awei.struts2.action;
import com.awei.struts2.model.User;
import com.opensymphony.xwork2.ActionSupport;
public class OgnlAction extends ActionSupport{
private String password;
private User user;
private String username;
public OgnlAction(){
}
public String execute(){
System.out.println(username);
System.out.println(password);
System.out.println(user.getAge());
return SUCCESS;
}
public String getPassword() {
return password;
}
public User getUser() {
return user;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUser(User user) {
this.user = user;
}
public void setUsername(String username) {
this.username = username;
}
}
com.awei.struts2.model包中的User.java
package com.awei.struts2.model;
public class User {
private int age;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public String toString(){
return "user"+age;
}
}
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>My JSP 'index.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>
<<a href="ognl/ognl?username=a&password=123&user.age=18">enter</a><br>
</body>
</html>
ognl.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
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>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>
从值栈中取得的值:<s:property value="username"/><br/>
从值栈中取得对象的普通方法:<s:property value="user.age"/><br/>
<s:debug></s:debug>
</body>
</html>
由于index.jsp中为user.age赋值了,所以struts2就会new一个user对象,并调用user.setAge(18)将18赋值给user.age,ognl.jsp要显示user.age,就会调用user.getAge()方法将age读出。
Then:通过对象导航得到对象的值或方法
先构造两个对象类:Cat.java和Dog.java放在com.awei.struts2.model
Cat.java
package com.awei.struts2.model;
public class Cat {
private Dog friend;
public Dog getFriend() {
return friend;
}
public void setFriend(Dog friend) {
this.friend = friend;
}
public String miaomiao(){
return "miaomiao";
}
}
Dog.java
package com.awei.struts2.model;
public class Dog {
private String name;
public Dog(){
}
public Dog(String name){
super();
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
修改OgnlAction.java
package com.awei.struts2.action;
import com.awei.struts2.model.Cat;
import com.awei.struts2.model.User;
import com.opensymphony.xwork2.ActionSupport;
public class OgnlAction extends ActionSupport{
private Cat cat;
private String password;
private User user;
private String username;
public OgnlAction(){
}
public String execute(){
System.out.println(username);
System.out.println(password);
System.out.println(user.getAge());
return SUCCESS;
}
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 String f(){
return "ffffff";
}
}
修改ognl.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
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>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>
从值栈中取得的值:<s:property value="username"/><br/>
取得get/set方法:<s:property value="user.age"/><br/>
访问值栈中对象的普通方法:<s:property value="cat.friend.name"/><br/>
访问值栈中对象的普通方法(get/set方法):<s:property value="password.length()"/><br/>
访问值栈中对象的普通方法:<s:property value="cat.miaomiao()"/><br/>
访问值栈中action的方法:<s:property value="f()"/><br/>
<s:debug></s:debug>
</body>
</html>
Then:访问静态方法
修改ognl.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
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>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>
从值栈中取得的值:<s:property value="username"/><br/>
取得get/set方法:<s:property value="user.age"/><br/>
访问值栈中对象的普通方法:<s:property value="cat.friend.name"/><br/>
访问值栈中对象的普通方法(get/set方法):<s:property value="password.length()"/><br/>
访问值栈中对象的普通方法:<s:property value="cat.miaomiao()"/><br/>
访问值栈中action的方法:<s:property value="f()"/><br/>
<hr/>
访问静态方法:<s:property value="@com.awei.struts2.ognl.S@s()"/><br/>
//访问的方式是@方法所在类的全名@静态方法或@静态属性
访问静态属性:<s:property value="@com.awei.struts2.ognl.S@str"/><br/>
访问Math类的静态方法:<s:property value="@@max(2,3)"/><br/>
<s:debug></s:debug>
</body>
</html>
那么com.awei.struts.ognl中的S.java的定义如下:
package com.awei.struts2.ognl;
public class S {
public static String str="Static Attribute Str";
public static String s(){
return "static method";
}
}
Then:OGNL投影
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
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>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>
从值栈中取得的值:<s:property value="username"/><br/>
取得get/set方法:<s:property value="user.age"/><br/>
访问值栈中对象的普通方法:<s:property value="cat.friend.name"/><br/>
访问值栈中对象的普通方法(get/set方法):<s:property value="password.length()"/><br/>
访问值栈中对象的普通方法:<s:property value="cat.miaomiao()"/><br/>
访问值栈中action的方法:<s:property value="f()"/><br/>
<hr/>
访问静态方法:<s:property value="@com.awei.struts2.ognl.S@s()"/><br/>
访问静态属性:<s:property value="@com.awei.struts2.ognl.S@str"/><br/>
访问Math类的静态方法:<s:property value="@@max(2,3)"/><br/>
<hr/>
访问普通对象的构造方法:<s:property value="new com.awei.struts2.model.User(23)"/><br/>
<hr/>
访问List的对象:<s:property value="users"/><br/>
访问List中指定的对象:<s:property value="users[0]"/><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="dogSet"/><br/>
访问Map中的对象:<s:property value="catMap"/><br/>
访问Map中的某个元素:<s:property value="catMap.cat001"/><br/>
访问Map中所有的key:<s:property value="catMap.keys"/><br/>
访问Map中所有的value:<s:property value="catMap.values"/><br/>
访问Map的size:<s:property value="catMap.size()"/>|<s:property value="catMap.size"/><br/>
<hr/>
投影|过滤:<s:property value="users.{?#this.age==1}.{age}"/><br/>
投影2:<s:property value="users.{^#this.age>1}.{age}"/><br/>
投影3:<s:property value="users.{$#this.age>1}.{age}"/><br/>
投影4:<s:property value="users.{$#this.age>1}==null"/><br/>
<hr/>
[]<s:property value="[2]"/>
<s:debug></s:debug>
</body>
</html>
解释:
users.{?#this.age==1}.{age} ?是表示条件选择的意思,是选择users中user.age==1的user的age
users.{^#this.age>1}.{age} ^是表示开头,即选择users中user.age>1的users中的第一个的age
users.{$#this.age>1}.{age} $是表示开头,即选择users中user.age>1的users中的第一个的age
users.{$#this.age>1}==null 判断age>1的users的第一个是否为空
Finally:OGNL[]
[]<s:property value="[2]"/>
[]是取出valuestack中的object的名称。
如果一个应用中有好多个action,那么会按照action的调用顺序加到valuestack中。
举例:
ognl.xml修改为:
<?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" namespace="/ognl" extends="struts-default">
<action name="ognl" class="com.awei.struts2.action.OgnlAction">
<result >/ognl.jsp</result>
</action>
<action name="another" class="com.awei.struts2.action.AnotherAction">
<result type="chain">ognl</result>
</action>
</package>
<!-- Add packages here -->
</struts>
增加AnotherAction:
package com.awei.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
public class AnotherAction extends ActionSupport{
public String execute()throws Exception{
return super.execute();
}
}
结果显示:
[][com.awei.struts2.action.OgnlAction@1458657, com.awei.struts2.action.AnotherAction@176eeb9, com.opensymphony.xwork2.DefaultTextProvider@179c7cf]
Struts ValueStack Debug
Value Stack Contents
Object | Property Name | Property Value |
---|---|---|
com.awei.struts2.action.OgnlAction | texts | null |
users | [user1, user2, user3] | |
cat | null | |
errorMessages | [] | |
locale | zh_CN | |
actionMessages | [] | |
password | null | |
catMap | {cat001=catcat001, cat003=catcat003, cat002=catcat002, cat004=catcat004} | |
actionErrors | [] | |
username | null | |
errors | {} | |
fieldErrors | {} | |
dogSet | [dogdoudou, dogoudy, dogdiandian] | |
user | null | |
com.awei.struts2.action.AnotherAction | texts | null |
actionErrors | [] | |
errors | {} | |
fieldErrors | {} | |
errorMessages | [] | |
locale | zh_CN | |
actionMessages | [] | |
com.opensymphony.xwork2.DefaultTextProvider | texts | null |