Tuesday, March 25, 2014

Starting with Struts 2(Eclipse Kepler and Tomcat 7 embedded)

Here are to configure a small eclipse project to run a small Struts 2 example project....

My current configuration is:
JDK 1.6
Tomcat 7(eclipse embedded configuration)
Eclipse Kepler for JavaEE developer
Struts 2.3.16.1(refer to Struts Project Home Page)
all on my Debian Wheezy.....

1) Download from Struts site the latest(at now the 2.3.16.1 version) version of .zip file available. Unzip it.

2) Under Eclipse create a new Dynamic Web Project.
Choose a custom name(I choose StartingWithStruts), the target runtime(Tomcat 7 previously configurated) and after in the "Dynamic Web Module Version" combo menu choose the 2.5 specific.
Then next, and choose click on the generate web deployment descriptor(web.xml file) radio button.
Then Finish.

3)Copy into WEB-INF/lib folder these libraries that you find under struts_home_resources_folder directory that you created when you downloaded and unzipped the struts zip file.

commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.1.jar
commons-logging-1.1.3.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.16.1.jar
xwork-core-2.3.16.1.jar

Ensure you have into your classpath(right click on the project, Properties, Java Build Path, libraries)  "Web App Libraries" library setted.



4) Add in web.xml the filter StrutsPrepareAndExecuteFilter class in every time a page is called. Update web.xml in this way:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>StartingWithStruts</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>      
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>


5)Create a package com.test within our first class LoginAction
that maps our first jsp page, a login form.
Note the execute method, that is the action called when form button is performed...
Note the extension of ActionSupport class that will allow us to use the getText method(to get properties from resource bundle file)

package com.test;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Class that maps our first jsp page, a simple login form
 * @author Maurizio Franco
 *         http://maurizio-franco.blogspot.it
 *         maurizio.franco@ymail.com
 */
public class LoginAction extends ActionSupport {

    private String username ;
    private String password ;
    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }
    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }
    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }
    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
   
    /*
     * Performs a simple check if username.equals(password)
     * It returns a String that maps the next page where the flow is redirected.
     */
    public String executeLogin () {
        String returnValue = "" ;       
        if (username.equals(password)) {
            returnValue = "loginOK" ;
        } else {
            addActionError(getText("error.login"));
            returnValue = "loginKO" ;
        }
        return returnValue ;
    }

}



 6) Under WebContent I create a two jsp files.
First one is index.jsp file that contains:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
    <head>
        <title>StartingWithStruts</title>
    </head>
   
    <body>
        <h2>StartingWithStruts - Simple Login Form</h2>
        <s:actionerror />
        <s:form action="login.action" method="post">
            <s:textfield name="username" key="label.username" size="20" />
            <s:password name="password" key="label.password" size="20" />
            <s:submit method="executeLogin" key="label.login" align="center" />
        </s:form>
    </body>
</html>

Then loginOK.jsp, that contains a simple page to give the success login message.

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome page...</title>
</head>

<body>
    <h2>User <s:property value="username" /> logged successfully!!!</h2>
</body>
</html>

7) Then we have to add in the classpath two files. The resources bundle file, that I call messages.properties and I add it under src folder
It contains this:

label.username= Username
label.password= Password
label.login= Login
error.login= Invalid Username/Password. Please try again.

Then directly under src folder I create a struts.xml file with this:

<?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>
    <constant name="struts.custom.i18n.resources" value="messages" />

    <package name="default" extends="struts-default" namespace="/">
        <action name="login" method="executeLogin" class="com.test.LoginAction">
            <result name="loginOK">loginOK.jsp</result>
            <result name="loginKO">index.jsp</result>
        </action>
    </package>
</struts>



That's all.
Start the embedded Tomcat and point to http://loaclhost:8080/YOUR_PROJECT_NAME/index.jsp

You can fine this smaill project illustreted by me, under a sourceforge.net branch. Ready for download via SVN.
This is the direct url:
svn://svn.code.sf.net/p/mauriziofranco/code/StartingWithStruts/trunk/StartingWithStruts
or this is command for shell:
svn checkout svn://svn.code.sf.net/p/mauriziofranco/code/StartingWithStruts/trunk/StartingWithStruts  mauriziofranco-code



For other documentation and examples refer to http://struts.apache.org/release/2.2.x/docs/home.html

Bye....

 

6 comments: