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() {
            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>
  5. We define the minesweeper struts portlet in the portlet.xml
  6. <portlet-app>
        <portlet id="minesweeper">
            <display-name>Minesweeper Portlet</display-name>
  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. -->
            <!-- The default action to invoke in view mode. -->
  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>
  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"/>
Continue to Part three: Wiring the backend with Spring Framework and Hibernate


No comments:

Post a Comment