【问题标题】:Accessing Spring 3 beans in tiles 3 expressions in definition file访问定义文件中的 tile 3 表达式中的 Spring 3 bean
【发布时间】:2013-05-31 15:20:42
【问题描述】:

我正在使用 Spring 3.2tiles 3.0

我想从这样的 spring bean 属性中设置图块定义中的属性值

<put-attribute name="headTitle" expression="${msgs['serviceGroups.title']}" />

msgs 是 HashMap,它是在 Spring 应用上下文中定义的

<bean id="msgs" class="qa.gov.moi.eservices.web.util.MessageSourceMapAdapter">
    <constructor-arg name="messageSource">
        <ref bean="messageSource"/>
    </constructor-arg>
</bean>

这是 spring-tiles 配置

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles-defs.xml</value>
        </list>
    </property>
</bean>

这是模板 default.jsp

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title><tiles:getAsString name="headTitle"/></title>


</head>
<body>
    <div id="wrapper">
        <div id="container">
            <div id="top_header">
                <tiles:insertAttribute name="heading" />
                <tiles:insertAttribute name="menuTab" />
                <tiles:insertAttribute name="genralizationTab" />
            </div><!-- top_header -->
            <tiles:insertAttribute name="content" />
            <tiles:insertAttribute name="footer" />
        </div><!-- container -->
    </div><!-- wrapper -->
</body>

当我尝试运行应用程序时,出现以下异常

 Uncaught exception created in one of the service methods of the servlet /WEB-INF/jsp/layouts/default.jsp in application eservices. Exception created : java.lang.NullPointerException

除了这个问题,一切正常。

有什么方法可以让图块表达式可以访问 spring bean?

【问题讨论】:

    标签: spring tiles


    【解决方案1】:

    问题分为几个小问题:

    1 - 第一个是启用 tiles 访问 spring bean,这可以通过将 spring 上下文 bean 暴露给 Tiles 视图来完成 - check this 由 mck 提供。

    2 - 第二个是如何渲染JSP 中的属性,使用tiles:getAsString 标记和put-attribute 没有值将抛出NullPointerException,因为tiles:getAsString 标记使用简单的toString()在定义中提供的值上呈现自身,它将完全忽略expression 属性,而不是getAsString 使用insertAttribute,它可以评估expression

    <!--this works fine with expressions-->
    <tiles:insertAttribute name="headTitle" ignore="true" />
    
    <!-- and this will throw NullPointerException if value is not provided-->
    <tiles:getAsString name="headTitle" ignore="true"/>
    

    【讨论】:

      猜你喜欢
      • 2013-02-06
      • 1970-01-01
      • 2013-10-26
      • 2012-05-26
      • 1970-01-01
      • 2014-07-24
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      相关资源
      最近更新 更多