一.读取ObjectStack里的对象的属性

1.设计模型类Person

person.java

package com.edu.ognl.action;

import java.util.Date;

public class Person {
    private String name;
    private int age;
    private Date birthday;
    public Person() {

    }
    public Person(String name,int age,Date birthday) {
        this.name=name;
        this.age=age;
        this.birthday=birthday;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

}

2.设计Action

PersonAction.java

package com.edu.ognl.action;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

@Namespace("/test")
@ParentPackage("struts-default")
@Results({@Result(name = "success",location="/showognl.jsp")})
public class PersonAction extends ActionSupport {
    private static final long serialVersionUID =1L;
    private Person person;
    @Action("personOgnlTest")
    public String ognlTest() throws Exception{
        person = new Person("张三",26,new Date());
        return SUCCESS;
    }
    public Person getPerson() {return person;}
    public void setPerson(Person person) {this.person = person;}
}

3.设计显示信息的JSP页面

showognl.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Struts2的OGN表达式与应用案例</title>
</head>
<body>
<h3>(1)读取ObjectStack里的对象的属性</h3>
Person={姓名:<s:property value="person.name"/>;
年龄:<s:property value="person.age"/>;
出生日期:<s:property value="person.birthday"/>}<br>

</body>
</html>

4.配置web

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>benzhu1</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

5.测试

启动服务器,打开链接http://localhost:8080/(项目名)/test/personOgnlTest。

二.读取ContextMap里的的对象的属性

1.设计Aciton

OgnlAction.java

package com.edu.ognl.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/test")
@ParentPackage("struts-default")
@Results({@Result(name = "input",location="/showognl2.jsp")})
public class OgnlAction extends ActionSupport {
    private static final long serialVersionUID =1L;
    @Action("ognlTest")
    public String ognlTest() throws Exception{
        ActionContext ctx=ActionContext.getContext();
        ctx.getApplication().put("msg", "application信息");
        ctx.getSession().put("msg", "seesion信息");
        HttpServletRequest request=ServletActionContext.getRequest();
        request.setAttribute("msg", "request信息");
        return INPUT;
    }
}

2.设计显示信息的JSP页面

showognl2.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!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>Struts2的OGNL表达式与应用案例2</title>
</head>
<body>
<h3>(2)访问OGNL上下文和Action上下文–使用OGNL访问属性值</h3>
parameters:<s:property value="#parameters.msg"/><br>
request.mgs:<s:property value="#request.msg"/><br>
session.mgs:<s:property value="#session.msg"/><br>
application.mgs:<s:property value="#application.msg"/><br>
attr.mgs:<s:property value="#attr.msg"/> <br>
</body>
</html>

3.测试

启动服务器,打开链接http://localhost:8080/(项目名)/test/ognlTest?msg=shangdong。

三.访问数组、List和Map类型的属性

1.设计JSP页面

showognl3.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!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>Struts2的OGNL表达式与应用案例3</title>
</head>
<body>
<h3>(3)构造Man,并显示其信息</h3>
<s:set name="foobar" value="#{‘foo1′:’bar1′,’foo2′:’bar2’ }"/>
The value of key "fool" is <s:property value="#foobar[‘foo1’]"/><br>

<h3>(4)创建list集合,并且遍历出集合中的值</h3>
<s:set name="list" value="{‘eeeee’,’ddddd’,’ccccc’,’bbbbb’,’aaaaa’}"></s:set>
<s:iterator value="#list" var="a">
<s:property value="a" />
</s:iterator>
<hr/>
</body>
</html>

2.测试

启动服务器,打开链接http://localhost:8080/(项目名)/showognl3.jsp。