Struts 2 Login Application XML Based Example
May 08, 2014
In this page, we will learn how to create an application in Struts 2 using XML configuration. For the example we have taken a login application. Struts 2 applications can be created annotation based and xml based. In xml based application, the actions and responses are configured in struts.xml. The JSP page which needs to be redirected after run the action is also configured in struts.xml.
Software Used
To create and run the demo we have used below software.1. JDK 6
2. Eclipse
3. Tomcat 7
Define struts.xml
In struts.xml, within the <struts> tag define the namespace, action class and jsp pages which will be redirected.struts.xml
<struts> <package name="user" namespace="/user" extends="struts-default"> <action name="login"> <result>login.jsp</result> </action> <action name="result" class="com.concretepage.action.ResultAction"> <result name="SUCCESS">result.jsp</result> <result name="ERROR">error.jsp</result> </action> </package> </struts>
Configure web.xml to Support Struts 2
Create web.xml for struts 2. Configure StrutsPrepareAndExecuteFilter so that request and responses can be filtered according to struts application.web.xml
<web-app> <display-name>Struts 2 Example</display-name> <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>
Create Action Class
I have created one action class ResultAction.java which consists two property and a method as execute(). The properties are used to populate data from jsp. Execute method is the method from where action class starts executing.ResultAction.java
package com.concretepage.action; public class ResultAction { private String userName; private String pwd; public String execute() { if(getUserName() !=null && getUserName().equals(getPwd())){ return "SUCCESS"; }else{ return "ERROR"; } } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
Create JSP pages
Find login.jsp which will be displayed first on accessing the demo. The properties defined in action class and input fields name of jsp must match in their name.login.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head></head> <body> <h1>Struts 2 Example</h1> <s:form action="result"> <s:textfield name="userName" label="User Name"/> <s:password name="pwd" label="Password"/> <s:submit/> </s:form> </body> </html>
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head></head> <body> <h1>Struts 2 Example</h1> <h4>Your User Name is: <s:property value="userName"/></h4> </body> </html>
error.jsp
<html> <head></head> <body> <h1>Struts 2 Example</h1> <h4> You are not authorized for the application. </h4> </body> </html>
Eclipse Configuration used in Struts 2 Application
I have configured login application in eclipse in below structure.Maven Dependency for Struts 2
Find the maven dependency for struts 2.pom.xml
<dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.16</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>2.3.8</version> </dependency> </dependencies>