Wednesday, December 14, 2011

Struts2 and portlet

This is a short how to get a Struts2 application up and running in JBoss portal framework.

We will use the following tools one at a time in this tutorial.

Tools:

JBoss 4.0.5 with the portal extension.

Maven 2.0.4

Struts 2.0.6

Ubuntu Edgy Eft

Java 5.


Note this. There is a fundamental difference between Struts1 and struts2. The whole concept is changed and Struts has moved
away from the straight flow based approach to a more object oriented way with dependency injection. There are a multitude of sites about this, but the best staring point is probably the Struts homepage.


So before starting. The disclaimer is that in a serious production environment, this application will not be enough. This is just a test for the home cooker.
Step 1.

Install JBoss 4.0.5 and pick the one with version number 4.0.5 and choose Run Installer. That's right, make sure you choose "Run Installer".

Pick installation language.
This should be standard stuff. You go next all the time and accept the license agreement (well, only if you accept it of course). pick installation directory.
For the sake of argument I choose /jbossportlet. Pick your choice and then press next.
let the installation program create the directory. Press ok.
You have to choose protal here.

In your jbossportal/bin directory there are two files of interest.
run.sh and run.conf make a copy of each and name them debug.sh and debug.conf. Edit debug.sh and find the one and only occurence of the word run.conf in the file. Change this to debug.conf.
Now change the debug.sh file and remove the bracket# in fron ot the line where it says something like this,
#JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y"
When youe are done the ine should be like this
JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y"
OK. Test JBoss now go to the installation directory/bin and type ./run.sh this should fire up your JBoss app server.

To see if it works type http://localhost:8080/portal and the JBoss portal page should appear. Do not panic if it takes a while. If you do not see this page
without errors do not bother to proceed, something is major wrong at your place right now.

Maven
Install Maven go to Mavens home page.
I used version 2.0.4. Not that it really matters, but version numbers are important in ceratin circles.
There is a web.xml file in the WEB-INF directory, if you don't do this Maven doesn't build your web app.
Add one pom.xml to the main MyPortlet directory and add one pom.xml to the MyPortlet.war directory.
The content is as follows.
The top pom.xml shall look like this. This is the pom in the MyPortlet directory.

4.0.0
Maven Default Project
com.aja.myportlet
MyPortlet
1.0
pom

MyPortlet.war




org.apache.maven.plugins
maven-compiler-plugin

1.5
1.5




The pom.xml in the MyPortlet.war directory shall look like this.

4.0.0

com.aja.myportlet
MyPortlet
1.0

MyPortlet.war
war


junit
junit
4.1
test


log4j
log4j
provided
1.2.13


javax.servlet
servlet-api
2.5
provided


opensymphony
xwork
2.0.1


commons-logging
commons-logging
1.0.4


commons-attributes
commons-attributes-api
2.1


commons-collections
commons-collections
3.2


freemarker
freemarker
2.3.4


org.apache.struts
struts2-core
2.0.6


asm
asm
1.5.3



Go to the MyPortlet.war directory and exceute this command.
mvn clean install
Eclipse.
I assume you know Eclipse. Otherwise you are in for a long journey. Now make the eclipse project file in maven by
executing this mvn eclipse:eclipse.
Import the project. You must define your Maven repository in the Eclipse class-path setting. The variable is called M2_REPO and you must point to your Maven repository. If you are unsure check out the Maven documentation.
In windows most of the time you'll find the REPO in the Documents folder.
Coding
When we are done the following files will exist in our project.
In the WEB-INF directory
jboss-app.xml
jboss-portlet.xml
MyPortlet-object.xml
portlet.xml
web.xml
I also felt a need to add this tld as well. It can be found in the Struts2.0.6.core jar in the /META-INF directory.
I don't think you need them but I have to verify that.
struts-tags.tld
In the src/main/resources directory
struts.xml
In the WEB-INF/pages/view (Yes you must create that directory)
helloForm.jsp
helloWorld.jsp
And we'll do one Java class and put it in
src/main/java/com/aja/mp/action/HelloWorldAction.java
The content of all files (except tlds) will be shonw below. If you add this content your application will work.
struts.xml









/WEB-INF/pages/view/helloForm.jsp


/WEB-INF/pages/view/helloForm.jsp


/WEB-INF/pages/view/helloWorld.jsp



jboss-app.xml


MyPortlet

jboss-portlet.xml





MyPortlet-object.xml


portlet.xml




My very first struts Portlet
MyPortlet
My first WebWork Portlet

org.apache.struts2.portlet.dispatcher.Jsr168Dispatcher



viewNamespace
/view



defaultViewAction
index


0


text/html


en


My very own WebWork Portlet
WWPortlet
struts,portlet


web.xml






struts

org.apache.struts2.dispatcher.FilterDispatcher




struts
/*




org.apache.struts2.portlet.context.ServletContextHolderListener





preparator

org.apache.struts2.portlet.context.PreparatorServlet




/struts-tags
/WEB-INF/struts-tags.tld


helloForm.jsp

<%@ taglib uri="/struts-tags" prefix="ww" %>

Hi there! Please enter your name







helloWorld.jsp

<%@ taglib uri="/struts-tags" prefix="ww" %>

Hello



">Back to form

HelloWorldAction.java

package com.aja.mp.action;

import java.util.Collection;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 2452536171755317259L;
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return super.execute();
}
@Override
public Collection getActionMessages() {
// TODO Auto-generated method stub
return super.getActionMessages();
}
}

We are done, build and deploy the application.
mvn clean install
Rename and copy war to /jbossportal/server/default/deploy/MyPortlet.war
You will most probably get two errors at start up time, you can ignore those if it one complaining about a dev property and another complaining about i18.

0 comments:

Post a Comment