Hi_Amos
坚持每天都在进步!!

一.知识点学习

1.struts2中包含以下6种对象,requestMap,sessionMap,applicationMap,paramtersMap,attr,valueStack;

 

1)requestMap用来存放包含当前HttpServletRequest的属性(attribute)的Map,简单来说就是request域中的值;   

2)sessionMap用来存放包含当前HttpSession的属性(attribute)的Map

3)applicationMap用来存放包含当前应用的ServletContext的属性(attribute)的Map

4)paramtersMap包含当前HTTP请求参数的Map 

5)attr,只是用来取值,用于按request > session > application顺序访问其属性(attribute

6)valueStack值栈是Action的数据中心,关于Action中的所有Value都是存放在valueStack中.

 

2.OGNL几种常见的符号用法

<s:property value="#attr.username"/>会在以下几个域对象中依次查询

[pageContext对象]===>requestMap对象===>valueStack对象===>sessionMap对象===>applicationMap对象===>空白字符串

注意:pageContext对象不是Struts2的数据中心之一.

3.#用法:

1) <s:property value="#request.username"/> 作用于struts2的域对象,而不是普通域对象

2)<s:property value="#user.username"/>作用于JavaBean对象

3)<s:property value="#username"/><===><s:property value="username"/>作用于普通字符串

 

二.实例

1.源码

OgnlAction.java

package ognl;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class OgnlAction extends ActionSupport {
    private static final long serialVersionUID = -842453764814706964L;
    public String execute() throws Exception {
        // 放置在requestMap对象中 1
        ServletActionContext.getRequest().setAttribute("username", "request_username");
        // 放置在SessionMap对象中3
        ActionContext.getContext().getSession().put("username", "session_username");
        // 放置在ApplicationMap对象中
        ServletActionContext.getServletContext().setAttribute("username", "application_username");
        //放置在ValueStack对象中2
        ActionContext.getContext().getValueStack().set("username", "valuestack_username");;

        return SUCCESS;
    }
}

User.java

package ognl;

/** 
* @ClassName: User 
* @Description: 用户
* @author: amosli
* @email:amosli@infomorrow.com
* @date Feb 17, 2014 11:00:11 PM  
*/
//JavaBean对象
public class User {
    private Integer id;// 编号
    private String name;// 姓名
    private Integer age;// 年龄
    public User() {
    }
    public User(Integer id, String name, Integer age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

 

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <!--<include file="config/upload.xml"></include> -->
    <!-- 加载其他配置文件 -->
    <!-- <include file="config/upload-interceptor.xml"></include> -->
    <!-- 加载属性文件-国际化 -->
    <!-- <constant name="struts.custom.i18n.resources" value="message"></constant> -->    <!-- 结果集 -->
    <!-- <include file="config/result_struts.xml"></include> -->
    <!-- 类型转换 -->
    <!-- <include file="config/type_struts.xml"></include> -->
    <!-- 文件下载 -->
    <!-- <include file="config/download_struts.xml"></include> -->
    <!-- 验证validator -->
    <!-- <include file="config/validator_struts.xml"></include> -->
    
    <!-- ognl语言表达式 -->
    <include file="config/ognl_struts.xml"></include>
</struts>

 

ognl_struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="ognl" extends="struts-default">
        <action name="OgnlAction" class="ognl.OgnlAction" method="execute">
            <result name="success" type="dispatcher">
            /WEB-INF/ognl_success.jsp
            </result>
        </action>
    </package>
</struts>

 

ognl_2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%@ page import="java.util.*" %>
<%@ page import="ognl.User" %>
<!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>Insert title here</title>
</head>
<body>
<%
    List<User> userList = new ArrayList<User>();
    userList.add(new User(1,"张三",20));
    userList.add(new User(2,"李四",25));
    userList.add(new User(3,"amos",30));
    pageContext.setAttribute("userList", userList);
%>
<hr>
<table border="1" align="center">
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>年龄</th>
    </tr>
<s:iterator var="user" value="#attr.userList">
    <tr>
        <td><s:property value="#user.id"/></td>
        <td><s:property value="#user.name"/></td>
        <td><s:property value="#user.age"/></td>
    </tr>
</s:iterator>
</table>
<!-- 只取name一列 -->
<hr>
<table border="1" align="center">
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>年龄</th>
    </tr>
<s:iterator var="username" value="#attr.userList.{name}">
    <tr>
        <td></td>
        <!-- #在这里加不加都是一样的 -->
        <td><s:property value="#username"/></td>
        <td></td>
    </tr>
</s:iterator>
</table>
</body>
</html>

 

ognl_success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>Insert title here</title>
</head>
<body>
requestMap对象: <s:property value="#request.username"/><br>
sessionMap对象: <s:property value="#session.username"/><br>
applicationMap对象: <s:property value="#application.username"/><br>
valueStack对象: <s:property value="username"/><br>
parametersMap对象: <s:property value="#parameters.username"/><br>
attr对象: <s:property value="#attr.username"/>
</body>
</html>

2.效果图

1).验证上面所说的6个对象

2)#的用法

 

3.源码分析

1)struts.xml在程序运行时就被加载到内存中,struts.xml加载了config/ognl_struts.xml.

2)config/ognl_struts.xml中配置了ognl.OgnlAction,如果成功那么页面转发到/WEB-INF/ognl_success.jsp

3)OgnlAction.java中为除了attr外的5种对象赋值.

4)WEB-INF/ognl_success.jsp中取出OgnlAction中的赋值.

 其中取值的方式<s:property value="#request.username"/>,主要用到#的第一种用法.

        <s:property value="#attr.username"/>,主要可以用来测试attr取值的顺序.

5)User.java定义了一个JavaBean对象,包含三个字段id,name,age;

6)ognl_2.jsp,新建list,将定义的JavaBean对象的值赋给userList,并将值传递到当前页面pageContext中.

<s:iterator var="user" value="#attr.userList">
<tr>
<td><s:property value="#user.id"/></td>
<td><s:property value="#user.name"/></td>
<td><s:property value="#user.age"/></td>
</tr>
</s:iterator>

其中使用#attr来获取pageContext中的内容.然后使用<s:iterator>标签进行遍历,并将值存放到user中.

然后再通过#user.id来取出变量user中id的值,这里也表现了JavaBean对象中非值栈中的value,一定要记得加上#

取单个值的格式如下,#attr.userList.{name}只取userList中的name这一列.

<s:iterator var="username" value="#attr.userList.{name}">
    <tr>
        <td></td>
        <!-- #在这里加不加都是一样的 -->
        <td><s:property value="#username"/></td>
        <td></td>
    </tr>
</s:iterator>

针对不JavaBean对象,加不加#效果是一样的.

4.本文源码链接

OGNL语言表达式学习

 

 

 

 

 

 

posted on 2014-02-18 00:20  Hi_Amos  阅读(1289)  评论(4编辑  收藏  举报