你好,游客 登录 注册 搜索
背景:
阅读新闻

Struts2的值栈和OGNL

[日期:2017-03-07] 来源:Linux社区  作者:TigerandRose [字体:大 中 小]

1、值栈是什么?

  值栈是对应每一个请求对象的内存数据中心。每次请求一个action生成一个狭义上的值栈(ValueStack)。这个值栈存储了我们定义在action中的每个属性的值,这些属性必须有get和set方法,以致于我们可以在jsp页面中使用EL表达式。

2、值栈的作用:

  值栈能够在线程安全的情况下提供公共的数据存取服务。当有请求到达时,struts会为每个请求创建一个新的值栈。值栈封装了一次请求所有需要操作的相关的数据。

3、值栈的内容:

  在struts中是有广义和狭义值栈之分的。狭义值栈一般指的是实现com.opensymphony.xwork2.util.ValueStack接口。主要支持EL表达式来存取数据。

  广义值栈指的是ActionContext对象。在ActionContext对象中可以存放参数和值

例如:session、request、application等。

4、值栈的基本使用:

  一般来讲都是先获取ActionContext对象。

ActionContext actionContext=ActionContext.getContext();

  通过ActionContext对象来获取狭义上的值栈。

ValueStack value=actionContext.getValueStack();

  通过set方法设值。

value.set("name", "fangchao(value)");
        value.set("age", 21);

  在前台Hello.jsp中获取值。

获取狭义上的值栈数据:<s:property value="name"/><s:property value="age"/><br>

  当然也需要在struts.xml中设置

<package name="hello" namespace="/" extends="struts-default">
        <action name="hello" class="com.fangchao.action.HelloAction">
            <result name="success">success.jsp</result>
        </action>
    </package>

  也可以在jsp中使用session等,后台代码如下:

Map<String, Object> session=actionContext.getSession();
        session.put("name", "fangchao2(value)");
        session.put("age",23);
        
        
        Map<String, Object> application=actionContext.getApplication();
        application.put("name", "fangchao3");
        application.put("age", 25);

  前台代码:

请求参数:<s:property value="#parameters.name"/><s:property value="#parameters.age"/><br>
    
    request:<s:property value="#request.name"/><s:property value="#request.age"/><br>
    session:<s:property value="#session.name"/><s:property value="#session.age"/><br>
    application:<s:property value="#application.name"/><s:property value="#application.age"/><br>
    attr:<s:property value="#attr.name"/><s:property value="#attr.age"/><br>

推荐阅读:

Struts中异步传送XML和JSON类型的数据 http://www.linuxidc.com/Linux/2013-08/88247.htm

Struts2的入门实例 http://www.linuxidc.com/Linux/2013-05/84618.htm

Struts2学习笔记-Value Stack(值栈)和OGNL表达式  http://www.linuxidc.com/Linux/2015-07/120529.htm 

struts2文件上传(保存为BLOB格式) http://www.linuxidc.com/Linux/2014-06/102905.htm

在Struts2中使用ModelDriven action http://www.linuxidc.com/Linux/2017-03/141203.htm

Struts2实现ModelDriven接口 http://www.linuxidc.com/Linux/2014-04/99466.htm

遇到的Struts2文件下载乱码问题 http://www.linuxidc.com/Linux/2014-03/98990.htm

Struts2数据验证机制  http://www.linuxidc.com/Linux/2016-10/135995.htm

struts2简单示例 http://www.linuxidc.com/Linux/2016-11/137146.htm

Struts2绑定对象数组  http://www.linuxidc.com/Linux/2017-01/139080.htm

Struts2 s:if标签以及 #,%{},%{#}的使用方法  http://www.linuxidc.com/Linux/2016-11/137188.htm

Struts 的详细介绍:请点这里
Struts 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/141472.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款