0

<spring:eval>在 JSP 中使用标签:

<spring:eval expression="'today is ' + today" />

todayjava.util.Date在哪里。一切正常,只要今天变量不为空。什么时候today为空,我得到

org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property 'today' can be found on null
    org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:243)
    org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:112)
    org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:107)
    org.springframework.expression.spel.ast.OpPlus.getValueInternal(OpPlus.java:70)
    org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:102)
    org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:98)
    org.springframework.web.servlet.tags.EvalTag.doEndTag(EvalTag.java:126)
    org.apache.jsp.WEB_002dINF.views.admin.inbox_jsp._jspx_meth_s_005feval_005f1(inbox_jsp.java:195)
    org.apache.jsp.WEB_002dINF.views.admin.inbox_jsp._jspService(inbox_jsp.java:120)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954)
    org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:684)
    org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:678)
    org.apache.tiles.request.jsp.JspRequest.doInclude(JspRequest.java:123)
    org.apache.tiles.request.AbstractViewRequest.dispatch(AbstractViewRequest.java:47)
    org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:45)
    org.apache.tiles.request.render.ChainedDelegateRenderer.render(ChainedDelegateRenderer.java:68)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259)
    org.apache.tiles.template.InsertAttributeModel.renderAttribute(InsertAttributeModel.java:188)
    org.apache.tiles.template.InsertAttributeModel.execute(InsertAttributeModel.java:132)
    org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:299)
    org.apache.jsp.WEB_002dINF.views.common.main_002dlayout_jsp._jspx_meth_tiles_005finsertAttribute_005f2(main_002dlayout_jsp.java:243)
    org.apache.jsp.WEB_002dINF.views.common.main_002dlayout_jsp._jspService(main_002dlayout_jsp.java:103)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tiles.request.servlet.ServletRequest.forward(ServletRequest.java:265)
    org.apache.tiles.request.servlet.ServletRequest.doForward(ServletRequest.java:228)
    org.apache.tiles.request.AbstractClientRequest.dispatch(AbstractClientRequest.java:57)
    org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:45)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:397)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:238)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:221)
    org.apache.tiles.renderer.DefinitionRenderer.render(DefinitionRenderer.java:59)
    org.springframework.web.servlet.view.tiles3.TilesView.renderMergedOutputModel(TilesView.java:145)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:152)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)

所以我尝试使用 Elvis 运算符检查 null:

<spring:eval expression="'today is ' + (today ?: 'null date!') " />

但这给出了完全相同的表达式。使用调试器进入 Spring PropertyOrFieldReference 代码后,我发现它似乎无法处理空变量。

普通的旧 JSTL 代码通过不打印任何内容来处理 null,这很好:

today is ${today}

today如果是 bean 的属性,它在 SPeL 中也可以正常工作:

<spring:eval expression="'today is ' + myBean.today" />

这会按预期产生字符串“今天为空”。在这个简单的例子中,当变量为 null 时,我可以使用 JSTL 围绕我的<spring:eval>标签进行分支,但我真正需要做的是使用<spring:eval>执行一个具有多个变量的静态函数,其中任何一个变量都可以合法地为 null . 问题是,如果变量为空,SPeL 似乎无法正确解析变量。

4

1 回答 1

1

EL没问题。范围不明确。Spring甚至找不到变量。

将此用于请求范围:

<spring:eval expression="'today is ' + requestScope['today']" />

将此用于会话范围:

<spring:eval expression="'today is ' + sessionScope['today']" />
于 2013-04-30T05:41:53.173 回答