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实 ...
随机推荐
- centos firefox中文乱码问题
下载两个rpm包 fonts-chinese-3.02-9.6.e15.noarch.rpm fonts-ISO8859-2-75dpi-1.0-17.1.noarch.rpm 然后安装rmp rpm ...
- hbase运行模式
Hbase有两种运行模式:standalone和distributed.standalone模式参见Quick Start Guide.以distributed模式设置Hbase,需要编辑Hbase ...
- Visual Studio 2008 – ASP.NET “System.Runtime.InteropServices.COMException”
The Issue When openning an existing ASP.NET project for the first time in Visual Studio 2008 it retu ...
- 5A的肖特基二极管 SK5x / SK5xx
5A的肖特基 管压降经实测2A/0.3V, SK5x 和SK5xx别在于前者是四位命名,后者是一个五位命名带B, 封装不一样,参数基本一致.不细看手册容易用错了. 四位命名:封装为SMC (DO-21 ...
- 单独批次性任务采用MySQL定时器解决需求
有时候我们在开发的时候会遇到一些需求是在某个固定的时间段实现某些特殊功能,只做一次或者有规律的每分钟一次每小时一次,那么这个时候我们可以启用MySQL的定时器来帮忙解决该问题. 比如,有一个场景是要求 ...
- Cocos2d-x 3.x学习笔记(一):开始Cocos2d之旅
首先,进入官网下载cocos2d-x:http://www.cocos.com/download/,当然你需要注册一个账号才可以下载. 接下来需要跟着官网的配置文档配置一下开发环境,不得不说,Coco ...
- Struts的类型转换
struts中jsp提交的数据,struts会自动转换为action中的属性类型: 对于基本的数据类型以及日期类型会自动转换: 就像前面那一篇博客的代码封装一样,会把字符串类型传过来的数据转化为相应的 ...
- 【转】使用junit4进行单元测试(高级篇)
转自:http://blog.csdn.net/andycpp/article/details/1329218 通过前 2 篇文章,您一定对 JUnit 有了一个基本的了解,下面我们来探讨一下JUni ...
- HubbleDotNet 学习之路
1.创建后台任务实现自动同步更新表数据.打开工具后点击“management”选项卡,选择“task scheduler management”,在弹出的窗口中点击右侧的"add" ...
- Codeforces Round #268 (Div. 1) A. 24 Game 构造
A. 24 Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/468/problem/A D ...