一.知识点学习

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. NLP资源

    http://www.cs.columbia.edu/~mcollins/notes-spring2013.html http://web.stanford.edu/class/cs224n/syll ...

  2. 3分钟搞定Linux系统正则表达式

    正则表达式是一种字符模式,用于在查找过程中匹配制定的字符. 元字符通常在Linux中分为两类:Shell元字符,由Linux Shell进行解析:正则表达式元字符,由vi/grep/sed/awk等文 ...

  3. Chrome DevTools — Network

    Chrome DevTools — Network https://segmentfault.com/a/1190000008407729 chrome DevTools的各种使用: 在dev too ...

  4. 如何用Client OM获取页面上一个Content web part的内容

    [解决方法] According to Wictor Wilén, The Client Object Model is fairly limited when it comes to working ...

  5. Android 基本 Jackson Marshalling(serialize)/Unmarshalling(deserialize)

    本文内容 基本 Jack Marshalling 忽略属性 忽略 Null 字段 改变字段名字 基本 Jackson Marshalling 把 JSON 解析成 JsonNode Unmarshal ...

  6. FPipe, CMD命令行下的端口重定向工具

    英文文档: FPipe v2. - TCP/UDP port redirector. Copyright (c) by Foundstone, Inc. http://www.foundstone.c ...

  7. ArcGIS鼠标滚轮方向之ArcMap篇

    ArcMap中地图缩放滚轮方向与我们常用的AutoCAD.在线地图等相反.ArcMap默认滚轮向上缩小,向下放大,曾经引起过多位用户的不满.好在ArcMap提供个性化设置. 打开ArcMap菜单栏-自 ...

  8. HTML DOM defaultValue 属性

    定义和用法 defaultValue 属性设置或返回文本框的初始内容. 注释:文本框的初始值是位于 <textarea> 和 </textarea> 标签之间的文本.在表单被重 ...

  9. Selenium2(WebDriver)总结(二)---Firefox的firebug插件参数设置(补充)

    本文是对上一节的补充:http://www.cnblogs.com/puresoul/p/4251536.html 使用Selenium2(webdriver)启动firefox且自动加载firebu ...

  10. 在CentOS7(虚拟机)下通过源码安装Postgresql10以及基本配置

    操作系统:CentOS7 安装文件:postgresql-10.0.tar.gz 系统环境:gcc.Python 1:源码安装 [postgres@localhost ~]# tar zxvf pos ...