struts2中的OGNL表达式语言
不适用任何框架时编写的JSP页面,在JSP页面中通过EL表达式只能访问page/request/session/aaplication范围的属性;
PersonAction.xml:
Book.java:
index.jsp:
结果:
而使用struts框架可以通过EL表达式访问Action中的属性,这是由于:
案例:
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="book" extends="struts-default" namespace="/person"> <action name="book" class="blog.action.PersonAction"> <result name="input">/index.jsp</result> <result name="success">/index.jsp</result> </action> </package> </struts>
PersonAction.xml:
package blog.action; import java.util.ArrayList; import java.util.List; import blog.bean.Book; import com.opensymphony.xwork2.ActionSupport; public class PersonAction extends ActionSupport { private String name; private List<Book> books; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this.books = books; } public String execute(){ this.books = new ArrayList<Book>(); this.books.add(new Book(1, "spring", 100)); this.books.add(new Book(2, "ejb3.0", 120)); this.books.add(new Book(3, "struts2.0", 78)); this.name = "李大仁"; return "success"; } }
Book.java:
package blog.bean; public class Book { private int id; private String name; private double price; 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 double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Book(int id,String name,double price){ this.id = id; this.name = name; this.price = price; } }
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <% request.setAttribute("user1","lisi"); session.setAttribute("user2","zhangsan"); %> <!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"> </head> <body> --通过EL表达式直接访问Action的属性--<br/> ${name }<br/> -- 通过OGNL表达式访问request和session范围的数据 --<br/> <s:property value="#request.user1"/><br/> <s:property value="#session.user2"/><br/> <s:set var="list" value="{'user1','user2','user3'}"></s:set> <br/>-- s:iterator在迭代集合时有个特点:会把当前迭代的对象放在值栈的栈顶 --<br/> <s:iterator value="#list"> <s:property/><br/> </s:iterator> <br/> -- 采用OGNL表达式创建集合 --<br/> <s:set var="maps" value="#{'key1':11,'key2':22,'key3':33}"></s:set> <s:iterator value="#maps"> <s:property value="key"/> = <s:property value="value"/> <br/> </s:iterator> <br/>-- 采用OGNL表达式判断对象是否存在于集合中 --<br/> <s:if test="'foo' in {'foo','abc'}"> 在 </s:if> <s:else> 不在 </s:else> <br/> <s:if test="'foo' not in {'xxxx','abc'}"> 不在 </s:if> <s:else> 在 </s:else> <br/><br/> -- OGNL表达式的投影功能 --<br/> <s:iterator value="books.{?#this.price>80}"> <s:property value="name"/> : 价格 <s:property value="price"/> <br/> </s:iterator> </body> </html>
结果: