Struts2 ognl表达式
生活随笔
收集整理的這篇文章主要介紹了
Struts2 ognl表达式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、實驗目的
1. 掌握OGNL表達式的語法結構
2. 掌握OGNL表達式獲取Action中的相關值的方法。
3. 理解投影的含義。
二、實驗內容
? ? ? ? 創建業務控制類OgnlAction,測試基本屬性、基本方法、類對象、靜態屬性、靜態方法、List列表、
Set集合、Map映射等各種類型參數采用OGNL表達式獲取的辦法。
三、實驗步驟及代碼
1、創建一些javaBean
a、User類 屬性int age,帶參構造方法
package com.tj.struts.bean;public class User {private int age;public User(){}/*** @param age*/public User(int age) {this.age = age;}/*** @return the age*/public int getAge() {return age;}/*** @param age the age to set*/public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "User"+age;}}
b、Dog類 屬性 String ?name 帶參構造方法
package com.tj.struts.bean;public class Dog {private String name ;/*** */public Dog() {// TODO Auto-generated constructor stub}/*** @param name*/public Dog(String name) {this.name = name;}/*** @return the name*/public String getName() {return name;}/*** @param name the name to set*/public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Dog"+name;} }c、Cat類 ?屬性Dog類對象 friend 成員方法miaomiao
package com.tj.struts.bean;public class Cat {private Dog friend;public Cat(){}/*** @param friend*/public Cat(Dog friend) {this.friend = friend;}/*** @return the friend*/public Dog getFriend() {return friend;}/*** @param friend the friend to set*/public void setFriend(Dog friend) {this.friend = friend;}public String miaomiao(){return "miaomiaoMethod";}@Overridepublic String toString() {return "Cat"+friend ;} }d、靜態內容類S ?靜態屬性STR ?靜態方法s()
package com.tj.struts.bean;public class S {public static String STC="static String ";public static String s(){return STC+"static mathod";} } 2、創建業務控制類a、包含基本屬性username、password
b、類成員User作為屬性
c、類成員Cat(Cat的屬性為Dog對象)作為屬性
d、保存User對象的列表users(存儲三個對象1,2,3)
e、保存dog對象的集合dogs(存儲三個對象dog1,dog2,dog3)
f、保存dog鍵值對的映射dogMap(存儲三組數據dog100:Dog(100) dog101: Dog(101) dog102:Dog(102))
g、包含基本方法m(返回字符串actionMethod)
package com.tj.struts.contoller;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.tj.struts.bean.Cat; import com.tj.struts.bean.Dog; import com.tj.struts.bean.User;public class OgnlAction extends ActionSupport{private String username;private String password;private Cat cat;private User user;private List<User> users=new ArrayList<User>();private Set<Dog> dogs=new HashSet<Dog>();private Map<String ,Dog> dogmap=new HashMap<String,Dog>();/*** @return the username*/public String getUsername() {return username;}/*** @param username the username to set*/public void setUsername(String username) {this.username = username;}/*** @return the password*/public String getPassword() {return password;}/*** @param password the password to set*/public void setPassword(String password) {this.password = password;}/*** @return the cat*/public Cat getcat() {return cat;}/*** @param cat the cat to set*/public void setcat(Cat cat) {this.cat = cat;}/*** @return the user*/public User getUser() {return user;}/*** @param user the user to set*/public void setUser(User user) {this.user = user;}/*** @return the users*/public List<User> getUsers() {return users;}/*** @param users the users to set*/public void setUsers(List<User> users) {this.users = users;}/*** @return the dogs*/public Set<Dog> getDogs() {return dogs;}/*** @param dogs the dogs to set*/public void setDogs(Set<Dog> dogs) {this.dogs = dogs;}/*** @return the dogmap*/public Map<String, Dog> getDogmap() {return dogmap;}/*** @param dogmap the dogmap to set*/public void setDogmap(Map<String, Dog> dogmap) {this.dogmap = dogmap;}public String m(){return "actionMethod";}public OgnlAction(){username="ognltester";password="123456789";user=new User();cat=new Cat(new Dog("dog0"));users.add(new User(1));users.add(new User(2));users.add(new User(3));dogs.add(new Dog("dog1"));dogs.add(new Dog("dog2"));dogs.add(new Dog("dog3"));dogmap.put("dog1200", new Dog("dog1200"));dogmap.put("dog1201", new Dog("dog1201"));dogmap.put("dog1202", new Dog("dog1202"));}}3、配置
a、web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"><display-name></display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><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> </web-app> b、struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts><package name="default" namespace="/" extends="struts-default"><action name="OGNL" class="com.tj.struts.contoller.OgnlAction"><result>/Ognltest.jsp</result></action></package><!-- 允許調用靜態方法 --><constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> </struts>4、測試
a、index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% 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><a href="OGNL">ognltest</a></body> </html>b、Ognltest.jsp <%@ 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 'Ognltest.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><%@taglib uri="/struts-tags" prefix="s" %><body ><center>這是ognl測試界面<br><br>1、訪問值棧中action的普通屬性:<br>username= <s:property value="username"/><br>2、訪問值棧中的對象的普通屬性:<br>user.age=<s:property value="user.age"/><br>user.age=<s:property value="user['age']"/><br>user.age=<s:property value="user[\"age\"]"/><br>3、訪問值棧中對象的普通屬性:<br><s:property value="cat.friend.name"/><br>4、訪問值棧中對象的普通方法:<br>password長度=<s:property value="password.length()"/><br>5、訪問值棧中對象的普通方法:<br><s:property value="cat.miaomiao()"/><br>6、訪問action中的普通方法:<br><s:property value="m()"/><br>7、訪問靜態方法:(*)<br><s:property value="@com.tj.struts.bean.S@s()"/><br>8、訪問靜態屬性:<br><s:property value="@com.tj.struts.bean.S@STC"/><br>9、訪問Math類的靜態方法:<br><s:property value="@@max(5,10)"/><br>10、訪問普通類的構造方法:<br><s:property value="new com.tj.struts.bean.User(10)"/><br>11、訪問list:<br><s:property value="users"/><br>12、訪問list中的某個元素:<br><s:property value="users[1]"/><br>13、訪問list元素中某個屬性的集合:<br><s:property value="users.{age}"/><br>14、訪問list元素中某個屬性的集合的特定值:<br><s:property value="users.{age}[0]"/><br>15、訪問Set:<br><s:property value="dogs"/><br>16、訪問Set中的某個元素:<br><s:property value="dogs[0]"/><br>17、訪問Map:<br><s:property value="dogmap"/><br>18、訪問Map中的某個元素:<br><s:property value="dogmap.dog1202"/><br>19、訪問Map中所有的Key:<br><s:property value="dogmap.keys"/><br>20、訪問Map中所有的value:<br><s:property value="dogmap.values"/><br>21、訪問容器的大小:<br><s:property value="dogmap.size()"/><br>22、投影:users集合中年齡為1的第一個元素<br><s:property value="users.{?#this.age==1}[0]"/><br>23、投影:users集合中年齡大于1的user的年齡的集合<br><s:property value="users.{?#this.age>1}.{age}"/><br>24、投影:users集合中年齡》1的user集合的最后一個user的age<br><s:property value="users.{$#this.age>1}.{age}"/><br>25、投影:判斷users集合中年齡>1的user集合的最后一個user的age是否為空<br><s:property value="users.{$#this.age>1}.{age}==null"/><br>26、根對象:“[0]代表值棧”<br><s:property value="[0].password"/><br></center></body> </html>
總結
以上是生活随笔為你收集整理的Struts2 ognl表达式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring_mvc ioc/DI 控制
- 下一篇: struts2 简单登录校验示例