Struts Roseindia

Here we will discuss in detail about the Struts framework, its latest release, features, architecture and versions.

Struts Roseindia

Struts 2 Framework is used to develop enterprise web application. It is an open source framework. For this we require Servlet API 2.4, JSP API 2.0 and Java 5. Struts 2 Framework encourages Model-View-Controller based architecture. It is made such to make the work easier by dividing the work in three different parts. Struts 2 framework builds, develops and maintains the whole application. It is based on an Interface and all the base classes are given an extra application.

Struts 2 Tags contain output data and provide style sheet driven markup that helps in creating dynamic web applications with less coding. Tags also support validation and localization of coding offering more utilization.

Struts 2 Interceptors is a powerful mechanism that enhances the control on a request. Interceptors perform multiple tasks including Logging, Validation, File Upload, Double-submit guard, exception handling, internationalization, displaying intermediate result etc. Controller call Interceptors before and after the execution of Action.

Features of Struts 2

  • Simple and easy web app development
  • POJO based actions
  • classes are based on interfaces
  • core interfaces are independent from HTTP
  • Any class can be used as an action class
  • JavaBean is used to input properties in action class
  • Struts 2 actions can be easily integrated in Spring
  • Ajax support helps to make apps more dynamic

Struts 2 Architecture:

Model: contains the data and the business logic and rules that govern the access of data and update the data.

View: is the interface that interacts with outside world and displays the result. There can be multiple view for different purposes.

Controller: controls the interaction between the user, model and view. It maps the request sent by user to a specific action and then invokes it.

Lifecycle of a Request in Struts 2 Framework:

  • A user sends request to the server
  • Controller maps the request and determines which Action to invoke.
  • Interceptors are applied to the request.
  • Action is executed to perform database operation'
  • Output is rendered.
  • Interceptors are applied again in reverse order.
  • Finally the control is returned to the servlet container, which sends the output to the user

The list of Struts Framework:

  • Struts 2.3.15.2 - s the latest stable release
  • Struts 2.3.8
  • Struts 2.2.1
  • Struts 2.1.8
  • Struts 2
  • Struts 1

Struts 2 Login Form Example:

package net.roseindia;


import com.opensymphony.xwork2.ActionSupport;

import java.util.Date;


/**
* <p> Validate a user login. </p>

*/

public class Login extends ActionSupport {


public String execute() throws Exception {

System.out.println("Validating login");

if(!getUsername().equals("Admin") || !getPassword().equals("Admin")){

addActionError("Invalid user name or password! Please try again!");

return ERROR;

}else{
return SUCCESS;

}
}


// ---- Username property ----

/**
* <p>Field to store User username.</p>
* <p/>

*/

private String username = null;


/**
* <p>Provide User username.</p>

*
* @return Returns the User username.

*/

public String getUsername() {

return username;

}

/**
* <p>Store new User username</p>

*
* @param value The username to set.

*/

public void setUsername(String value) {

username = value;

}



// ---- Username property ----

/**
* <p>Field to store User password.</p>
* <p/>

*/

private String password = null;


/**
* <p>Provide User password.</p>

*
* @return Returns the User password.

*/

public String getPassword() {

return password;

}


/**
* <p>Store new User password</p>

*
* @param value The password to set.

*/

public void setPassword(String value) {
password = value;

}


}
Here is the code of struts.xml file:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">


<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />


<package name="default" namespace="/example" extends="struts-default">


<global-results>
<result name="error">/error.jsp</result>
</global-results>

<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"

result="error" />
</global-exception-mappings>

<action name="showLogin">
<result>/pages/login.jsp</result>
</action>

<action name="doLogin" class="net.roseindia.Login">
<result name="input">/pages/login.jsp</result>
<result name="error">/pages/login.jsp</result>
<result>/pages/loginsuccess.jsp</result>
</action>

</package>

<!-- Add packages here -->
</struts>
Here is the code of login.jsp file:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts 2 Login Application!</title>
<link href="<s:url value="/css/main.css"/>" rel="stylesheet" type="text/css"/>
</head>
<body>

<s:form action="doLogin" method="POST">
<tr>
<td colspan="2">

Login
</td>

</tr>
<tr>
<td colspan="2">
<s:actionerror />
<s:fielderror />
</td>
</tr>


<s:textfield name="username" label="Login name"/>
<s:password name="password" label="Password"/>
<s:submit value="Login" align="center"/>
</s:form>
</body>
</html>

Resource: