[info]一.读取ObjectStack里的对象的属性[/info]
1.设计模型类Person。
com.edu.ognl.action/person.java
[sourcecode language=”java” title=”com.edu.ognl.action/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;
}
}[/sourcecode]
2.设计Action,并配置Action,其代码如下。
com.edu.ognl.action/PersonAction.java
[sourcecode language=”java” title=”com.edu.ognl.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;}
}[/sourcecode]
3.设计显示信息的JSP页面。
showognl.jsp
[sourcecode language=”html” title=”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>[/sourcecode]
4.配置web。
web.xml
[sourcecode language=”xml” title=”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>[/sourcecode]
5.启动服务器,打开链接http://localhost:8080/(项目名)/test/personOgnlTest。
效果图:
[info]二.读取ContextMap里的的对象的属性[/info]
1.设计Aciton,并采用注释配置Action。
com.edu.ognl.action/OgnlAction.java
[sourcecode language=”java” title=”com.edu.ognl.action/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;
}
}[/sourcecode]
2.设计显示信息的JSP页面。
showognl2.jsp
[sourcecode language=”html” title=”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>[/sourcecode]
3.启动服务器,打开链接http://localhost:8080/(项目名)/test/ognlTest?msg=shangdong。
效果图:
[info]三.访问数组、List和Map类型的属性[/info]
1.设计JSP页面,其代码如下。
showognl3.jsp
[sourcecode language=”html” title=”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>[/sourcecode]
2.启动服务器,打开链接http://localhost:8080/(项目名)/showognl3.jsp。
效果图:
项目下载:[bdbtn]https://pan.benzhu.xyz/%E4%BB%A3%E7%A0%81/%E6%BA%90%E4%BB%A3%E7%A0%81/%E9%A1%B9%E7%9B%AE/Struts2%E7%9A%84OGNL%E8%A1%A8%E8%BE%BE%E5%BC%8F%E4%B8%8E%E5%BA%94%E7%94%A8%E6%A1%88%E4%BE%8B.rarr[/bdbtn]