Struts 2中的OGNL
来源:互联网 发布:韩顺平js 编辑:程序博客网 时间:2024/04/30 13:57
操作集合对象。
OGNL的用法
OGNL是通常要结合Struts 2的标志一起使用。主要是#、%和$这三个符号的使用。使用方法如下:
新建名为Struts2_OGNL的Web工程
#”主要有三种用途:
访问OGNL上下文和Action上下文,#相当于ActionContext.getContext();下表有几个ActionContext中有用的属性: 名称 作用 例子
parameters 包含当前HTTP请求参数的Map #parametersid[0]作用相当于request.getParameter("id")
request 包含当前HttpServletRequest的属性(attribute)的Map #request.userName相当于request.getAttribute("userName")
session 包含当前HttpSession的属性(attribute)的Map #session.userName相当于session.getAttribute("userName")
application 包含当前应用的ServletContext的属性(attribute)的Map #application.userName相当于application.getAttribute("userName")
attr 用于按request > session > application顺序访问其属性(attribute) #attr.userName相当于按顺序在以上三个范围(scope)内读取userName属性,直到找到为止
用于过滤和投影(projecting)集合,如books.{?#this.price<100};
构造Map,如#{'foo1':'bar1', 'foo2':'bar2'}。
下面让我们它们的具体写法,首先是Action类代码:
package tutorial.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 tutorial.model.Book;
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 session;
private ServletContext application;
private List 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 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();
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;
}
}
以上代码分别在request、session和application的范围内添加“userName”属性,然后再在JSP页面使用OGNL将其取回。
下面是Ognl.jsp的代码,内容如下:
访问OGNL上下文和Action上下文
parameters.userName:
request.userName:
session.userName:
application.userName:
attr.userName:
用于过滤和投影(projecting)集合
Books more than $35
<s:iterator value="books.{?#this.price > 35}">
<s:property value="title" /> - $<s:property value="price" /><br>
</s:iterator>
The price of "Code Complete, Second Edition" is:
构造Map
The value of key "foo1" is
清单3 WebContent/Ognl.jsp 以上代码值得注意的是“”,因为“books.{?#this.title=='Code Complete, Second Edition'}.{price}”返回的值是集合类型,所以要用“[索引]”来访问其值。 最后是Struts 2的配置文件struts.xml,内容如下:
/Ognl.jsp
“%”符号的用途是在标志的属性为字符串类型时,计算OGNL表达式的值。
“$”有两个主要的用途,用于在国际化资源文件中,引用OGNL表达式。在Struts 2配置文件
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- mysql问题:using expire_logs_days without log_bin crashes the server...
- 索引被更改时会生成undo,redo吗
- Nitro.Pro.v8.0.3.1.Incl.Keygen-Lz0
- Nitro.Pro.v8.0.3.1.X64.Incl.Keygen-Lz0
- Android常用代码及笔记
- Struts 2中的OGNL
- 静态链接库与动态链接库的区别
- LeetCode: Count and Say
- Repeater控件
- C++——头文件的作用
- 安装qt for android开发环境
- 实现权限控制的几种方式
- Struts2开发--类型转换
- MySQL Create Table语句的应用与语法