OGNL表达式
1、OGNL ----对象图导航语言 Object Graph Navigation Language
主要用于struts框架中(目前在别的地方还没接触到)
就是<s:property value="" /> value里面的内容,就叫ONGL表达式;
2、用途
使用ONGL语言,在前端页面中,访问action的属性、方法;姑且认为就是对JSP的封装,是编程更加方便。
3、使用方法
<%@ 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>ONGL-的应用</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> ONGL的应用. <br> <p>获取普通成员变量(int)</p> <s:property value="id" /> <p>获取普通成员变量(String)</p> <s:property value="name" /> <p>获取对象成员变量(user)</p> men:<s:property value="men" /> men.id:<s:property value="men.id" /> men.name:<s:property value="men.name" /> <p>调用对象的方法(user)</p> <s:property value="men.print()"/> <p>获取静态成员变量(int)</p> static var:<s:property value="@com.actio.OnglAction@count"/> <li>访问Math类的静态方法:<s:property value="@@max(2,3)" /></li> <p>获取List</p> List.size:<s:property value="users.size()" /> List:<s:property value="users" /> List1: <s:property value="users[0]"/> list2.name <s:property value="users[1].name"/> <p>获取Set</p> Set.size:<s:property value="childs.size()" /> Set:<s:property value="childs" /> <p>获取Map</p> Map.size:<s:property value="container.size()" /> Map:<s:property value="container" /> <p>调用普通方法:</p> general method:<s:property value="mySay()"/> <p>调用静态方法</p> static method:<s:property value="@com.actio.OnglAction@sayHello()"/> </body> </html>
package com.actio; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import com.opensymphony.xwork2.ActionSupport; import com.sun.org.apache.bcel.internal.generic.NEW; import com.vo.user; public class OnglAction extends ActionSupport{ public static int count = 90; public static int getCount() { return count; } public static String sayHello(){ System.out.println("调用静态方法"); return "static Method-----"; } public static void setCount(int count) { OnglAction.count = count; } public Set<String> childs = new HashSet<String>(); public Map<String, Object> container; public int id; public user men; public String name; public List<user> users = new ArrayList<user>(); public OnglAction(){ } @Override public String execute() throws Exception {//默认执行此方法 // TODO Auto-generated method stub this.name="PPT"; this.men=new user(1001,"tony"); users.add(new user(20111907,"潘腾")); users.add(new user(20111908, "沈")); childs.add("panteng"); childs.add("zhang"); return super.execute(); } public Set<String> getChilds() { return childs; } public Map<String, Object> getContainer() { return container; } public int getId() { return id; } public String getName() { return name; } public List<user> getUsers() { return users; } public String mySay(){ System.out.println("非静态方法:" + name); return "mySay()"; } public void setChilds(Set<String> childs) { this.childs = childs; } public void setContainer(Map<String, Object> container) { this.container = container; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setUsers(List<user> users) { this.users = users; } }
package com.vo; import com.opensymphony.xwork2.ActionSupport; public class user extends ActionSupport{ public user(){ } public user(int i,String nam){ this.id=i; this.name=nam; } @Override public String toString() { // TODO Auto-generated method stub return "id:" + id + " name:" + name; } public int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String name; public String print(){ return "调用对象的方法~~~~~~成功"; } }
<?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"> <default-action-ref name="index" /> <global-results> <result name="error">/WEB-INF/jsp/error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error"/> </global-exception-mappings> <action name="index"> <result type="redirectAction"> <param name="actionName">HelloWorld</param> <param name="namespace">/example</param> </result> </action> </package> <include file="example.xml"/> --> <!-- Add packages here --> <constant name="struts.devMode" value="false" /> <constant name="struts.i18n.encoding" value = "UTF-8" /> <constant name="struts.ognl.allowStaticMethodAccess" value = "true" ></constant> <package name="default" namespace="/" extends="struts-default"> <action name="hello"> <result> /hello.jsp </result> </action> </package> <package name="np" namespace="/home" extends="struts-default"> <action name="hello"> <result> /index.jsp </result> </action> <action name = "enter_*" class = "ActionClass" method="{1}"> <result name="error"> /error.jsp </result> <result name="ok"> /index.jsp </result> <result name="addSuccess"> /{1}Success.jsp </result> <result name="fileldRrror"> /filedError.jsp </result> </action> </package> <package name="de" namespace="/" extends="struts-default" > <action name = "onglAction" class = "com.actio.OnglAction" > <result>/UseOngl.jsp</result> </action> </package> </struts>
切记,在struts.xml中添加这句话:<constant name="struts.ognl.allowStaticMethodAccess" value = "true" ></constant> 否则访问不到静态的属性和方法
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name></display-name> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
4、 <s:property value="[0].username"/></li>
[0] 代表从value context中第一个对象开始向下寻找参数~~并不代表第一个对象。