ognl用法

1.       首先创建一个web项目,然后创建其余组件。

2.       导入struts包。

3.       编辑struts.xml.

4.  建立Book类。类中必须要求构造方法。

5.  建立ognlAction

6.  新建ognl.jsp

编辑struts.xml.


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
	<package name="struts2_ognl" extends="struts-default">
		<action name="ognl" class="com.ognlAction">
			<result name="success">/ognl.jsp</result>
		</action>
	</package>
</struts>    


建立Book类

package com;

 

public class Book {

private String isbn;

private String title;

private double price;

public Book(String isbn,String title,doubleprice){

    this.isbn=isbn;

    this.title=title;

    this.price=price;

}

public String getIsbn(){

    return isbn;

}

public void setIsbn(String isbn){

    this.isbn=isbn;

}

public String getTitle(){

    return title;

}

public void setTitle(String title){

    this.title=title;

}

public String getPrice(){

    return title;

}

public void setPrice(double price){

    this.price=price;

}

}

建立ognlAction

package com;

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.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;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public void setServletContext(ServletContext application) {
		this.application = application;
	}

	public List<Book> getBooks() {
		return books;
	}

	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;
	}
}

ognl.jsp

<body>
 <h3>访问OGNL上下文和Action上下文</h3>
    <p>parameters: <s:property value="#parameters.userName" /></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>
    
  </body>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值