0
votes

If inside a controller I set

model.addAttribute("page-title", "Home");

and I would like to have it on a tiles laytou.jsp like this:

<title><tiles:getAsString name="page-title"/></title>

what should I do?

  1. Write a preparer?

    to the preparer de put-attribute definition doesn't matter so looks like it makes no sense.

  2. Just add a <put-attribute name="page-title" value="${page-title}"/> into definition?

    When I do this I just get "${page-title} - My Website" as output. EL is not been evaluated.

So please, what's the best practice and how to make it work?

tiles-defs.xml:

<tiles-definitions>

  <definition name="baseLayout" template="/WEB-INF/pages/common/layout.jsp">
    <put-attribute name="website-title" value="My Website"/>
    <put-attribute name="page-title" expression="Default Title"/>
    <put-attribute name="header" value="/WEB-INF/pages/common/header.jsp"/>
    <put-attribute name="body" value=""/>
    <put-attribute name="footer" value="/WEB-INF/pages/common/footer.jsp"/>
  </definition>

    <definition name="*" extends="baseLayout">
        <put-attribute name="page-title" value="${page-title}"/>
        <put-attribute name="body" value="/WEB-INF/pages/{1}.jsp"/>
    </definition>

</tiles-definitions>

layout.jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="language" content="English" />  
        <title><tiles:getAsString name="page-title"/> - <tiles:getAsString name="website-title"/></title>
        <link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/style.css" />" media="all"/>
    </head>

    <body>

        <div id="container">

            <!--  header -->
            <tiles:insertAttribute name="header"/>

            <!-- main central container -->
            <tiles:insertAttribute name="body"/>

            <!-- footer -->
            <tiles:insertAttribute name="footer"/>

        </div>

    </body>
</html>

homeController.java:

@Controller
public class HomeController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView home(Locale locale, Model model) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("home");
        model.addAttribute("page-title", "Home");
        return mav;
    }

}

servlet-context.xml:

<beans:bean id="tilesviewResolver" class="org.springframework.web.servlet.view.tiles2.TilesViewResolver" p:order="0"/>
<beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <beans:property name="definitions" value="/WEB-INF/tiles/tiles-defs.xml"/>
</beans:bean>

pom.xml:

<!-- Tiles -->
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-jsp</artifactId>
    <version>2.2.2</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>       

<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-el</artifactId>
    <version>2.2.2</version>
</dependency>
4

4 Answers

2
votes

Just use pure EL without Tiles. Model attributes are placed on request as attributes (HttpServletRequest#getAttribute) under their name. This is accessible via requestScope['page-title'] or just simply by attribute name itself:

<title><c:out value="${page-title}" /></title>
<title>${page-title} - without HTML escaping provided by c:out</title>

This has nothing to do with Tiles. You don't need to take Tiles into account when working with model / request attributes.

1
votes

Pavei is right, in your use case it's not obvious why you need to put the request attribute in as a tiles attribute.

If you really do need it as a tiles attribute then do it like

<put-attribute name="page-title" expression="${page-title}"/>
1
votes

My English is poor. And I haven't use tiles2.x in my project, I use tiles3.x. If you want to use expression language support, first you should enable CompleteAutoloadTilesContainerFactory.

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

then you have to configure defintions like this .

<definitions name="*" extends="baseLayout">
    <put-attribute name="page-title" expression="${page-title}" />
    <put-attribute name="body" value="/WEB-INF/pages/{1}.jsp" />
</definitions>

Tiles Document https://tiles.apache.org/framework/tutorial/advanced/el-support.html Good luck to you .

0
votes

Let's recap:

  1. The EL language is supported since Tiles 2.1 without extra configuration.
  2. If we use Tiles 3.x, we should:
    • Adding this dependency to pom:
<dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-extras</artifactId>
        <version>3.0.5</version>
    </dependency>
  • Enabling CompleteAutoloadTilesContainerFactory:
 <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles/tiles-defs.xml</value>
        </list>
    </property>
   <!--enable CompleteAutoloadTilesContainerFactory -->
    <property name="completeAutoload" value="true"></property>
</bean>