OGNL表达式的使用方法
定义:
OGNL是对象图导航语言(即该表达式访问的是对象及对象的属性)
知识点:
1.基本属性的访问
1.1 访问值栈中action的普通属性
- <s:propertyvalue="username"/>
1.2 访问值栈中对象的普通属性
- <s:propertyvalue="student.age"/>
1.3 访问值栈中对象(对象包含对象)的普通属性
- <s:propertyvalue="cat.friend.name"/>
1.4 访问值栈中对象的普通方法
- <s:propertyvalue="cat.friend.say().length()"/>
1.5 访问值栈中action的普通方法
- <s:propertyvalue="execute()"/>
1.6 访问静态方法
- <s:propertyvalue="@com.wj.struts2.util.OGNLUtil@getString()"/>
1.7 访问静态属性
- <s:propertyvalue="@com.wj.struts2.util.OGNLUtil@URL"/>
1.8 访问Math类的属性
- <s:propertyvalue="@@max(5,3)"/>
1.9 访问普通类的构造方法
- <s:propertyvalue="newcom.wj.struts2.action.model.Dog()"/>
2.访问容器
2.1 访问List
2.1.1 访问list
- <s:propertyvalue="list"/>
2.1.2 访问list中的某个元素
- <s:propertyvalue="list[2]"/>
2.1.3 访问list中某个元素属性的集合
- <s:propertyvalue="list.{no}"/>
2.1.4 访问list中某个元素的属性
- <s:propertyvalue="list[2].no"/>
2.2 访问Set
2.2.1 访问set
- <s:propertyvalue="set"/>
2.2.2 访问set中的某个元素(不可访问,set无顺序)
2.3 访问Map
2.3.1 访问Map
- <s:propertyvalue="map"/>
2.3.2 访问Map中的某个元素
- <s:propertyvalue="map['key']"/>
2.3.3 访问Map中的所有key
- <s:propertyvalue="map.keys"/>
2.3.4 访问Map中的所有value
- <s:propertyvalue="map.values"/>
2.3.5 访问容器的大小
- <s:propertyvalue="map.size()"/>
3.投影
3.1 通过投影访问list中年龄为25的学生的姓名
- <s:propertyvalue="list.{?#this.age==25}[0].{no}[0]"/>
3.2 通过投影访问list中年龄大于25的集合中的首元素的姓名
- <s:propertyvalue="list.{^#this.age>20}.{no}"/>
3.3 通过投影访问list中年龄大于25的集合中的尾元素的姓名
- <s:propertyvalue="list.{$#this.age>20}.{no}"/>
3.4 通过投影判断list中年龄大于25的集合是否为空
- <s:propertyvalue="list.{?#this.age>25}==null"/>
4.栈
用[]来访问栈中的元素,注意:[0]表示从栈中的开始位置进行遍历其中的元素
- <s:propertyvalue="[1][0].execute()"/>
示例代码
OGNLAction.java
- /**
- *OGNL表达式
- *@author健
- */
- publicclassOGNLActionextendsActionSupport{
- /**
- *序列化
- */
- privatestaticfinallongserialVersionUID=1L;
- /**
- *猫
- */
- privateCatcat;
- /**
- *List
- */
- privateList<Student>list;
- /**
- *Map
- */
- privateMap<String,Dog>map;
- /**
- *密码
- */
- privateStringpassword;
- /**
- *Set
- */
- privateSet<Dog>set;
- /**
- *学生
- */
- privateStudentstudent;
- /**
- *用户名
- */
- privateStringusername;
- publicOGNLAction(){
- this.list=newArrayList<Student>();
- Studentstudent1=newStudent("zhangsan",20);
- Studentstudent2=newStudent("lisi",25);
- Studentstudent3=newStudent("wangwu",28);
- Studentstudent4=newStudent("zhaoliu",24);
- Studentstudent5=newStudent("xiaotaoqi",18);
- list.add(student1);
- list.add(student2);
- list.add(student3);
- list.add(student4);
- list.add(student5);
- this.set=newHashSet<Dog>();
- Dogdog1=newDog("bandeng");
- Dogdog2=newDog("zhuozi");
- Dogdog3=newDog("yizi");
- set.add(dog1);
- set.add(dog2);
- set.add(dog3);
- this.map=newHashMap<String,Dog>();
- map.put("0001",dog1);
- map.put("0002",dog2);
- map.put("0003",dog3);
- }
- /**
- *控制器
- *@return跳转的页面
- */
- @Override
- publicStringexecute(){
- return"success";
- }
- /**
- *取得cat
- *@returncat
- */
- publicCatgetCat(){
- returncat;
- }
- /**
- *取得list
- *@returnlist
- */
- publicList<Student>getList(){
- returnlist;
- }
- /**
- *取得map
- *@returnmap
- */
- publicMap<String,Dog>getMap(){
- returnmap;
- }
- /**
- *取得password
- *@returnpassword
- */
- publicStringgetPassword(){
- returnpassword;
- }
- /**
- *取得set
- *@returnset
- */
- publicSet<Dog>getSet(){
- returnset;
- }
- /**
- *取得student
- *@returnstudent
- */
- publicStudentgetStudent(){
- returnstudent;
- }
- /**
- *取得username
- *@returnusername
- */
- publicStringgetUsername(){
- returnusername;
- }
- /**
- *设置cat
- *@paramcatcat
- */
- publicvoidsetCat(Catcat){
- this.cat=cat;
- }
- /**
- *设置list
- *@paramlistlist
- */
- publicvoidsetList(List<Student>list){
- this.list=list;
- }
- /**
- *设置map
- *@parammapmap
- */
- publicvoidsetMap(Map<String,Dog>map){
- this.map=map;
- }
- /**
- *设置password
- *@parampasswordpassword
- */
- publicvoidsetPassword(Stringpassword){
- this.password=password;
- }
- /**
- *设置set
- *@paramsetset
- */
- publicvoidsetSet(Set<Dog>set){
- this.set=set;
- }
- /**
- *设置student
- *@paramstudentstudent
- */
- publicvoidsetStudent(Studentstudent){
- this.student=student;
- }
- /**
- *设置username
- *@paramusernameusername
- */
- publicvoidsetUsername(Stringusername){
- this.username=username;
- }
- }
ChainAction.java
- /**
- *OGNL表达式
- *@author健
- */
- publicclassChainActionextendsActionSupport{
- /**
- *序列化
- */
- privatestaticfinallongserialVersionUID=1L;
- /**
- *控制器
- *@return跳转的页面
- */
- @Override
- publicStringexecute(){
- return"success";
- }
- }
OGNLUtil.java
- /**
- *工具类
- *@author健
- */
- publicclassOGNLUtil{
- /**
- *静态常量
- */
- publicstaticfinalStringURL="staticproperty";
- /**
- *静态方法
- *@return字符串
- */
- publicstaticStringgetString(){
- return"staticmethod";
- }
- }
Cat.java
- /**
- *猫
- *@author健
- */
- publicclassCat{
- /**
- *名字
- */
- privateStringname;
- /**
- *朋友
- */
- privateDogfriend;
- /**
- *取得name
- *@returnname
- */
- publicStringgetName(){
- returnname;
- }
- /**
- *设置name
- *@paramnamename
- */
- publicvoidsetName(Stringname){
- this.name=name;
- }
- /**
- *取得friend
- *@returnfriend
- */
- publicDoggetFriend(){
- returnfriend;
- }
- /**
- *设置friend
- *@paramfriendfriend
- */
- publicvoidsetFriend(Dogfriend){
- this.friend=friend;
- }
- /**
- *类方法
- *@return返回字符串
- */
- publicStringsay(){
- return"miaomiao";
- }
- /**
- *构造
- */
- publicCat(){
- System.out.println("catconstructor");
- }
- /**
- *重写toString方法
- */
- publicStringtoString(){
- return"catnameis"+this.name;
- }
- }
Dog.java
- /**
- *狗
- *@author健
- */
- publicclassDog{
- /**
- *名字
- */
- privateStringname;
- /**
- *构造
- */
- publicDog(){
- System.out.println("dogconstructor");
- }
- /**
- *构造
- */
- publicDog(Stringname){
- this.name=name;
- }
- /**
- *取得name
- *@returnname
- */
- publicStringgetName(){
- returnname;
- }
- /**
- *设置name
- *@paramnamename
- */
- publicvoidsetName(Stringname){
- this.name=name;
- }
- /**
- *类方法
- *@return返回字符串
- */
- publicStringsay(){
- return"miaomiao";
- }
- /**
- *重写toString方法
- */
- publicStringtoString(){
- return"dognameis"+this.name;
- }
- }
Student.java
- /**
- *学生类
- *@author健
- */
- publicclassStudent{
- /**
- *学号
- */
- privateStringno;
- /**
- *年龄
- */
- privateintage;
- /**
- *构造方法
- */
- publicStudent(){
- System.out.println("进入学生类的构造方法");
- }
- /**
- *构造方法
- */
- publicStudent(Stringno,intage){
- this.age=age;
- this.no=no;
- }
- /**
- *取得no
- *@returnno
- */
- publicStringgetNo(){
- returnno;
- }
- /**
- *设置no
- *@paramnono
- */
- publicvoidsetNo(Stringno){
- this.no=no;
- }
- /**
- *取得age
- *@returnage
- */
- publicintgetAge(){
- returnage;
- }
- /**
- *设置age
- *@paramageage
- */
- publicvoidsetAge(intage){
- this.age=age;
- }
- /**
- *重写toString方法
- */
- publicStringtoString(){
- return"Student:"+this.no+","+this.age;
- }
- }
struts.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEstrutsPUBLIC
- "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <!--配置开发模式:修改不用重启服务器-->
- <constantname="struts.devMode"value="true"/>
- <!--配置允许静态方法的访问-->
- <constantname="struts.ognl.allowStaticMethodAccess"value="true"/>
- <!--包含的文件-->
- <includefile="ognl.xml"></include>
- </struts>
ognl.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEstrutsPUBLIC
- "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <packagename="ognl"namespace="/ognl"extends="struts-default">
- <actionname="ognl"class="com.wj.struts2.action.OGNLAction">
- <resultname="success">/result.jsp</result>
- </action>
- <actionname="chain"class="com.wj.struts2.action.ChainAction">
- <resulttype="chain">
- <paramname="actionName">ognl</param>
- <paramname="namespace">/ognl</param>
- </result>
- </action>
- </package>
- </struts>
index.jsp
- <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
- <%@tagliburi="/struts-tags"prefix="s"%>
- <%
- Stringpath=request.getContextPath();
- StringbasePath=request.getScheme()+"://"
- +request.getServerName()+":"+request.getServerPort()
- +path+"/";
- %>
- <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
- <html>
- <head>
- <basehref="<%=basePath%>">
- <title>Struts2_OGNL</title>
- <metahttp-equiv="pragma"content="no-cache">
- <metahttp-equiv="cache-control"content="no-cache">
- <metahttp-equiv="expires"content="0">
- <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
- <metahttp-equiv="description"content="Thisismypage">
- <!--
- <linkrel="stylesheet"type="text/css"href="styles.css"mce_href="styles.css">
- -->
- </head>
- <body>
- <div>
- 访问基本元素
- <li><ahref="ognl/ognl?username=wangjian&password=123456"mce_href="ognl/ognl?username=wangjian&password=123456">1.访问值栈中action的普通属性</a></li>
- <li><ahref="ognl/ognl?student.age=20&student.no=0001"mce_href="ognl/ognl?student.age=20&student.no=0001">2.访问值栈中对象的普通属性</a></li>
- <li><ahref="ognl/ognl?cat.friend.name=oudy"mce_href="ognl/ognl?cat.friend.name=oudy">3.访问值栈中对象(对象包含对象)的普通属性</a></li>
- <li><ahref="ognl/ognl?cat.friend.name=oudy"mce_href="ognl/ognl?cat.friend.name=oudy">4.访问值栈中对象的普通方法</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">5.访问值栈中action的普通方法</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">6.访问静态方法</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">7.访问静态属性</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">8.访问Math类的属性</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">9.访问普通类的构造方法</a></li>
- </div>
- <br>
- <br>
- <div>
- 访问容器<br>
- (一)访问List<br>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">1.访问list</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">2.访问list中的某个元素</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">3.访问list中某个元素属性的集合</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">4.访问list中某个元素的属性</a></li><br>
- (二)访问Set<br>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">1.访问set</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">2.访问set中的某个元素(不可访问,set无顺序)</a></li><br>
- (三)访问Map<br>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">1.访问Map</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">2.访问Map中的某个元素</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">3.访问Map中的所有key</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">4.访问Map中的所有value</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">5.访问容器的大小</a></li>
- <br>
- </div>
- <br>
- <br>
- <div>
- 投影<br>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">1.通过投影访问list中年龄为25的学生的姓名</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">2.通过投影访问list中年龄大于25的集合中的首元素的姓名</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">3.通过投影访问list中年龄大于25的集合中的尾元素的姓名</a></li>
- <li><ahref="ognl/ognl"mce_href="ognl/ognl">4.通过投影判断list中年龄大于25的集合是否为空</a></li><br>
- </div>
- <br>
- <br>
- <div>
- 栈<br>
- <li><ahref="ognl/chain"mce_href="ognl/chain">用[]来访问栈中的元素,注意:[1]表示从栈中的开始位置进行遍历其中的元素</a></li>
- </div>
- </body>
- </html>
result.jsp
- <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
- <%@tagliburi="/struts-tags"prefix="s"%>
- <%
- Stringpath=request.getContextPath();
- StringbasePath=request.getScheme()+"://"
- +request.getServerName()+":"+request.getServerPort()
- +path+"/";
- %>
- <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
- <html>
- <head>
- <basehref="<%=basePath%>">
- <title>Struts2_OGNL</title>
- <metahttp-equiv="pragma"content="no-cache">
- <metahttp-equiv="cache-control"content="no-cache">
- <metahttp-equiv="expires"content="0">
- <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
- <metahttp-equiv="description"content="Thisismypage">
- <!--
- <linkrel="stylesheet"type="text/css"href="styles.css"mce_href="styles.css">
- -->
- </head>
- <body>
- 访问值栈中action的普通属性:<br>
- <li>username:<s:propertyvalue="username"/></li>
- <li>password:<s:propertyvalue="password"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问值栈中对象的普通属性<br>
- <li>student:<s:propertyvalue="student"/></li>
- <li>no:<s:propertyvalue="student.no"/></li>
- <li>age:<s:propertyvalue="student.age"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问值栈中对象(对象包含对象)的普通属性<br>
- <li>friendName:<s:propertyvalue="cat.friend.name"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问值栈中对象的普通方法<br>
- <li>method01:<s:propertyvalue="cat.friend.say().length()"/></li>
- <li>method02:<s:propertyvalue="cat.say()"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问值栈中action的普通方法<br>
- <li>method:<s:propertyvalue="execute()"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问静态方法<br>
- <li>staticmethod:<s:propertyvalue="@com.wj.struts2.util.OGNLUtil@getString()"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问静态属性<br>
- <li>staticproperty:<s:propertyvalue="@com.wj.struts2.util.OGNLUtil@URL"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问静态属性<br>
- <li>Mathmethod:<s:propertyvalue="@@max(5,3)"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问普通类的构造方法<br>
- <li>Constructermethod:<s:propertyvalue="newcom.wj.struts2.action.model.Dog()"/></li>
- <br>
- ----------------------------------------
- <br>
- <br>
- <br>
- 访问容器<br>
- (一)访问List<br>
- <li>1.访问list----<s:propertyvalue="list"/></li>
- <li>2.访问list中的某个元素----<s:propertyvalue="list[2]"/></li>
- <li>3.访问list中某个元素属性的集合----<s:propertyvalue="list.{no}"/></li>
- <li>4.访问list中某个元素的属性----<s:propertyvalue="list[2].no"/></li><br>
- (二)访问Set<br>
- <li>1.访问set----<s:propertyvalue="set"/></li>
- <li>2.访问set中的某个元素(不可访问,set无顺序)----<s:propertyvalue="set[1]"/></li><br>
- (三)访问Map<br>
- <li>1.访问Map----<s:propertyvalue="map"/></li>
- <li>2.访问Map中的某个元素----<s:propertyvalue="map['0001']"/></li>
- <li>3.访问Map中的所有key----<s:propertyvalue="map.keys"/></li>
- <li>4.访问Map中的所有value----<s:propertyvalue="map.values"/></li>
- <li>5.访问容器的大小----<s:propertyvalue="map.size()"/></li>
- <br>
- ----------------------------------------
- <br>
- <br>
- <br>
- <div>
- 投影<br>
- <li>1.通过投影访问list中年龄为25的学生的姓名----<s:propertyvalue="list.{?#this.age==25}[0].{no}[0]"/></li>
- <li>2.通过投影访问list中年龄大于25的集合中的首元素的姓名----<s:propertyvalue="list.{^#this.age>20}.{no}"/></li>
- <li>3.通过投影访问list中年龄大于25的集合中的尾元素的姓名----<s:propertyvalue="list.{$#this.age>20}.{no}"/></li>
- <li>4.通过投影判断list中年龄大于25的集合是否为空----<s:propertyvalue="list.{?#this.age>25}==null"/></li><br>
- </div>
- ----------------------------------------
- <br>
- <br>
- <br>
- <div>
- 栈<br>
- <li>用[]来访问栈中的元素,注意:[0]中0表示从栈中的开始位置进行遍历其中的元素----<s:propertyvalue="[1][0].execute()"/></li>
- </div>
- <s:debug></s:debug>
- </body>
- </html>