Part two: Making the portlet with Struts2

This is part two of the Minesweeper portlet tutorial.

The portlet is configured with several xml files which can be found in the minesweeper/src/main/webapp/WEB-INF and minesweeper/src/main/resources directories. The following steps try to illustrate how a Struts2 action can be configured to become a Liferay portlet.
  1. Our action class GameAction has a method start for starting a new game
  2. public class GameAction extends ActionSupport {
        private Game game;
        
        public String start() {
            game.init();
            return SUCCESS;
        }
    }
    
  3. We configure action startGame in the struts.xml to call that method and render the result success with a freemarker template
  4. <struts>
        <package name="minesweeper" extends="struts-portlet-default" namespace="/minesweeper">
            <action name="startGame" class="com.blogspot.pragmatastic.minesweeper.GameAction" method="start">
                <result name="success" type="freemarker">/pages/game.ftl</result>
            </action>
        </package>
    </struts>
    
  5. We define the minesweeper struts portlet in the portlet.xml
  6. <portlet-app>
        <portlet id="minesweeper">
            <portlet-name>minesweeper</portlet-name>
            <display-name>Minesweeper Portlet</display-name>
            <portlet-class>org.apache.struts2.portlet.dispatcher.Jsr168Dispatcher</portlet-class>
    
  7. We map the portlet to the action defined in the struts.xml by setting the viewNameSpace and defaultViewAction parameters
  8.         <!-- The view mode namespace. Maps to a namespace in the Struts2 config file. -->
            <init-param>
                <name>viewNamespace</name>
                <value>/minesweeper</value>
            </init-param>
            <!-- The default action to invoke in view mode. -->
            <init-param>
                <name>defaultViewAction</name>
                <value>startGame</value>
            </init-param>
        </portlet>
    </portlet-app>
    
  9. We add Liferay specific configuration for our portlet in the liferay-portlet.xml, in this case we define a portlet specific css file
  10. <liferay-portlet-app>
        <portlet>
            <portlet-name>minesweeper</portlet-name>
            <header-portlet-css>/css/minesweeper.css</header-portlet-css>
        </portlet>
    </liferay-portlet-app>
    
  11. And finally we make it visible in the dock menu "Add application" listing by defining the category for it in the liferay-display.xml file
  12. <display>
        <category name="example">
            <portlet id="minesweeper"/>
        </category>
    </display>
    
Continue to Part three: Wiring the backend with Spring Framework and Hibernate

References

http://struts.apache.org/2.0.14/docs/struts-2-portlet-tutorial.html
http://www.liferay.com/community/wiki/-/wiki/Main/liferay-portlet.xml+%285.2.0%29

No comments:

Post a Comment