Struts2(九)OGNL标签一与Struts2标签
一、什么是OGNL
Object Graph Navigation Language对象图导航语言。
是Struts2默认的表达式语言,开源,功能更强大。和EL表达式有点相似
存取对象的属性,调用对象的方法
访问静态方法,静态属性
访问值栈及Stack Context
支持赋值、运算操作、字段类型转化等。
二、简单例子
导入所需要的struts2的包
web.xml增加struts2过滤器
<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>
实体类
package com.pb.entity; /* * 街道实体类 */ public class Street { private int streetId; //街道ID private String streetName; //街道名称 public Street() { super(); // TODO Auto-generated constructor stub } public Street(int streetId, String streetName) { super(); this.streetId = streetId; this.streetName = streetName; } public int getStreetId() { return streetId; } public String getStreetName() { return streetName; } public void setStreetId(int streetId) { this.streetId = streetId; } public void setStreetName(String streetName) { this.streetName = streetName; } }
package com.pb.entity; /* * 房子实体类 */ public class Hourse { private int hourseId; //房屋ID private String hourseName; //房屋名称 private Street street; //房屋所在街道 public Hourse() { super(); // TODO Auto-generated constructor stub } public Hourse(int hourseId, String hourseName, Street street) { super(); this.hourseId = hourseId; this.hourseName = hourseName; this.street = street; } public int getHourseId() { return hourseId; } public String getHourseName() { return hourseName; } public Street getStreet() { return street; } public void setHourseId(int hourseId) { this.hourseId = hourseId; } public void setHourseName(String hourseName) { this.hourseName = hourseName; } public void setStreet(Street street) { this.street = street; } }
HourseAction
package com.pb.web.action; import com.opensymphony.xwork2.ActionSupport; import com.pb.entity.Hourse; import com.pb.entity.Street; public class HourseAction extends ActionSupport { private Hourse hourse; @Override public String execute() throws Exception { //声明街道对象 Street street =new Street(001,"南新路"); hourse=new Hourse(11,"一房一厅",street); return SUCCESS; } public Hourse getHourse() { return hourse; } public void setHourse(Hourse hourse) { this.hourse = hourse; } }
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.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="hourse" class="com.pb.web.action.HourseAction"> <result name="success"> /ognl1.jsp </result> </action> </package> </struts>
ognl1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>OGNL一</title> </head> <body> 房屋的名称:<s:property value="hourse.hourseName"/> 房屋的街道名称:<s:property value="hourse.street.streetName"/> <s:debug/> </body> </html>
在地址栏中输入
http://localhost:8080/OGNLDemo1/hourse.action
发生页面跳转,并取出其中的值
三、访问值栈中的值
四、访问List 、Map、Set
struts.xml
<action name="ognl" class="com.pb.web.action.OgnlAction"> <result name="success"> /ognllist.jsp </result> </action>
ognllistAction
package com.pb.web.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.pb.entity.Street; public class OgnlAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private List<Street> streetList; private Map<String, Street> streetMap; private Set <Street> streetSet; public List<Street> getStreetList() { return streetList; } public void setStreetList(List<Street> streetList) { this.streetList = streetList; } public Map<String, Street> getStreetMap() { return streetMap; } public void setStreetMap(Map<String, Street> streetMap) { this.streetMap = streetMap; } public Set<Street> getStreetSet() { return streetSet; } public void setStreetSet(Set<Street> streetSet) { this.streetSet = streetSet; } @Override public String execute() throws Exception { streetList=new ArrayList<Street>(); streetList.add(new Street(1, "高新南一路")); streetList.add(new Street(2, "高新南二路")); streetList.add(new Street(3, "高新南三路")); streetMap=new HashMap<String,Street>(); streetMap.put("k1", new Street(4, "高新南四路")); streetMap.put("k2", new Street(5, "高新南五路")); streetMap.put("k3", new Street(6, "高新南六路")); streetSet =new HashSet<Street>(); streetSet.add(new Street(7, "高新南七路")); streetSet.add(new Street(8, "高新南八路")); streetSet.add(new Street(9, "高新南九路")); return SUCCESS; } }
jsp页页
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>OGNL一</title> </head> <body> <h1>List</h1> 访问List<s:property value="streetList" /><br/> 访问List中第一个元素:<s:property value="streetList[0].streetName" /><br/> List中的大小:<s:property value="streetList.size()" /><br/> List是否为空:<s:property value="streetList.isEmpty()" /><br/> 自定义集合List:<s:property value="{1,2,3,4,5}" /><br/> 自定义集合List访问第一个元素:<s:property value="{1,2,3,4,5}[0]" /><br/> <hr/> <h1>Map</h1> 访问Map<s:property value="streetMap" /><br/> 访问Map中第一个元素:<s:property value="streetMap['k1'].streetName" />|<s:property value="streetMap.k1.streetName"/><br/> 访问Map 中所有key:<s:property value="streetMap.keys" /><br/> 访问Map 中所有values:<s:property value="streetMap.values" /><br/> Map中的大小:<s:property value="streetMap.size()" /><br/> Map是否为空:<s:property value="streetMap.isEmpty()" /><br/> 自定义集合Map:<s:property value="#{'k1':'v1','k2':'v2','k3':'v3'}" /><br/> 自定义集合Mapt访问第一个元素:<s:property value="#{'k1':'v1','k2':'v2','k3':'v3'}['k1']" />|<s:property value="#{'k1':'v1','k2':'v2','k3':'v3'}.k1" />
<br/> <hr/> <h1>Set</h1> 访问Set<s:property value="streetSet" /><br/> 访问Set中第一个元素:<s:property value="streetSet.toArray()[0].streetName" />|<s:property value="streetSet.toArray()[0].streetName"/><br/> Set中的大小:<s:property value="streetSet.size()" /><br/> Set是否为空:<s:property value="streetSet.isEmpty()" /><br/> <hr/> <s:debug/> </body> </html>
在页面中输入:http://localhost:8080/OGNLDemo1/ognl.action访问
五、访问静态方法和投影
5.1、访问静态方法和静态属性
package com.pb.demo; public class Demo { public final static String STR="STATIC STR"; public static String staticMethod(){ String info="Demo Static Method"; return info; } }
在ognl2.jsp 中可以通过如下代码访问该类的静态属性以及静态方法:
<%@taglib uri="/struts-tags" prefix="s"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>OGNL 对象图导航语言</h1> 调用静态方法:<s:property value="@com.pb.demo.Demo@staticMethod()"/> <br> 调用静态常量:<s:property value="@com.pb.demo.Demo@STR"/> <s:debug/> </body> </html>
struts.xml
//开启静态方法 <constant name="struts.ognl.allowStaticMethodAccess" value="true" />
其中<constant>常量你可以去default.properties 文件中查找并复制使用。
5.2、访问投影
选择就是过滤满足选择条件的集合元素。选择操作的语法是:collection.{X YYY},其中X
是一个选择操作符,后面则是选择用的逻辑表达式,而选择操作符有三种:
?选择满足条件的所有元素
^选择满足条件的第一个元素
$选择满足条件的最后一个元素
package com.pb.entity; /* * 用户名 */ public class User { private Integer age; private String name; private String pasword; public User() { super(); // TODO Auto-generated constructor stub } public User(String name, String pasword, Integer age) { super(); this.name = name; this.pasword = pasword; this.age = age; } public Integer getAge() { return age; } public String getName() { return name; } public String getPasword() { return pasword; } public void setAge(Integer age) { this.age = age; } public void setName(String name) { this.name = name; } public void setPasword(String pasword) { this.pasword = pasword; } }
package com.pb.web.action; import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; import com.pb.entity.User; public class UserAction extends ActionSupport { private User user; private List<User> users=new ArrayList<User>(); public User getUser() { return user; } public void setUser(User user) { this.user = user; } public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } @Override public String execute() throws Exception { User u1 = new User(); u1.setName("zhangsan"); u1.setPasword("zhangsan"); u1.setAge(10); this.users.add(u1); User u2 = new User(); u2.setName("lisi"); u2.setPasword("lisi"); u2.setAge(23); this.users.add(u2); User u3 = new User(); u3.setName("wangwu"); u3.setPasword("wangwu"); u3.setAge(22); this.users.add(u3); return SUCCESS; } }
struts.xml
<package name="default" namespace="/" extends="struts-default"> <action name="login" class="com.pb.web.action.UserAction"> <result name="success"> /ognl.jsp </result> </action> </package>
jsp页面
<%@taglib prefix="s" uri="/struts-tags" %> <!-- 年龄大于18 的所有用户的姓名 --> 集合元素过滤:<s:property value="users.{?#this.age>18}.{name}"/><br/> <!-- 年龄大于18 的所有用户的第一个用户的姓名 --> 集合元素过滤:<s:property value="users.{^#this.age>18}.{name}"/><br/> <!-- 年龄大于18 的所有用户的最后一个用户的姓名 --> 集合元素过滤:<s:property value="users.{$#this.age>18}.{name}"/><br/>