OGNL基礎語法以及與Struts框架整合 的體現html
一:OGNL基礎語法java
1.建立實體類web
package cn.itcast.domain; /** * @author 做者 * @version 建立時間:2018年7月24日 上午9:51:20 * 類說明: */ public class User { private String name; private Integer age; public User() { } public User(String name,Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
2.OGNL基礎語法 框架
package cn.itcast.ognltest; import java.util.HashMap; import java.util.Map; import org.junit.Test; import cn.itcast.domain.User; import ognl.Ognl; import ognl.OgnlContext; import ognl.OgnlException; /** * @author 做者 * @version 建立時間:2018年7月24日 上午9:52:17 類說明:OGNL基礎語法 基礎步驟: 1.建立root * 放置任何對象做爲Root均可以 2.建立Context 核心是裏面放置Map鍵值對 3.將root的值和Context的值放入OGNL中 */ public class OgnlBase { private User userRoot = new User(); private Map<String, User> context = new HashMap<String, User>(); private OgnlContext oc = new OgnlContext(); // 基本步驟 public void test01() { // 1.建立root 放置任何對象做爲Root均可以 // User userRoot = new User(); userRoot.setName("張三"); userRoot.setAge(18); // 2.建立Context 核心是裏面放置Map鍵值對 // Map是接口不能實例化,只能使用其實現類實例化 // Map<String,User> context = new HashMap<String,User>(); context.put("user1", new User("李四", 19)); context.put("user2", new User("王五", 20)); // 3.將root的值和Context的值放入OGNL中 // OgnlContext ognl = new OgnlContext(); oc.setRoot(userRoot); oc.setValues(context); } // 基本取值 @Test public void test02() throws OgnlException { test01(); // 從root中取值 String name = (String) Ognl.getValue("name", oc, oc.getRoot()); // 從context中取值 String name1 = (String) Ognl.getValue("#user1.name", oc, oc.getRoot()); // 打印 System.out.println(name); System.out.println(name1); } // 基本賦值 @Test public void test03() throws OgnlException { test01(); // 給root中賦值 Ognl.getValue("name='八戒'", oc, oc.getRoot()); String name = (String)Ognl.getValue("name", oc, oc.getRoot()); // 給context中賦值 Ognl.getValue("#user1.name = '悟空'", oc, oc.getRoot()); String name1 = (String) Ognl.getValue("#user1.name", oc, oc.getRoot()); // 打印 System.out.println(name); System.out.println(name1); } // 調用方法 @Test public void test04() throws OgnlException { test01(); Ognl.getValue("setName('唐僧')", oc, oc.getRoot()); String name = (String)Ognl.getValue("getName()", oc, oc.getRoot()); // 調用context方法 Ognl.getValue("#user1.setName('沙僧')", oc, oc.getRoot()); String name1 = (String) Ognl.getValue("#user1.getName()", oc, oc.getRoot()); // 打印 System.out.println(name); System.out.println(name1); } // 調用靜態方法 @Test public void test05() throws OgnlException { test01(); //例如調用系統自帶的方法 @Java.lang.Math完整類名 Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot()); System.out.println(pi); } // 建立對象(List,Map) @Test public void test06() throws OgnlException { test01(); //建立List對象 Integer size = (Integer)Ognl.getValue("{'唐僧','悟空','八戒','沙僧'}.size()", oc, oc.getRoot()); String name = (String)Ognl.getValue("{'唐僧','悟空','八戒','沙僧'}[0]", oc, oc.getRoot()); String name1 = (String)Ognl.getValue("{'唐僧','悟空','八戒','沙僧'}.get(1)", oc, oc.getRoot()); System.out.println(size); System.out.println(name); System.out.println(name1); //建立Map對象 Integer size1 = (Integer)Ognl.getValue("#{'name':'tom','age':20,'adress':'秦皇島'}.size()", oc, oc.getRoot()); String name2 = (String)Ognl.getValue("#{'name':'tom','age':20,'adress':'秦皇島'}['name']", oc, oc.getRoot()); Integer age = (Integer)Ognl.getValue("#{'name':'tom','age':20,'adress':'秦皇島'}.get('age')", oc, oc.getRoot()); String adress = (String)Ognl.getValue("#{'name':'tom','age':20,'adress':'秦皇島'}.get('adress')", oc, oc.getRoot()); System.out.println(size1); System.out.println(name2); System.out.println(age); System.out.println(adress); } }
二:OGNL與Struts框架整合dom
1..導包jsp
2.配置struts文件ide
3.配置web.xmlpost
4.書寫actionui
package cn.itcast.ognltest; /** * @author 做者 * @version 建立時間:2018年7月24日 下午5:15:29 * 類說明: */ import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import cn.itcast.domain.User; public class StrutsOgnlAction extends ActionSupport implements ModelDriven<User>{ private User user = new User(); @Override public String execute() throws Exception { // TODO Auto-generated method stub System.out.println(user); return super.execute(); } @Override public User getModel() { // TODO Auto-generated method stub return user; } }
5.jspthis
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/StrutsOgnlAction" method="post"> 用戶名:<input type="text" name="name"/> <input type="submit" value="提交" /> </form> </body> </html>