Struts and Tiles and fmt:message key values


Web tier: servlets, JSP, Web frameworks: Struts and Tiles and fmt:message key values

  1. Struts and Tiles and fmt:message key values (2 messages)

    I am building a Struts Web application and I'm using Tiles for the first time. I have it all set up and working nicely. My problem comes in the fact that I want to use a standard messages.resources file for my page titles.

    For example

    home.title=Main Home Page
    info.title=Intro Page


    I am used to setting the bundle at the top of the page like:

    <fmt:setBundle basename="messages"/>

    and then directly accessing keys as follows:

    <fmt:message key="home.title"/>

    however I am now using tiles so what I had hoped was to use a tiles attribute to pass the key :

      <definition name=".home" extends=".baseDef">
        <put name="titleKey" value="title.home" />

    and then use the key in the tile like:

    <fmt:message key="${titleKey}"/>

    however, nothing I've tried has worked (including just trying the fmt:message tag w/o using Tiles like :
    <c:set var="titleKey" value="home.title"/>) .

    For some reason, the fmt:message tag refuses to accept exptession values.

    Can anyone offer me a best practice on how to handle this? Or at least solve my problem?
  2. I'm not sure about using the resource file, but if you want to pass the value directly you should try

      <definition name=".home" extends=".baseDef">
        <put name="titleKey" value="Main Home Page" />

    and then

      <tiles:getAsString name="titleKey"/>

    in your jsp
  3. I make temporary solution for the same problem :)
    in layout JSP put that code
               ComponentContext context = ComponentContext.getContext(request);
                   if (context == null) {
                       throw new ServletException("This must be called by a Tile, not directly");
               String title = (String) context.getAttribute("titleKey");
               if(title != null){
           <title><bean:message key="<%=title%>"/></title>
                   <title><bean:message key="index.title"/></title>