0

I am using Spring 3.2 and tiles 3.0.

I want to set the value of an attribute in a tiles definition from a spring bean property like this

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

msgs is HashMap and it is defined in spring application context

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

and this is spring-tiles config

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

and this the template 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>

when i tried to run the application i got the following exception

 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

except this issue every thing is OK.

Is there any way to make spring beans accessible to tiles expressions?

aomar
  • 500
  • 1
  • 6
  • 23

1 Answers1

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"/>
Community
  • 1
  • 1
aomar
  • 500
  • 1
  • 6
  • 23