0

我正在使用Spring 3.2tiles 3.0

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

<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>

这是弹簧瓷砖配置

<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?

4

1 回答 1

0

The problem is divided to couple of small problems:

1 - The first one is to enable tiles to access spring beans and this can be done by exposing spring context beans to Tiles view - check this provided by mck.

2 - The second one is how to render the attribute in the JSP, using tiles:getAsString tag with an put-attribute have no value will throw NullPointerException as the tiles:getAsString tag uses the simple toString() on the value provided in the definition to render itself and it completely will ignore the expression attribute, instead of getAsString use insertAttributewhich can evaluate the 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-06-07T14:34:01.957 回答