|
Struts is the grandaddy of Java webapp frameworks so it’s fitting that we start our tour here. I think it’s probably safe to say that Struts was the first model 2 (web MVC) framework to gain widespread adoption in the Java arena and to this day it’s still used by many people.
Just to ensure everybody is up to speed, model 2/web MVC is an architectural pattern that promotes separation of concerns between the model, the view and the controller in a web environment. As we saw in the previous model 1 implementations of the sample application, each of the JSP pages contains Java code to lookup data and display it to the user. With an MVC approach, each of the components has a strict responsibility as follows.
- Model : represents the data being viewed/manipulated.
- View : responsible for rendering the model back to the user.
- Controller : responsible for taking the request/user input and initialising the model, manipulating it, etc.
In reality, different people have different views as to whether the controller represents only the web specific parts of the process flow and whether real business logic in fact resides in the model (it being a model of the underlying business). What’s important here is that web MVC promotes a separation of concerns through reusable and testable components, regardless of how you cut it. For more information about the model 2 architecture, take a look at Designing Web Applications and Servlet Patterns.
Strut 2 Architecture:
Request Lifecycle in Struts 2 applications
- User Sends request: User sends a request to the server for some resource.
- FilterDispatcher determines the appropriate action: The FilterDispatcher looks at the request and then determines the appropriate Action.
- Interceptors are applied: Interceptors configured for applying the common functionalities such as workflow, validation, file upload etc. are automatically applied to the request.
- Execution of Action: Then the action method is executed to perform the database related operations like storing or retrieving data from the database.
- Output rendering: Then the Result renders the output.
- Return of Request: Then the request returns through the interceptors in the reverse order. The returning request allows us to perform the clean-up or additional processing.
- Display the result to user: Finally the control is returned to the servlet container, which sends the output to the user browser.

Developing Login Applicaiton in Struts:
- Login page is displayed to take the input.
- User enters user name and password and then clicks on the “Login” button.
- User validation is done in action class and if user enters Admin/Admin in the user name/password fields, then success pages is displayed. Otherwise the error message is displayed on the screen.
1. Login.jsp
<%@ 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>
Note: When application is executed it generates the following Html Code:
<html> <head> <title>Struts 2 Login Application!</title>
<link href=”/struts2tutorial/css/main.css” rel=”stylesheet” type=”text/css”/> </head>
<body>
<form id=”doLogin” name=”doLogin” onsubmit=”return true;” action=”/struts2tutorial/roseindia/doLogin.action” method=”POST”> <table class=”wwFormTable”> <tr> <td colspan=”2″> Login </td>
</tr> <tr> <td class=”tdLabel”><label for=”doLogin_username” class=”label”> Login name:</label> </td> <td><input type=”text” name=”username” value=”" id=”doLogin_username”/> </td> </tr> <tr> <td class=”tdLabel”><label for=”doLogin_password” class=”label”> Password:</label></td> <td><input type=”password” name=”password” id=”doLogin_password”/> </td> </tr> <tr> <td colspan=”2″> <div align=”center”> <input type=”submit” id=”doLogin_0″ value=”Login”/> </div> </td> </tr> </table> </form> </body> </html>
2. Loginsuccess.jsp <html>
<head>
<title>Login Success</title>
</head>
<body>
<p align=“center”><font color=“#000080″ size=“5″>Login Successful</font></p>
</body>
</html>
3. Action Class
package strtu.login;
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;
}
}
4. Config action mapping (Strut.xml) <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>
|