Struts2学习笔记OGNL例子代码
来源:互联网 发布:最新大型网络手游 编辑:程序博客网 时间:2024/06/05 06:14
Struts2学习笔记OGNL例子代码
|字号 订阅
OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功 能。它使用相同的表达式去存取对象的属性。(摘自百度百科)
第一部分:OgnlAction类,继承自ActionSupport
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import cn.hxex.struts.core.ognl.bean.Student;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class OgnlAction extends ActionSupport {
@SuppressWarnings("unchecked")
public String execute() {
Map sessionMap = ActionContext.getContext().getSession();
sessionMap.put("userName", "David");
List<Student> students = new ArrayList<Student>();
students.add( new Student("Mike", 20) );
students.add( new Student("Tom", 18) );
students.add( new Student("Jack", 16) );
students.add( new Student("White", 15) );
setStudents( students );
return SUCCESS;
}
private List<Student> students;
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
第二部分JSP页面
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Ognl</title>
</head>
<body>
<h2>Access Action Context:</h2>
session.userName: <s:property value="#session.userName" />
<h2>Projection And Selection Operators:</h2>
Mike age is:
<s:property value="students.{?#this.name=='Mike'}.{age}[0]"/><br/>
Students that larger than 17:
<s:iterator value="students.{?#this.age > 17}">
<li><s:property value="name" /> - <s:property value="age" /></li>
</s:iterator>
<h2>Construction Map:</h2>
<s:set name="foobar" value="#{ 'foo' : 'foo value', 'bar' : 'bar value' }" />
The value of key "foo" is <s:property value="#foobar['foo']" />
</body>
</html>
第三部分运行调试:
请求页面的表单内容
<s:form action="ognl">
<s:submit value="submit"></s:submit>
</s:form>
环境搭建:
struts.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="core" extends="struts-default">
<!-- class里的是处理这个请求的类 -->
<action name="ognl" class="com.ognl.action.OgnlAction">
<!-- 处理完之后转去的页面 -->
<result>/ognl/ognl.jsp</result>
</action>
</package>
</struts>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>ognl</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>ognl</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.properties配置(不写也可以,但是会出现警告)
struts.locale=en_GB