20160502-struts2入门--ognl表达式

public class StrutsRequestWrapper extends HttpServletRequestWrapper {
public StrutsRequestWrapper(HttpServletRequest req) {
super(req);
}
public Object getAttribute(String s) {
......
ActionContext ctx = ActionContext.getContext();
Object attribute = super.getAttribute(s);//先从request范围获取属性值
if (ctx != null) {
if (attribute == null) {//如果从request范围没有找到属性值,即从ValueStack中查找对象的属性值
......
ValueStack stack = ctx.getValueStack();
attribute = stack.findValue(s);
......
}
}
return attribute;
}
}
<s:set name="list" value="{'zhangming','xiaoi','liming'}" />
<s:iterator value="#list" id="n">
<s:property value="n"/><br>
</s:iterator>
<s:set name="foobar" value="#{'foo1':'bar1', 'foo2':'bar2'}" />
<s:iterator value="#foobar" >
<s:property value="key"/>=<s:property value="value"/><br>
</s:iterator
<s:if test="'foo' in {'foo','bar'}">
在
</s:if>
<s:else>
不在
</s:else>
<s:if test="'foo' not in {'foo','bar'}">
不在
</s:if>
<s:else>
在
</s:else>
<s:iterator value="books.{?#this.price > 35}">
<s:property value="title" /> - $<s:property value="price" /><br>
</s:iterator>
public class BookAction extends ActionSupport {
private List<Book> books;
....
@Override
public String execute() {
books = new LinkedList<Book>();
books.add(new Book("A735619678", "spring", 67));
books.add(new Book("B435555322", "ejb3.0",15));
}
}
示例代码:
book.java
package com.dzq.domain;
public class Book {
private int bookid;
private String name;
private int price;
public int getBookid() {
return bookid;
}
public void setBookid(int bookid) {
this.bookid = bookid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Book(int bookid, String name, int price) {
this.bookid = bookid;
this.name = name;
this.price = price;
}
}
PersonListAction.java
package com.dzq.action;
import java.util.*;
import com.dzq.domain.Book;
public class PersonListAction {
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(){
books=new ArrayList<Book>();
books.add(new Book(1, "黑客与画家", 23));
books.add(new Book(2, "傲慢与偏见", 35));
books.add(new Book(3, "春娇与志明", 45));
name="小明";
return "list";
}
}
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="dzq" namespace="/dzq" extends="struts-default">
<action name="list" class="com.dzq.action.PersonListAction">
<result name="list">/WEB-INF/page/personlist.jsp</result>
</action>
</package>
</struts>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setAttribute("user", "xiaoduc");
request.getSession().setAttribute("user", "hello");
%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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>
<s:property value="#request.user"/><br>
<s:property value="#session.user"/><br> <s:set var="list" value="{'第一个','第二个','第三个'}" />
<s:iterator value="#list">
<s:property/><br>
</s:iterator>
<s:set var="maps" value="#{'key1':90,'key2':35,'key3':40 }" />
<s:iterator value="#maps">
<s:property value="key" />= <s:property value="value"/><br>
</s:iterator> <s:if test="'foo' in {'foo','value'}">
在
</s:if>
<s:else>
不在
</s:else> </body>
</html>
personlist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!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>
<s:property value="name"/>
${name }<br>
<s:iterator value="books.{?#this.price>30}" >
书名:<s:property value="name"/><br>
价格:<s:property value="price"/><br>
<hr>
</s:iterator>
</body>
</html>
20160502-struts2入门--ognl表达式的更多相关文章
- struts2简单入门-OGNL表达式
什么是OGNL表达式 Object-Graph Navigation Language的缩写. 可以遍历整个对象结构图,实现对象类型转换等功能的表达式. OGNL实际上是个Map集合,有一个上下文根对 ...
- Struts2的OGNL表达式语言
一.OGNL的概念 OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,可以任意存取对象的属性或者 ...
- 初窥struts2(二)OGNL表达式
Struts2总结 Struts2完整的处理流程: 1 客户端发送请求,交给struts2控制器(StrutsPrepareAndExecuteFilter). 2 Filter控制器进行请求过滤 ...
- struts2 与 OGNL 表达式,jsp中 利用ognl 在valuestack中取值
在Struts2中,一个请求在终于到达Action的方法之前,Action对象本身会被压入ValueStack(实际上就是放到ValueStack的CompoundRoot中),所以Action对象是 ...
- Struts2之OGNL表达式
OGNL(Object-Graph Navigation Language的简称),对象图导航语言,它是一门表达式语言,除了用来设置和获取Java对象的属性之外,另外提供诸如集合的投影和过滤以及lam ...
- struts2(四) ognl表达式、值栈、actionContext之间的关系
今天来说说ognl表达式在struts2中的运用. --wh 一.什么是Ognl? 通过百度百科查询到的解释,其中详细的说明了OGNL的作用. 下面我们就对OGNL这5个作用进行讲解 1.存取对象的任 ...
- Struts2之 OGNL表达式和值栈
技术分析之OGNL表达式概述(了解) 1. OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写 * 所谓对象图,即以任意 ...
- struts2:OGNL表达式,遍历List、Map集合;投影的使用
OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存 ...
- struts2:OGNL表达式之#、%、$符号运用
1. OGNL表达达符号"#" 1.1 #用于访问OGNL上下文和Action上下文,#相当于ActionContext.getContext() 注意:当系统创建了Action实 ...
随机推荐
- 曝光卖假币的店铺和旺旺ID
1:店铺名称:九玖邮币 掌柜ID:九玖邮币[此人较贼,价格只比我们低一点,图用的不太清晰的,或者直接用真品图,而且卖假币卖到2钻了,很多人上当了.]1.5 店铺名称:聚鑫钱币 掌柜ID:聚鑫钱币 [1 ...
- Poj2002 Squares
题意描述:有一堆平面散点集,任取四个点,求能组成正方形的不同组合方式有多少.相同的四个点,不同顺序构成的正方形视为同一正方形. 思路变迁: 1.最简单的方法,直接暴力搜索,即依次取四个顶点,根据其坐标 ...
- 教程-for do、while do 中循环退出
1.注意条件设置 for i:=1 to 10 dobegin if i>5 then begin break; end;end; break 全部continu ...
- CALayer精讲
前言 CALayer包含在QuartzCore框架中,这是一个跨平台的框架,既可以用在iOS中又可以用在Mac OS X中.后面要学Core Animation就应该先学好Layer(层). 我们看一 ...
- 48. 面向对象的LotusScript(十四)之Log4Dom上
日志是开发系统时的有效工具和常见需求.它不仅可以在程序排错时提供调试信息,还可以记录系统运行的日常状况,以供需要时查询或集中起来分析.在一些主要的编程语言如Java中,都有不少日志框架可供选择.在Lo ...
- 【3D研发笔记】之【数学相关】(一):坐标系
现在开始学习3D基础相关的知识,本系列的数学相关笔记是基于阅读书籍<3D数学基础:图形与游戏开发>而来,实现代码使用AS3,项目地址是:https://github.com/hammerc ...
- C++学习笔记(六):复杂数据类型(string、容器和STL)
STL 即Standard Template Library(标准模板库),由于C++自带的数据类型过于简单,并不能满足我们的使用需要,而STL作为C++标准的内置库为我们编写好了多种高效的数据结构和 ...
- mac磁盘满解决方案
背景 : 用mac电脑的人,估计都不习惯去关机吧.mac虽然可以不需要关闭电脑,但是久而久之由于应用软件占用产生缓存文件 or 产生虚拟内容交换文件 or 睡眠镜像文件 and so on. 会占用大 ...
- URAL 1019 - Line Painting
跟前面某个题一样,都是区间染色问题,还是用我的老方法,区间离散化+二分区间端点+区间处理做的,时间跑的还挺短 坑爹的情况就是最左端是0,最右端是1e9,区间求的是开区间 #include <st ...
- Android上实现仿IOS弹性ScrollView
[转]http://www.tuicool.com/articles/ummIJb