You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Struts 2 Portlet Tutorial - Creating a simple Bookmark Portlet

Work in progress

Using version 2.1.1-SNAPSHOT of the portlet plugin

Note that this tutorial assumes that you're familiar with basic Struts 2 web application programming.

If you have not used Struts 2 before, please check out some of the other Struts 2 tutorials first.

Preparations

In this tutorial we will use eclipse as our IDE. If you do not have Eclipse, you can download it from http://www.eclipse.org.

The project itself will be set up using Maven 2. Maven 2 is available from http://maven.apache.org.

If you have not used the maven-eclipse-plugin before, you need to set up the Eclipse workspace with a variable that points to the Maven 2 repository. To do this, type

mvn -Declipse.workspace=<path-to-eclipse-workspace> eclipse:add-maven-repo

Creating the project

We'll use Maven 2 with the Struts 2 Portlet Archetype to create a skeleton project for our portlet application. From the command line, issue the command:

mvn archetype:create -DarchetypeGroupId=org.apache.struts -DarchetypeArtifactId=struts2-archetype-portlet -DarchetypeVersion=2.1.1-SNAPSHOT -DartifactId=bookmark-portlet -DgroupId=com.mycompany

This will set up the maven 2 structure for us and also set up the basic configuration needed to create a Struts 2 portlet. The archetype creates a sample HelloWorld portlet that shows off some of the basic principles of Struts 2 portlet programming. To test the set up, type

mvn jetty:run -P pluto-embedded

in a command prompt. Open a browser and point your browser to http://localhost:8080/bookmark-portlet/pluto/index.jsp and play around.

Looking at the basics

To see how the basic HelloWorld example works, let's look at some of the configuration files:

struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
   <package name="default" extends="struts-portlet-default" namespace="/view">
	   <action name="index" class="com.mycompany.HelloAction">
		   <result>/WEB-INF/jsp/view/index.jsp</result>
	   </action>
   </package>
	
	<package name="edit" extends="struts-portlet-default" namespace="/edit">
	
		<action name="index" class="com.mycompany.UpdateNameAction">
			<result type="redirectAction">
				<param name="actionName">index</param>
				<param name="portletMode">view</param>
			</result>
			<result name="input">/WEB-INF/jsp/edit/index.jsp</result>
		</action>
	</package>
</struts>

Import the project into Eclipse

Now let's import the project into Eclipse. First, type

mvn eclipse:eclipse -P pluto-embedded

Then start Eclipse (if you have not already done so), and import the project using "File -> Import -> General -> Existing Projects into Workspace". Browse to the folder where you created the project and press finish. Your portlet project should now be setup up with all dependencies in place.

  • No labels