跳至正文
i@laoer.com

技术、生活、感悟 -- Laoer的博客

i@laoer.com

技术、生活、感悟 -- Laoer的博客

  • 首页
  • 人工智能
  • 互联网
  • 商业观察
  • 天乙软件
  • 用药有数
  • 隐私政策
  • 关于
  • X
  • 首页
  • 人工智能
  • 互联网
  • 商业观察
  • 天乙软件
  • 用药有数
  • 隐私政策
  • 关于
  • X
关

搜索

  • 首页
  • 人工智能
  • 互联网
  • 商业观察
  • 天乙软件
  • 用药有数
  • 隐私政策
  • 关于
  • X
i@laoer.com

技术、生活、感悟 -- Laoer的博客

i@laoer.com

技术、生活、感悟 -- Laoer的博客

  • 首页
  • 人工智能
  • 互联网
  • 商业观察
  • 天乙软件
  • 用药有数
  • 隐私政策
  • 关于
  • X
  • 首页
  • 人工智能
  • 互联网
  • 商业观察
  • 天乙软件
  • 用药有数
  • 隐私政策
  • 关于
  • X
关

搜索

  • 首页
  • 人工智能
  • 互联网
  • 商业观察
  • 天乙软件
  • 用药有数
  • 隐私政策
  • 关于
  • X
Java

关于Struts2里使用EL或JSTL

作者 Laoer
2009年2月16日 3 分钟阅读
0

最近在公司里做一些Java Web开发的培训,同时对已经做的一些工程做一些ReView,现在的工程里,工程师直接使用JSTL取得Action里的属性,这个用法我以前到真的没有用过,因为在我印象中,Struts2的这些Action属性,应该是在ValueStack中,而在某些情况下,从ValueStack取值是件挺麻烦的事情,在做天乙社区8时,我就参考Struts2的标记库,自己扩展标记库,从而取得ValueStack里的值,而JSTL应该是从Page、Request、Session和Application里顺序取值,莫非Struts2将ValueStack里的值也放入了Request?同时我们直接用EL标签也直接取出了Action的属性值,莫非真的放入了Request?但是打开Struts2的Debug,发现Request里并没有值。

带着这个疑问,我Google了一下,很快找到答案:

提问:在Struts2中,如何使用JSTL来读取Action中的变量?

这是一个历史悠久的问题。因为事实上,很多朋友(包括我在内)是不使用Struts2自身的标签库,而是使用JSTL的,可能因为JSTL标签库比较少,简单易用的原因吧。

我们知道,JSTL默认是从page,request,session,application这四个Scope逐次查找相应的EL表达式所对应 的对象的值。那么如果要使用JSTL来读取Action中的变量,就需要把Action中的变量,放到request域中才行。所以,早在 Webwork2.1.X的年代,我们会编写一个拦截器来做这个事情的。大致的原理是:在Action执行完返回之前,依次读取Action中的所有的变 量,并依次调用request.setAttribute()来进行设置。具体的整合方式,请参考以下这篇文档:http://wiki.opensymphony.com/display/WW/Using+WebWork+and+XWork+with+JSP+2.0+and+JSTL+1.1

不过随着时代的发展,上面的这种方式,已经不再被推荐使用了。(虽然如此,我们依然可以学习它的一个解决问题的思路)目前来说,自从 Webwork2.2以后,包括Struts2,都使用另外一种整合方式:对HttpServletRequest进行装饰。让我们来看一下源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 public class StrutsRequestWrapper extends HttpServletRequestWrapper {
 
     /**
      * The constructor
      * @param req The request
      */
     public StrutsRequestWrapper(HttpServletRequest req) {
         super(req);
     }
 
     /**
      * Gets the object, looking in the value stack if not found
      *
      * @param s The attribute key
      */
     public Object getAttribute(String s) {
         if (s != null && s.startsWith("javax.servlet")) {
             // don't bother with the standard javax.servlet attributes, we can short-circuit this
             // see WW-953 and the forums post linked in that issue for more info
             return super.getAttribute(s);
         }
 
         ActionContext ctx = ActionContext.getContext();
         Object attribute = super.getAttribute(s);
 
         boolean alreadyIn = false;
         Boolean b = (Boolean) ctx.get("__requestWrapper.getAttribute");
         if (b != null) {
             alreadyIn = b.booleanValue();
         }
 
         // note: we don't let # come through or else a request for
         // #attr.foo or #request.foo could cause an endless loop
         if (!alreadyIn && attribute == null && s.indexOf("#") == -1) {
             try {
                 // If not found, then try the ValueStack
                 ctx.put("__requestWrapper.getAttribute", Boolean.TRUE);
                 ValueStack stack = ctx.getValueStack();
                 if (stack != null) {
                     attribute = stack.findValue(s);
                 }
             } finally {
                 ctx.put("__requestWrapper.getAttribute", Boolean.FALSE);
             }
         }
         return attribute;
    }
}

public class StrutsRequestWrapper extends HttpServletRequestWrapper { /** * The constructor * @param req The request */ public StrutsRequestWrapper(HttpServletRequest req) { super(req); } /** * Gets the object, looking in the value stack if not found * * @param s The attribute key */ public Object getAttribute(String s) { if (s != null && s.startsWith("javax.servlet")) { // don't bother with the standard javax.servlet attributes, we can short-circuit this // see WW-953 and the forums post linked in that issue for more info return super.getAttribute(s); } ActionContext ctx = ActionContext.getContext(); Object attribute = super.getAttribute(s); boolean alreadyIn = false; Boolean b = (Boolean) ctx.get("__requestWrapper.getAttribute"); if (b != null) { alreadyIn = b.booleanValue(); } // note: we don't let # come through or else a request for // #attr.foo or #request.foo could cause an endless loop if (!alreadyIn && attribute == null && s.indexOf("#") == -1) { try { // If not found, then try the ValueStack ctx.put("__requestWrapper.getAttribute", Boolean.TRUE); ValueStack stack = ctx.getValueStack(); if (stack != null) { attribute = stack.findValue(s); } } finally { ctx.put("__requestWrapper.getAttribute", Boolean.FALSE); } } return attribute; } }

看到了嘛?这个类会在Struts2初始化的时候,替换HttpServletRequest,运行于整个Struts2的运行过程中,当我们试 图调用request.getAttribute()的时候,就会执行上面的这个方法。(这是一个典型的装饰器模式)在执行上面的方法时,会首先调用 HttpServletRequest中原本的request.getAttribute(),如果没有找到,它会继续到ValueStack中去查找, 而action在ValueStack中,所以action中的变量通过OGNL表达式,就能找到对应的值了。

在这里,在el表达式广泛使用的今天,JSTL1.1以后,也支持直接使用el表达式。注意与直接使用struts2的tag的区别,这里需要使用el的表示符号:${}

例如:${user.name}, <c:out value=”${department.name}” />

原来是这样,学无止境啊,很多东西需要仔细去研究。

标签:

JavaStruts
作者

Laoer

1999年开始做互联网,做过开发、技术管理,也创过几次业。 现在依然在一线做产品、写代码、折腾AI。 关注AI如何重新改变软件、公司和人的工作方式。 公众号:Laoer杂谈。

关注我
其他文章
上一个

PHP框架选择

下一个

Redhat Linux下src rpm的安装方法

暂无评论!成为第一个。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。

微信订阅

欢迎订阅微信公众号--“Laoer杂谈”,搜索微信号:laoertalk,“Laoer杂谈”是关于技术、创业、生活的杂谈与随笔,希望与大家多多交流。

用药有数·DoseLoop

用药有数·DoseLoop 一款不会响完就消失的用药提醒。每次计划用药持续追踪,直到有明确结果。支持慢病、老人、ADHD 等各种用药场景。

标签

AI AI Agent AI Coding Anthropic ChatGPT Claude Claude Code Codex DeepMind DeepSeek DeepSeek Harness Gemini Google GPT iOS iPhone Java JavaScript jQuery Kimi Kohana Linux LLM Mac Memcached Meta Mysql Nginx OpenAI Oracle PHP RSA Sam Altman SNS Struts Tomcat Web2.0 互联网 人工智能 创业 大模型 工作 旅游 生活 管理

日历

2026 年 11 月
日 一 二 三 四 五 六
1234567
891011121314
15161718192021
22232425262728
2930  
« 10 月    

近期评论

  • AI Coding 文档管理:写代码越来越快,文档却更重要了 发表在《AI写代码已经不稀奇了,真正的变化是很多东西突然值得做了》
  • 大模型商业化:中国AI模型越来越强,为什么反而更难赚钱? 发表在《DeepSeek V4涨价后,真正的问题不是谁在用,而是谁在赚钱》
  • Laoer 发表在《AI 有没有意识还没定论,Anthropic 为什么先限制虐待它?》
  • 爱生活 发表在《AI 有没有意识还没定论,Anthropic 为什么先限制虐待它?》
  • 爱生活 发表在《AI 有没有意识还没定论,Anthropic 为什么先限制虐待它?》
  • AI数据分析如何改变BI工作流?15分钟完成一份运营报告 发表在《我把DeepSeek Harness接进生产环境后,发现AI运维真正改变的不是查日志》
  • 99%的代码都由AI写了,那程序员真正值钱的是什么? – i@laoer.com 发表在《我把DeepSeek Harness接进生产环境后,发现AI运维真正改变的不是查日志》
  • 爱生活 发表在《你的一分钟语音,可能是别人的十分钟》
  • 爱生活 发表在《Bill Gates为什么突然不相信“AI最终会创造更多工作”了?》
  • DeepSeek Harness来了,国产AI Coding终于开始补最短的那块板 – i@laoer.com 发表在《这次真不慌了,Claude随时可以取消订阅》

分类

  • iOS
  • Java
  • JavaScript
  • Linux
  • Mac
  • PHP
  • Web2.0
  • 互联网
  • 人工智能
  • 创业
  • 博客文章
  • 商业观察
  • 工作
  • 数据库
  • 构架设计
  • 生活

归档

  • 2026 年 11 月
  • 2026 年 10 月
  • 2026 年 9 月
  • 2026 年 8 月
  • 2026 年 7 月
  • 2026 年 6 月
  • 2026 年 1 月
  • 2024 年 12 月
  • 2024 年 10 月
  • 2024 年 8 月
  • 2024 年 7 月
  • 2023 年 12 月
  • 2023 年 1 月
  • 2022 年 8 月
  • 2021 年 12 月
  • 2021 年 10 月
  • 2020 年 12 月
  • 2020 年 1 月
  • 2018 年 12 月
  • 2018 年 8 月
  • 2018 年 4 月
  • 2017 年 12 月
  • 2016 年 12 月
  • 2016 年 8 月
  • 2016 年 2 月
  • 2015 年 12 月
  • 2015 年 1 月
  • 2013 年 6 月
  • 2013 年 5 月
  • 2012 年 11 月
  • 2012 年 7 月
  • 2012 年 6 月
  • 2012 年 5 月
  • 2012 年 3 月
  • 2012 年 1 月
  • 2011 年 12 月
  • 2011 年 11 月
  • 2011 年 10 月
  • 2011 年 9 月
  • 2011 年 8 月
  • 2010 年 11 月
  • 2010 年 2 月
  • 2010 年 1 月
  • 2009 年 12 月
  • 2009 年 11 月
  • 2009 年 9 月
  • 2009 年 7 月
  • 2009 年 6 月
  • 2009 年 5 月
  • 2009 年 4 月
  • 2009 年 3 月
  • 2009 年 2 月

其他操作

  • 登录
  • 条目 feed
  • 评论 feed
  • WordPress.org
Copyright 2026 — i@laoer.com. All rights reserved. Blogsy WordPress Theme