【问题标题】:Access FlowScope from JSP without JSTL从没有 JSTL 的 JSP 访问 FlowScope
【发布时间】:2010-04-07 15:45:16
【问题描述】:

我正在使用 Spring Web Flow (v. 1.0.5),并且我有一个 JSP 页面,它对流进行 AJAX 调用并需要读取 XML 结果。该流程成功地将一个对象设置到 FlowScope 中,然后调用 JSP 页面来呈现结果。在 JSP 页面中,我想测试该对象是否具有属性(例如,.firstName),如果有,请执行一些操作。我可以使用 JSTL 表达式语言(通过说 ${userObj})访问 FlowScope 中的变量,但这只是让我把它吐出来。我已经尝试了以下方法来解决它并围绕它进行逻辑处理,并取得了不同程度的成功。

更新:剩下的问题是:如何在 scriptlet () 部分获取上下文和流范围?

<rootnode>

<!--  Attempt 1:  c:if isn't being interpreted (though ${userObj.firstName} is),
      it's just displaying the tags on the page. -->
<!--  Update, I didn't have the <%@ taglib directive for jstl/core.  
      Now I do, and they're being interpreted, but it says 
         "According to TLD or attribute directive in tag file,
          attribute test does not accept any expressions"
      How can the if/@test not accept expressions?  Isn't that its whole purpose
      in life? -->
<!--  Update 2, I also forgot the taglib for the rt language, which is separate,
      I guess (jstl/core_rt).  <c:if test now works properly. -->
<c:if test="${!empty(userObj.firstName)}">
<test>not empty</test>
</c:if>

<%
/* Attempt 2:  This throws an error, can't resolve "context".  How do I get the context? */
if (context.getFlowScope().userObj != null) {
    out.write("not null");
} else {
    out.write("not not null");
}
%>

<!--  Attempt 3:  This works, I get the reference to the Object and it
      spits out the correct firstName, gives me no control other than
      spitting out the value. -->
<userReference>${userObj}</userReference>
<userReference_firstName>${userObj.firstName}</userReference_firstName>

</rootnode>

【问题讨论】:

    标签: java spring jsp jstl


    【解决方案1】:

    如果您安装了 JSTL 并以正确的方式声明了 taglib 并将 web.xml 声明为至少 Servlet 2.4,则尝试 1 应该可以工作。另请参阅问题:

    要测试一个对象是否为空,您应该使用以下构造:

    <c:if test="${not empty userObj.firstName}">
    

    <c:if test="${userObj.firstName != null}">
    

    强烈建议不要尝试 2。 Scriptlet 是一种糟糕的做法,应始终由 JSTL 和 EL 之类的标记库替换(正如您在尝试 1 中所做的那样)。如果由于技术原因无法实现,那么编码应该在一个真正的 Java 类中完成,(间接地)从一个 Servlet 开始。

    尝试 3 是可行的,但我建议使用带有 Javabean-to-XML 序列化程序的 servlet,例如 XStream。这样您就可以透明地将一组 Javabeans 提供给响应的输出流。

    【讨论】:

      猜你喜欢
      • 2014-08-25
      • 1970-01-01
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-06
      • 2011-11-08
      • 2012-10-15
      相关资源
      最近更新 更多