We have seen that what is struts and how it works. I will guide the new developer, step by step to create the login application using struts 2 in this article.
Step 1 : Download the struts jar file or sample application from the official struts website.
Step 2 : Create a web project for jsp and open web.xml. In web.xml add the following lines of code in between <web-app> tag.
<filter> <filter-name>webwork</filter-name> <filter-class> org.apache.struts.action2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>webwork</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Above configuration declares FilterDispatcher for struts 2 which is boot strap component for your website.
Step 3 : Copy all the jar files of struts 2 application downloaded in Step 1 into lib folder of your application.
Step 4 : Create struts.xml file at the root level of the classes and add following lines :
<?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.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <include file="Login.xml"/> </struts>
As you can see in above configuration, I have included other configuration file named “Login.xml” Instead of writing all the configuration in single struts.xml , we can split the xml configuration file and include in main struts.xml. I will explain the creation of file “Login.xml” later in this tutorail series.
Step 5 : Now create a jsp page in folder “pages” under WebContent folder with Name “Login.jsp”.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Login</title> </head> <body> <form action="LoginAction.action" > UserName : <input type="text" name="uName" /><br/> Password : <input type="password" name="pwd" /><br /> <input type="submit"></input> </form> </body> </html>
Step 6 : Create one Model Class, where the logic is written to validate the User. Here in this series no database is envolved. We have simply hardcoaded the tutorial for easy understanding of the concept.
package com.G2.Model; public class LoginValidate { public static boolean isLoginValid(String uName, String pwd) { return "admin".equals(uName) && "admin".equals(pwd)?true:false; } }
Step 7: Now create the Action Class.
package com.G2.Actions; import com.G2.Model.LoginValidate; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { String uName,pwd; public String getuName() { return uName; } public void setuName(String uName) { this.uName = uName; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String execute() throws Exception { return LoginValidate.isLoginValid(uName, pwd) ? SUCCESS : INPUT; } }
Points to remember while creating Action classes in Struts 2:
- Every Action Class extend the Class “com.opensymphony.xwork2.ActionSupport“.
- override the execute() method.
- There is no compulsion on extending the ActionSupport class, and not necessary that only execute() method works. Any method, which return string can work. That’s the powerful feature of new Struts 2 framework. But if we will not extend “ActionSupport”, then we will miss much of the default capabilities of Struts like Validation features.
- Variable name must be same as expected request parameter name with getter and setters. In this case we expect two request parameter named uName and pwd.
- Any String result can be returned from execute() method of the struts2, but it is recommended to return the standard defined result like SUCCESS or INPUT.
Step 8: Create configuration file which is included in step 4 of name “Login.xml”. Instead of creating this file, we may also add the code lines in struts.xml, but it is good practice to have the separate xml file of separate modules in application.
<?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> <package name="Login" extends="struts-default"> <action name="LoginAction" class="com.G2.Actions.LoginAction"> <result name="input">/pages/Login.jsp</result> <result name="success">/pages/LoginSuccess.jsp</result> </action> </package> </struts>
Package in Java used to group together similar type of class. In struts 2, package tag is used to group together similar types of actions along with interceptors.
The action “LoginAction”, may return two types of result, either “input” or “success”, depending on that the view is decided.
Instead of writing “jsp file path” in <result> , it is recommended to write the action, so that in future, if the page name changes then instead of changing jsp file path everywhere, we have to change only tag.
So, the above configuration can be re-written as:
<?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> <package name="Login" extends="struts-default"> <action name="LoginInput"> <result>/pages/Login.jsp</result> </action> <action name="LoginSuccess"> <result>/pages/LoginSuccess.jsp</result> </action> <action name="LoginAction" class="com.G2.Actions.LoginAction"> <result name="input" type="chain">LoginInput</result> <result name="success" type="chain">LoginSuccess</result> </action> </package> </struts>
Note here, “type“ attribute is introduced. There are many result types available in struts.
Mostly used types are: chain, redirect and dispatcher
Difference in “chain” and “redirectAction”
In “Chain” result type, the next action class gets request object with all the parameters of the previous actions class whereas in “redirectAction”, new request is generated and therefore next Action class will not get the previous request and its parameters.
All available types can be read from: http://struts.apache.org/2.x/docs/result-types.html
Final application structure is:
Download War file of Source code (Change extension from zip to war)
Leave a Reply to rashuCancel reply