【问题标题】:Strange OGNL behaviour when using #this in s:select在 s:select 中使用 #this 时出现奇怪的 OGNL 行为
【发布时间】:2012-05-06 22:54:11
【问题描述】:

考虑以下情况,其中 availableExclusions 是 Date 对象的数组。

<s:select listValue="(new java.text.SimpleDateFormat('MM/dd/yyyy')).format(new java.util.Date(#this[0].time))" size="25" id="inputExclusionDates" name="available" list="availableExclusions" multiple="true" cssClass="multi-select" />

我创建上述内容是为了回答这个问题How would I format a list of dates within a struts 2 select tag?(我还建议其他解决方案可能是更好的方法)。不管使用这么长的 OGNL 表达式是否是一个好主意,我看不出有必要:new java.util.Date(#this[0].time) 尽管出于沮丧而创建了该片段,但令我惊讶的是,它在看似等效的#this[0] 没有工作的地方工作(没有输出)。

如果我们用 listValue="#this[0].class" 替换,我们会看到我们期望的结果:class java.util.Date

创建一个新的 Date 的行为符合预期,调用 Date 的已弃用属性(例如 listValue="#this[0].day")会产生预期的输出。

为什么我必须在这个 select 语句中使用冗余表达式 new java.util.Date(#this[0].time) 而不是 #this[0]

使用 Struts 2.3.1.2

【问题讨论】:

    标签: java struts2 ognl


    【解决方案1】:

    因为#this[0] 会自动转换为字符串。如果您将日志级别转为调试,您会看到一条通常被抑制的异常消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多