Creating an application using Struts2

Step 1. Open Eclipse and Goto New >> Dynamic Web Project

New >> Dynamic Web Project

New >> Dynamic Web Project

Step 2. Provide “StrutsApplication” as Project name and select “2.4” as Dynamic web module version and click Next

App Name and Dynamic web module version

App Name and Dynamic web module version

Step 3. Click Next

Next

Next

Step 4. Click Next

Next

Next

Step 5. Click Finish

After Finish

After Finish

Step 6.  Copy all the required JAR files in WebContent >> WEB-INF >> lib folder. Create this folder if it does not exists.

Copy JARs in WebContent >> WEB-INF >> lib

Copy JARs in WebContent >> WEB-INF >> lib

Step 7. In WebContent/WEB-INF/web.xml copy and paste the below code.

—————web.xml———————————-



StrutsApplication
Struts2 Application

struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter


struts2
/*


index.html
index.htm
index.jsp
default.html
default.htm
default.jsp

————————————————————–

The above code in web.xml will map Struts2 filter with url ‘/*’.

The default url mapping for struts2 application will be /*.action. Also note that we have defined Login.jsp as welcome file.

Step 8. Create a package com.example in the src folder and create a class LoginAction.java in this package. Copy and paste the below code in the class LoginAction.java

 

Create Java Class

Create Java Class

——————————LoginAction.java—————-

package com.example;

public class LoginAction
{
private String username;
private String password;

public String execute()
{
if (this.username.equals("admin")&& this.password.equals("admin123"))
{
return "success";
}
else
{
return "error";
}
}

public String getUsername()
{
return username;
}

public void setUsername(String username)
{
this.username = username;
}

public String getPassword()
{
return password;
}

public void setPassword(String password)
{
this.password = password;
}
}

The ResourceBundle
ResourceBundle is very useful Java entity that helps in putting the static content away from the source file. Most of the application define a resource bundle file such as ApplicationResources.properties file which contains static messages such as Username or Password and include this with the application.
ResourceBundle comes handy when we want to add Internationalization (I18N) support to an application.
 
 
Step 9. Click on root project folder “StrutsApplication” and Goto New >> Source Folder, Provide the Folder Name “resources” and click Finish.

 

New >> Source Folder

New >> Source Folder

 

resources folder

resources folder

 

Step 10. Create ApplicationResource.properties file under resources folder (create in the previous step).

 

ApplicationResource.properties

ApplicationResource.properties

Step 11. Copy and paste the below content in ApplicationResource.properties file.
label.username=Username
label.password=Password
label.login=Login

The JSPs

We will create two jsp files
Login.jsp : This will be the entry point of our struts application which will contain two text boxes username and passowr and a submit button
Welcome.jsp :  This will display the welcome message to the use on successful login into the application
 

Step 12. Create to jsps in the WebContent folder with name Login.jsp and Welcome.jsp and Copy the following content in the respective jsps. 

 

JSP location

JSP location

—————————Login.jsp——————————–

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


Struts 2 - Login Application

Struts 2 - Login Application








—————————–Welcome.jsp——————————————

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


Welcome

Hello, !


 

The struts.xml File
Struts2 reads the configuration and class definition from an xml file called struts.xml. This file is loaded from the classpath of the project. We will define struts.xml file in the resources folder. Create file struts.xml in resources folder.
 
Struts.xml

Struts.xml

Step 13. Copy and paste the following content in struts.xml file

—————–struts.xml————————————–






Welcome.jsp
Login.jsp

Note that in above configuration file, we have defined Login action of our application. Two result paths are mapped with LoginAction depending on the outcome of execute() method. If execute() method returns success, user will be redirected to Welcome.jsp else to Login.jsp.
Also note that a constant is specified with name struts.custom.i18n.resources. This constant specify the resource bundle file that we created in above steps. We just have to specify name of resource bundle file without extension (ApplicationResources without .properties).
Our LoginAction contains the method execute() which is the default method getting called by Sturts2. If the name of method is different, e.g. authenticate(); then we should specify the method name in <action> tag.
 
<action name=”login” method=”authenticate” class=”com.example.LoginAction”>
 
When user enter the wrong credentials, the user will be redirected to Login page without displaying any error to add this functionality we will add the error message in our ResourceBundle file.
 
Step 14. Open ApplicationResources.properties and add the below entry for error.login in it
 
error.login= Invalid Username/Password. Please try again.

 

Also we need to add logic in LoginAction to add error message if user is not authenticated. But there is one problem. Our error message is specified in ApplicationResources.properties file. We must specify key error.login in LoginAction and the message should be displayed on JSP page.
For this we must implement com.opensymphony.xwork2.TextProvider interface which provides method getText(). This method returns String value from resource bundle file. We just have to pass the key value as argument to getText() method. The TextProvider interface defines several method that we must implement in order to get hold on getText() method. But we don’t want to spoil our code by adding all those methods which we do not intend to use. There is a good way of dealing with this problem.
Struts2 comes with a very useful class com.opensymphony.xwork2.ActionSupport. We just have to extend our LoginAction class with this class and directly use methods such as getText(), addActionErrors() etc. Thus we will extend the LoginAction class with ActionSupport class and add the logic for error reporting into it. The final code in LoginAction must look like:
 
———————————LoginAction.java——————————–
package com.example;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport
{
private String username;
private String password;

public String execute()
{
if (this.username.equals("admin")&& this.password.equals("admin123"))
{
return "success";
}
else
{
addActionError(getText("error.login"));
return "error";
}
}

public String getUsername()
{
return username;
}

public void setUsername(String username)
{
this.username = username;
}

public String getPassword()
{
return password;
}

public void setPassword(String password)
{
this.password = password;
}
}

Now run the application on Tomcat server

Login Page

Login Page

Welcome Page

Welcome Page

Login Page with Error message

Login Page with Error message

 

source : Struts Tutorial

 

 

 

Rate this post

Leave a Reply