Java 不使用JSTL从JSP访问FlowScope
我使用的是SpringWebFlow(v.1.0.5),我有一个JSP页面,它对一个流进行AJAX调用,并需要读取XML结果。该流成功地将对象设置到FlowScope中,然后调用JSP页面来呈现结果。在JSP页面中,我想测试对象是否有属性(比如,.firstName),如果有,请做些什么。我可以使用JSTL表达式语言(通过说${userObj})访问FlowScope中的变量,但这样我就可以把它吐出来了。我已经尝试了下面的方法来理解它,并将其逻辑化,取得了不同的成功 更新:剩下的问题是:如何获取scriptlet()部分中的上下文和流范围?Java 不使用JSTL从JSP访问FlowScope,java,spring,jsp,jstl,Java,Spring,Jsp,Jstl,我使用的是SpringWebFlow(v.1.0.5),我有一个JSP页面,它对一个流进行AJAX调用,并需要读取XML结果。该流成功地将对象设置到FlowScope中,然后调用JSP页面来呈现结果。在JSP页面中,我想测试对象是否有属性(比如,.firstName),如果有,请做些什么。我可以使用JSTL表达式语言(通过说${userObj})访问FlowScope中的变量,但这样我就可以把它吐出来了。我已经尝试了下面的方法来理解它,并将其逻辑化,取得了不同的成功 更新:剩下的问题是:如何获取
<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>
不空
如果您安装了JSTL并以正确的方式声明了taglib,并且将web.xml
声明为至少Servlet 2.4,那么尝试1应该可以工作。另见问题:
要测试对象是否为空,应使用以下构造:
<c:if test="${not empty userObj.firstName}">
或
强烈反对尝试2。Scriptlet是一种糟糕的实践,应该总是被诸如JSTL和EL之类的标记库所取代(就像您在尝试1中所做的那样)。如果由于技术原因不可能,那么编码应该在真正的Java类中完成,(间接地)从Servlet开始
尝试3是可行的,但我建议使用servlet和Javabean-to-XML序列化程序,如。通过这种方式,您可以透明地将JavaBean集合提供给响应的outputstream
<c:if test="${userObj.firstName != null}">