0

Found some very good examples online that integrate Struts 2 with Tiles 2. I noticed that ALL of them map actions using wildcard method from a single action class. Is there a reason for doing so? My application doesn't work when I map actions individually to multiple classes. In the code below, lookUpAll action was added by me. Rest of the code is from the example I am trying to follow.

Struts.xml:

<struts>
<package name="default" extends="struts-default">
    <result-types>
        <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
    </result-types>

    <action name="*Link" method="{1}" class="action.LinkAction">
        <result name="welcome" type="tiles">welcome</result>
        <result name="friends" type="tiles">view</result>
        <result name="office" type="tiles">office</result>
    </action>

    <action name="lookUpAll" class="action.LookupAll">
        <result name="success" type="tiles">view</result>
        <result name="error" type="tiles">lookFail</result>
    </action>
</package>
</struts>

Tiles.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>

  <definition name="baseLayout" template="/baseLayout.jsp">
      <put-attribute name="title"  value="Template"/>
      <put-attribute name="header" value="/header.jsp"/>
      <put-attribute name="menu"   value="/menu.jsp"/>
      <put-attribute name="body"   value="/body.jsp"/>
      <put-attribute name="footer"   value="/footer.jsp"/>
  </definition>

  <definition name="welcome" extends="baseLayout">
      <put-attribute name="title"  value="Welcome"/>
      <put-attribute name="body"   value="/welcome.jsp"/>      
  </definition>

  <definition name="view" extends="baseLayout">
      <put-attribute name="title"  value="View"/>
      <put-attribute name="body"   value="/DispSchedule.jsp"/>      
  </definition>

  <definition name="lookFail" extends="baseLayout">
      <put-attribute name="title"  value="LookFail"/>
      <put-attribute name="body"   value="/lookUpFail.jsp"/>      
  </definition>

  <definition name="friends" extends="baseLayout">
      <put-attribute name="title"  value="Friends"/>
      <put-attribute name="body"   value="/friends.jsp"/>      
  </definition>

  <definition name="office" extends="baseLayout">
      <put-attribute name="title"  value="Office"/>
      <put-attribute name="body"   value="/office.jsp"/>      
  </definition>

</tiles-definitions>
Roman C
  • 49,761
  • 33
  • 66
  • 176
user1828016
  • 29
  • 1
  • 6
  • What's the actual question? The reason people use wildcard mappings is to avoid configuring very similar actions manually. – Dave Newton Jan 15 '13 at 16:43
  • Sorry, my actual question was if using wildcard for tiles is REQUIRED because it was a little odd to see ALL samples use that. Every single example mapped actions to single action class. – user1828016 Jan 15 '13 at 19:50
  • Are you saying that this configuration is not working for you? – Aleksandr M Jan 15 '13 at 21:46
  • Correct. I just added my Tiles.xml code as well. My action class returns the desired string("success"). And I see "http://localhost:8080/AutoUtil/lookUpAll.action" as the URL but it displays menu.jsp in Body instead of DispSchedule.jsp as specified in tile definition. – user1828016 Jan 15 '13 at 22:18
  • What version of Struts 2? (And isn't that normally information that would be obvious to provide?) – Dave Newton Jan 15 '13 at 22:20
  • Why bother re-defining the `"tiles"` result instead of just extending the Tiles plugin package? Also, you need to include your web.xml to show how you've configured the Tiles listener. – Dave Newton Jan 15 '13 at 22:27
  • Please narrow your example to a minimally-failing example and include the JSPs in question. I don't know how to duplicate your edited question's problem. – Dave Newton Jan 15 '13 at 22:42
  • Consider creating an SSCCE and putting it on Github or similar so it's easier to help. – Dave Newton Jan 15 '13 at 23:00

1 Answers1

1

Using a wildcard is not required, but is convenient when the app uses Single (or Multiple) Action, Multiple Methods to handle tightly-coupled functionality.

Particularly for small apps, and demos, it's a way to minimize XML configuration.

These days it might be more typical to remove most of the XML configuration and configure actions/methods individually using annotations.


Edit to reflect brand-new question.

I cannot duplicate the issue. Taking a minimal S2 app with a single line in menu.jsp and header.jsp and a stripped-down baseLayout.jsp results in the following:

enter image description here

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • I had to step off from this project for a little while. Not sure what exactly was wrong but I started from scratch again - struts.xml and tiles.xml look the same but it kinda works now. It does display the "DispSchedule.jsp" and the contents inside it, but displays it below the main window. Not inside the "body" because the content doesn't fit there. Is there a way to make the "body" segment such that a scroll bar is added when the content doesn't fit? – user1828016 Mar 13 '13 at 18:52
  • @user1828016 Sure; Google for "scrollable div". – Dave Newton Mar 13 '13 at 19:28