Struts2 OGNL
OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,可以任意存取对象的属性或者调用对象的方法,能够遍历整个对象的结构图,实现对象属性类型的转换等功能。
实战
<%@ 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>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> OGNL:object graph navigation language<br/> <a href="/ognl.action?ip=100.22.33.22&uv=100&pv=50&user_model.school=1111222&bbs_model.tites_model.author=author">OGNL访问</a> 《备注:OGNL 只有传递参数的时候才构造对象传递参数值或者使用domain model的时候可以自己new》 </body> </html>
struts.xml
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="ognl" class="com.struts2.action.OGNLController"> <result>/ognl.jsp</result> </action> </package>
OGNLController.java
package com.struts2.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; import com.summary.model.BBSModel; import com.summary.model.UserModel; public class OGNLController extends ActionSupport { private String ip; private String uv; private String pv; private UserModel user_model; private BBSModel bbs_model; private List dataList = new ArrayList(); private Set dataSet =new HashSet(); private Map<String, String> dataMap = new HashMap(); private List<UserModel> dataInfoList ; //在页面中获取不到对象的值,需要查看对象是否设置了set,get方法 public String execute() { dataList.add("list_001"); dataList.add("list_002"); dataList.add("list_003"); dataList.add("list_004"); dataList.add("list_005"); dataList.add("list_006"); dataList.add("list_007"); dataList.add("list_008"); dataSet.add("set_001"); dataSet.add("set_002"); dataSet.add("set_003"); dataSet.add("set_004"); dataSet.add("set_005"); dataSet.add("set_006"); dataSet.add("set_007"); dataSet.add("set_008"); dataMap.put("data_key_001", "data_value_001"); dataMap.put("data_key_002", "data_value_002"); dataMap.put("data_key_003", "data_value_003"); dataMap.put("data_key_004", "data_value_004"); dataMap.put("data_key_005", "data_value_005"); dataMap.put("data_key_006", "data_value_006"); dataInfoList = new ArrayList<UserModel>(); UserModel userModel1 = new UserModel(); userModel1.setUser_name("user_name_01"); userModel1.setSchool("用户的学校"); userModel1.setRemark("备注user_name_01"); userModel1.setPassword("密码用户名称01"); userModel1.setAge(15); dataInfoList.add(userModel1); UserModel userModel2 = new UserModel(); userModel2.setUser_name("user_name_02"); userModel2.setSchool("用户的学校02"); userModel2.setRemark("备注user_name_02"); userModel2.setPassword("密码用户名称02"); userModel2.setAge(33); dataInfoList.add(userModel2); UserModel userModel3 = new UserModel(); userModel3.setUser_name("user_name_032"); userModel3.setSchool("用户的学校03"); userModel3.setRemark("备注user_name_03"); userModel3.setPassword("密码用户名称03"); userModel3.setAge(2); dataInfoList.add(userModel3); UserModel userModel4 = new UserModel(); userModel4.setUser_name("user_name_04"); userModel4.setSchool("用户的学校04"); userModel4.setRemark("备注user_name_04"); userModel4.setPassword("密码用户名称04"); userModel4.setAge(22); dataInfoList.add(userModel4); return SUCCESS; } public String test() { return "访问Action值栈中的普通方法"; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getUv() { return uv; } public void setUv(String uv) { this.uv = uv; } public String getPv() { return pv; } public void setPv(String pv) { this.pv = pv; } public UserModel getUser_model() { return user_model; } public void setUser_model(UserModel user_model) { this.user_model = user_model; } public BBSModel getBbs_model() { return bbs_model; } public void setBbs_model(BBSModel bbs_model) { this.bbs_model = bbs_model; } public List getDataList() { return dataList; } public void setDataList(List dataList) { this.dataList = dataList; } public Set getDataSet() { return dataSet; } public void setDataSet(Set dataSet) { this.dataSet = dataSet; } public Map<String, String> getDataMap() { return dataMap; } public void setDataMap(Map<String, String> dataMap) { this.dataMap = dataMap; } public List<UserModel> getDataInfoList() { return dataInfoList; } public void setDataInfoList(List<UserModel> dataInfoList) { this.dataInfoList = dataInfoList; } }
StaticController.java
package com.struts2.action; import com.opensymphony.xwork2.ActionSupport; public class StaticController { public static String CONFIG="配置文件"; public StaticController() { System.out.println("xxxxxxxxxxxxxx"); } public static String getStaticMethod() { return "访问静态方法"; } public static String getCONFIG() { return CONFIG; } public static void setCONFIG(String config) { CONFIG = config; } }
BBSModel.java
package com.summary.model; public class BBSModel { private String title; private TitesModel tites_model; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public TitesModel getTites_model() { return tites_model; } public void setTites_model(TitesModel tites_model) { this.tites_model = tites_model; } }
TitesModel.java
package com.summary.model; public class TitesModel { private String author="朱诞生"; private String content="帖子内容"; private String times="2011-12-04"; private String reply_to="暂时无人回复"; public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTimes() { return times; } public void setTimes(String times) { this.times = times; } public String getReply_to() { return reply_to; } public void setReply_to(String reply_to) { this.reply_to = reply_to; } }
UserModel.java
package com.summary.model; public class UserModel { private String user_name=""; private String password=""; private String school=""; private String remark=""; private int age; private String add_date; public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public String getData() { return "访问值栈中对象的普通方法"; } public String getAdd_date() { return add_date; } public void setAdd_date(String add_date) { this.add_date = add_date; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
OGNL: 对象图导航语言
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ 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> 对象图导航语言<br/> OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,可以任意存取对象的属性或者调用对象的方法,能够遍历整个对象的结构图,实现对象属性类型的转换等功能。 <br/> 值栈普通属性和普通方法:<br/> 1:访问ACTION值栈中的普通属性 : ip=<s:property value="ip"/><br/> 2:访问值栈中对象的普通属性 : UserModel.school=<s:property value="user_model.school"/><br/> 3:访问值栈中对象的普通属性 (set,get 方法): BBSModel.TitesModel.author=<s:property value="bbs_model.tites_model.author"/> 《备注:OGNL 只有传递参数的时候才构造对象传递参数值》<br/> 4:访问值栈中对象的普通方法 :length()= <s:property value="ip.length()"/> | substring()=<s:property value="ip.substring(0,5)"/> <br/> 5:访问值栈中对象的普通方法 : <s:property value="test()"/> | <s:property value="user_model.getData()"/> <br/> <hr/> 值栈中静态属性和方法:使用@包名@类名 <br/> 1:访问静态属性:<s:property value="@com.struts2.action.StaticController@CONFIG"/><br/> 2:访问静态方法:<s:property value="@com.struts2.action.StaticController@getStaticMethod()"/><br/> 3:访问构造方法:<s:property value="new com.struts2.action.StaticController()"/><br/> <hr/> 访问数组和集合 <br/> 1:获取List数据:<s:property value="dataList"/> <br/> 2:获取List中的一个元素(数组和集合是一样的访问小标都是从0切使用中括号[]):<s:property value="dataList[4]"/><br/> 3:获取Set数据:<s:property value="dataSet"/><br/> 4:获取Set中的莫一个元素(Set是无序,且不能使用下标获取):<s:property value="dataSet[3]"/><br/> 5:获取Map数据: <s:property value="dataMap"/><br/> 6:获取Map的所有键:<s:property value="dataMap.keys"/><br/> 7:获取Map的所有值:<s:property value="dataMap.values"/><br/> 8:获取Map中的莫一个元素(使用类似于数组小标获取List的内容):<s:property value="dataMap['data_key_005']"/><br/> 9:获取List的大小<s:property value="dataList.size()"/><br/> <hr/> 访问集合 – 投影、选择(? ^ $) <br/> 1:选择获取年龄18岁以上的集合: <s:property value="dataInfoList.{?#this.age>=18}"/><br/> 2:选择获取年龄18岁以上的对象集合的姓名: <s:property value="dataInfoList.{?#this.age>=18}.{user_name}"/><br/> 3:选择获取年龄18岁以上的对象集合第一个姓名:dataInfoList.{?#this.age>=18}.{user_name}[0]= <s:property value="dataInfoList.{?#this.age>=18}.{user_name}[0]"/><br/> 4:选择获取年龄18岁以上的对象集合第一个姓名: dataInfoList.{^#this.age>=18}.{user_name}=<s:property value="dataInfoList.{^#this.age>=18}.{user_name}"/><br/> 5:选择获取以上的对象集合最后一个姓名: dataInfoList.{$#this.age>=1}.{user_name}=<s:property value="dataInfoList.{$#this.age>=1}.{user_name}"/><br/> 6:选择获取年龄18岁以上的对象集合的姓名的人数: <s:property value="dataInfoList.{?#this.age>=18}.{user_name}.size()"/><br/> <s:debug></s:debug> </body> </html>