一.知识点学习

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语言表达式学习

java struts2入门学习--OGNL语言基本用法的更多相关文章

  1. java struts2入门学习--OGNL语言常用符号和常用标签学习

    一.OGNL常用符号(接上一篇文章): 1.#号 1)<s:property value="#request.username"/> 作用于struts2的域对象,而不 ...

  2. java struts2入门学习--防止表单重复提交.OGNL语言学习

    一.知识点回顾 防止表单重复提交核心思想: 客户端和服务器端和写一个token,比较两个token的值相同,则非重复提交;不同,则是重复提交. 1.getSession三种方式比较: request. ...

  3. java struts2入门学习实例--将客户端IP地址和访问方式输出到浏览器

    实例1:实现客户端IP地址和访问方式输出到浏览器. IpAction.java package com.amos.web.action; import javax.servlet.http.HttpS ...

  4. java struts2入门学习--基于xml文件的声明式验证

    一.知识点总结 后台验证有两种实现方式: 1 手工验证顺序:validateXxx(针对Action中某个业务方法验证)--> validate(针对Action中所有的业务方法验证) 2 声明 ...

  5. java struts2入门学习---国际化

    一.国际化的概念 1.不同国家的人访问同一个网站,显示的语言不同. 2.对JSP页面进行国际化 属性(properties)文件命名规则:基名---语言--国家如, message_zh_CN.pro ...

  6. java struts2入门学习---拦截器学习

    一.拦截器,拦截器栈 1.拦截器的作用 拦截器本质上和servlet的过滤器是一样的.在struts2中,拦截器能够对Action前后进行拦截,拦截器是一个可插拨的,你可以选择使用拦截器,也可以卸载拦 ...

  7. java struts2入门学习实例--使用struts2快速实现上传

    一.文件上传快速入门 1).关于上传表单三要素 >>尽量以POST请求方式上传,因为GET支持文件大小是有限制的. >>必须要加上enctype="multipart ...

  8. java struts2入门学习---自定义类型转换

    自定义类型转换器的作用就是将struts无法识别的类型转换成自己所需要的. 比如输入:广东-东莞-虎门,对应的输出时能输出:广东省 东莞市 虎门(镇/区) 这里涉及到的知识点即是将String转换为任 ...

  9. java struts2入门学习---文件下载的二种方式

    一.关于文件下载: 文件下载的核心思想即是将文件从一个地方拷贝到另一个地方. 1.传统方式: 在Action中加入大量servlet api 操作.优点是好理解,缺点是耦合度高. 2.stream方式 ...

随机推荐

  1. CSS3动画的回调处理

    我们在做js动画的时候,很多时候都需要做回调处理,如在一个动画完成后触发一个事件.一个动画完成后执行另外一个动画等等,但在使用CSS3动画时能不能捕获到运动的状态做回调处理呢? CSS3动画也是可以做 ...

  2. 图片上传前预览、压缩、转blob、转formData等操作

    直接上代码吧: <template> <div> <div class="header">添加淘宝买号</div> <div ...

  3. 【MySQL】PostgresSQL-MySQL对比

    PostgresSQL-MySQL对比 (5 条消息)PostgreSQL 与 MySQL 相比,优势何在? - 知乎 IOC匹配 - 天眼公共空间 - 360企业安全Confluence 调查分析 ...

  4. C# 源码 AForge.NET

    AForge.NET是一个专门为开发者和研究者基于C#框架设计的,他包括计算机视觉与人工智能,图像处理,神经网络,遗传算法,机器学习,模糊系统,机器人控制等领域.这个框架由一系列的类库组成.主要包括有 ...

  5. [Angular] Use Angular components in AngularJS applications with Angular Elements

    When migrating AngularJS (v1.x) applications to Angular you have different options. Using Angular El ...

  6. 小议IE10下的DrawToBitmap方法

    在完成博文“PS网页设计教程XXIV——从头设计一个漂亮的网站”后. 出于习惯,打开之前“利用Webbrowser类实现超长网页的截屏的实现(解决报错不能截取的难题)”中的代码的程序,截取博文作为资料 ...

  7. 查看 js对象

    for (var obj in data) { document.write( '|'+obj +'|'); };

  8. brew install Jenkins

    Chens-MacBook-Pro:Downloads chenqing$ brew install jenkins ==> Downloading http://mirrors.jenkins ...

  9. DIV+CSS IE6/IE7/IE8/FF兼容问题汇总

    1.IE8下兼容问题,这个最好处理,转化成ie7兼容就可以.在头部加如下一段代码,然后只要在IE7下兼容了,IE8下面也就兼容了 <meta http-equiv="x-ua-comp ...

  10. 使用Spring框架入门一:基于XML配置的IOC/DI的使用

    一.Spring框架 1.方法一:逐项导入基础依赖包: spring-core.spring-beans.spring-context.spring-expression 2.方法二:最简洁的导入,直 ...