strust2 ognl例子
来源:互联网 发布:windows汉语意思 编辑:程序博客网 时间:2024/05/21 22:52
1.定义Action
package com.test.action;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import com.struts2.bean.*;
import com.opensymphony.xwork2.ActionSupport;
public class OgnlAction extends ActionSupport implements ServletRequestAware, SessionAware, ServletContextAware {
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private Map<String, String> session;
private ServletContext application;
private List<Book> books;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
@SuppressWarnings("unchecked")
public void setSession(Map session) {
this.session = session;
}
public void setServletContext(ServletContext application) {
this.application = application;
}
public List<Book> getBooks() {
return books;
}
@Override
public String execute() {
request.setAttribute("userName", "Max From request");
session.put("userName", "Max From session");
application.setAttribute("userName", "Max From application");
books = new LinkedList<Book>();
books.add(new Book("978-0735619678", "Code Complete, Second Edition", 32.99));
books.add(new Book("978-0596007867", "The Art of Project Management", 35.96));
books.add(new Book("978-0201633610", "Design Patterns: Elements of Reusable Object-Oriented Software", 43.19));
books.add(new Book("978-0596527341", "Information Architecture for the World Wide Web: Designing Large-Scale Web Sites", 25.19));
books.add(new Book("978-0735605350", "Software Estimation: Demystifying the Black Art", 25.19));
return SUCCESS;
}
}
2.定义struts.xml
<action name="OgnlAction" class="com.test.action.OgnlAction">
<result name="success">/ognl/Ognl.jsp</result>
</action>
3.定义jsp文件
<h3>访问OGNL上下文和Action上下文</h3>
<p>parameters: <s:property value="#parameters.userName[0]" /></p>
<p>request.userName: <s:property value="#request['userName']" /></p>
<p>session.userName: <s:property value="#session.userName" /></p>
<p>application.userName: <s:property value="#application.userName" /></p>
<p>attr.userName: <s:property value="#attr.userName" /></p>
<hr />
<h3>用于过滤和投影(projecting)集合</h3>
<p>Books more than $35</p>
<ul>
<s:iterator value="books.{?#this.price > 35}">
<li><s:property value="title" /> - $<s:property value="price" /></li>
</s:iterator>
</ul>
<p>The price of "Code Complete, Second Edition" is: <s:property value="books.{?#this.title=='Code Complete, Second Edition'}.{price}[0]"/></p>
<hr />
<h3>构造Map</h3>
<s:set name="foobar" value="#{'foo1':'bar1', 'foo2':'bar2'}" />
<p>The value of key "foo1" is <s:property value="#foobar['foo1']" /></p>
<hr />
<h3>%的用途</h3>
<p><s:url value="#foobar['foo1']" /></p>
<p><s:url value="%{#foobar['foo1']}" /></p>
4.浏览器访问地址:http://localhost:8989/Struts2/OgnlAction.action?userName=leon,最后输出结果:
访问OGNL上下文和Action上下文
parameters: leon
request.userName: Max From request
session.userName: Max From session
application.userName: Max From application
attr.userName: Max From request
用于过滤和投影(projecting)集合
Books more than $35
The Art of Project Management - $35.96
Design Patterns: Elements of Reusable Object-Oriented Software - $43.19
The price of "Code Complete, Second Edition" is: 32.99
构造Map
The value of key "foo1" is bar1
%的用途
#foobar['foo1']?userName=leon
bar1?userName=leon
- strust2 ognl例子
- Strust2-OGNL表达式
- strust2 OGNL表达式
- ognl.ExpressionSyntaxException: Malformed OGNL expression(Strust2)
- Strust2第(十)篇《ognl概念和原理详解》
- strust2 ognl或者el表达式遍历嵌套list集合
- Struts2 OGNL表达式例子
- bjsxt的OGNL例子
- OGNL表达式相关例子
- eclipse——配置strust2框架例子
- Strust2
- strust2
- Strust2
- Struts2 OGNL的一个例子
- Strust2第(十一)篇《OGNL表达式的基本语法和用法》
- JSTL标签和EL表达式以及Strust2标签和ognl表达式
- 【Struts2框架】第三节OGNL表达式-OGNL表达式例子
- 【Struts2框架】第三节OGNL表达式-OGNL表达式例子2
- 9种不同的服务器程序设计范式
- ARM学习之路(一) linux操作指令学习..(by 星空武哥)
- 不再纠结Java中的String类
- Record of NS_Simulator bug fixed.
- Heritrix3.0教程 源码分析(一) Heritrix 3.0新特性新功能介绍
- strust2 ognl例子
- 4月7日网络安全小总结
- 在tomcat的server.xml中配置数据库连接池和手动部署到tomcat
- ubuntu下无线网设置
- Struts2+Jquery实现Aajx
- android content provider 使用
- 驯服爬虫 Heritrix
- ARM学习之路(二) Vi编辑器的使用(by 星空武哥)
- 卡特兰数 Catalan数 ( ACM 数论 组合 )