World ApplicationWorld Application
Struts 2 Login Login Application Application
• struts-default.xmlstruts-default.xml
•
• struts-plugin.xmlstruts-plugin.xml
•
• struts.xmlstruts.xml
•
• struts.propertiesstruts.properties
•
• web.xmlweb.xml The
The struts.propertiesstruts.properties file is provided for backward-compatiblity with WebWork. Infile is provided for backward-compatiblity with WebWork. In the struts.properties file, each entry is treated as a constant. In
the struts.properties file, each entry is treated as a constant. In the web.xml file, anythe web.xml file, any FilterDispatcher initialization parameters are loaded as constants.
FilterDispatcher initialization parameters are loaded as constants.
In the various XML variants, the constant
In the various XML variants, the constant element has two required attributes :element has two required attributes :namename andand
value value.. A
Attttrriibbuuttee RReeqquuiirreedd DDeessccrriippttiioonn name
name yesyes the name of the constantthe name of the constant value
value yesyes the value of the constantthe value of the constant Constant Example (struts.xml)
Constant Example (struts.xml)
<struts>
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.devMode" value="true" />
...
...
</struts>
</struts>
Constant Example (struts.properties) Constant Example (struts.properties) struts.devMode = true
struts.devMode = true
Struts 2
Struts 2 Login Login Application Application
Developing Struts 2 Login Application Developing Struts 2 Login Application
In this section we are going to develop login application based on Struts 2 Framework.
In this section we are going to develop login application based on Struts 2 Framework.
Our current login application does not
Our current login application does not validate the user against the database. validate the user against the database. Instead loginInstead login name and passwords are validated against the hardcode values (User: Admin and
name and passwords are validated against the hardcode values (User: Admin and Password: Admin) in the actions class.
Password: Admin) in the actions class.
Working of the application Working of the application
1.
1. LogLogin pain page ige is diss displaplayed tyed to tako take the ie the inputnputss 2.
2. User eUser enters nters user nauser name and pame and passworssword and thed and then clicn clicks on the "ks on the "Login" bLogin" buttonutton.. 3.
3. User vaUser validatlidation is done iion is done in action cln action class and if uass and if user entser enters Admers Admin/Adin/Admin in the usmin in the user er name/password fields, then success pages is displayed. Otherwise the error
name/password fields, then success pages is displayed. Otherwise the error message is displayed on the screen.
message is displayed on the screen.
Steps to developed the application Steps to developed the application Here are simple and easy steps to
Here are simple and easy steps to develop Login page in develop Login page in the using Struts 2 framework.the using Struts 2 framework.
1.
1. Develop Login FormDevelop Login Form The gui of the application
The gui of the application consists of login form (consists of login form (login.jsplogin.jsp) and success message) and success message page (
page (loginsuccess.jsploginsuccess.jsp) .) . The
The login.jsplogin.jsp is used to display the login page to is used to display the login page to the user. In our application it isthe user. In our application it is saved in "webapps\struts2tutorial\pages\" folder. Here is the code of
saved in "webapps\struts2tutorial\pages\" folder. Here is the code of login.jsplogin.jspfile:file:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<html>
<head>
<head>
<title>Struts 2 Login A
<title>Struts 2 Login Application!</title>pplication!</title>
<link href="<s:url value="
<link href="<s:url value="/css/main.css/css/main.css"/>" rel="stylesheet" type="text/css"/>"/>" rel="stylesheet" type="text/css"/>
</head>
<s:textfield name="username" label="Login name"/>
<s:textfield name="username" label="Login name"/>
<s:password name="password" label="Password"/>
</html>
displays action errors and field validation errors.
displays action errors and field validation errors.
3.
3. The codeThe code<s:form action="doLogin" method="POST"><s:form action="doLogin" method="POST"> generates the htmlgenerates the html form for the application.
form for the application.
The code : The code :
<s:textfield name="username" label="Login name"/>
<s:textfield name="username" label="Login name"/>
<s:password name="password" label="Password"/>
<s:password name="password" label="Password"/>
generates Login Name and
generates Login Name and Password fields.Password fields.
4.
4. The submit button is generated throughThe submit button is generated through<s:submit value="Login"<s:submit value="Login"
align="center"/>
align="center"/> code.code.
When application is executed it gen
When application is executed it generates the following html code:erates the following html code:
<html>
<html>
<head>
<head>
<title>Struts 2 Login Application!</title>
<title>Struts 2 Login Application!</title>
<link href="/struts2tutorial/css/main.css" rel="stylesheet"
<form id="doLogin" name="doLogin" onsubmit="return true;"
<form id="doLogin" name="doLogin" onsubmit="return true;"
action="/struts2tutorial/roseindia/doLogin.action" method="POST">
<td class="tdLabel"><label for="doLogin_username" class="label">
<td class="tdLabel"><label for="doLogin_username" class="label">
Login name:</label>
Login name:</label>
</td>
</td>
<td><input type="text" name="username" value="" id="doLogin_username"/>
<td><input type="text" name="username" value="" id="doLogin_username"/>
</td>
<td class="tdLabel"><label for="doLogin_password" class="label">
<td class="tdLabel"><label for="doLogin_password" class="label">
Password:</label></td>
Password:</label></td>
<td><input type="password" name="password" id="doLogin_password"/>
<td><input type="password" name="password" id="doLogin_password"/>
</td>
</td>
</tr>
</tr>
<tr>
<tr>
<td colspan="2"><div align="center"><input type="submit"
<td colspan="2"><div align="center"><input type="submit"
id="doLogin_0" value="Login"/>
id="doLogin_0" value="Login"/>
</div></td>
</div></td>
</tr>
</tr>
</table></form>
</table></form>
</body>
</body>
</html>
</html>
5.
5. On viewing the above On viewing the above generated html code you will find that Struts 2generated html code you will find that Struts 2
automatically generates form, html table, label for the html elements. This another automatically generates form, html table, label for the html elements. This another grate feature of Struts as compared to Struts 1.x.
grate feature of Struts as compared to Struts 1.x.
The
The loginsuccess.jsploginsuccess.jsp page displays the Login Success message when user ispage displays the Login Success message when user is authenticated successfully. Here is the code of
authenticated successfully. Here is the code of loginsuccess.jsploginsuccess.jsp file:file:
<html>
<html>
<head>
<head>
<title>
<title>Login SuccessLogin Success</title></title>
</head>
</head>
<body>
<body>
<p align=
<p align="center""center"><font color=><font color="#000080""#000080"size=size="5""5">>LoginLogin Successful
Successful</font></p></font></p>
</body>
</body>
</html>
</html>
6.
6.
7.
7. Developing Action ClassDeveloping Action Class
Now let's develop the action class
Now let's develop the action class to handle the login request. In Struts 2 to handle the login request. In Struts 2 it is notit is not necessary to implement the Action interface, any POJO object
necessary to implement the Action interface, any POJO object with executewith execute signature can be used in
signature can be used in Struts 2. The Struts 2 framework provides a baStruts 2. The Struts 2 framework provides a basese
ActionSupport class to implement commonly used interfaces. In our action class ActionSupport class to implement commonly used interfaces. In our action class (Login.java) we have implemented ActionSupport interface. Our "Login.java" is (Login.java) we have implemented ActionSupport interface. Our "Login.java" is saved in
saved in the "webapps\struts2tutorial\WEB-INF\src\java\net\rthe "webapps\struts2tutorial\WEB-INF\src\java\net\roseindia" directoy.oseindia" directoy.
Here is the code of Login.java action class:
Here is the code of Login.java action class:
package
* <p><p> Validate a user login.Validate a user login. </p></p>
*/
*/
public class
public classLoginLogin extendsextends ActionSupport {ActionSupport {
public
public String execute()String execute() throwsthrows Exception {Exception { System.out.println(
System.out.println( "Validating login""Validating login"););
if
if(!getUsername().equals((!getUsername().equals( "Admin""Admin") || !) || ! getPassword().equals(
getPassword().equals( "Admin""Admin")){)){
addActionError(
addActionError( "Invalid user name or password! Please"Invalid user name or password! Please try again!"
// Username property // Username property ----/**
/**
*
* <p><p>Field to store User username.Field to store User username.</p></p>
*
* <p/><p/>
*/
*/
private
private String username =String username = nullnull;;
/**
/**
*
* <p><p>Provide User username.Provide User username.</p></p>
*
*
*
* @return@return Returns the User username.Returns the User username.
*/
*/
public
public String getUsername() {String getUsername() { return
* <p><p>Store new User usernameStore new User username</p></p>
*
*
*
* @param@param value The username to set.value The username to set.
*/
*/
public void
public void setUsername(String value) {setUsername(String value) { username = value;
username = value;
} }
// Username property // Username property ----/**
/**
*
* <p><p>Field to store User password.Field to store User password.</p></p>
*
* <p/><p/>
*/
*/
private
private String password =String password = nullnull;;
/**
/**
*
* <p><p>Provide User password.Provide User password.</p></p>
*
*
*
* @return@return Returns the User password.Returns the User password.
*/
*/
public
public String getPassword() {String getPassword() { return
* <p><p>Store new User passwordStore new User password</p></p>
*
*
*
* @param@param value The password to set.value The password to set.
*/
*/
public void
public void setPassword(String value) {setPassword(String value) { password = value;
9. Configuring action mapping (in struts.xml)Configuring action mapping (in struts.xml) Now we will create action mapping
Now we will create action mapping in the struts.xml file. Here is the code to in the struts.xml file. Here is the code to bebe added in the struts.xml:
added in the struts.xml:
<action name="
<action name="showLoginshowLogin">">
<result>/pages/login.jsp</result>
<result>/pages/login.jsp</result>
</action>
</action>
<action name="d
<action name="doLoginoLogin" class="" class="net.roseindia.Loginnet.roseindia.Login">">
<result name="input">/pages/login.jsp</result>
<result name="input">/pages/login.jsp</result>
<result
<result name="error">/pages/login.jsp</resultname="error">/pages/login.jsp</result>>
<result>/pages/loginsuccess.jsp</result>
<result>/pages/loginsuccess.jsp</result>
</action>
</action>
10.
10.In the above mapping the action "showLogin" is In the above mapping the action "showLogin" is used to display the lused to display the login pageogin page and "doLogin" validates the user
and "doLogin" validates the user using action class (Login.java).using action class (Login.java).
11.
11.CSS file (main.css)CSS file (main.css)
This css file is used to enhance the
This css file is used to enhance the presentation of the login form. Thepresentation of the login form. Themain.cssmain.css isis saved into
saved into "\webapps\struts2tutorial\"\webapps\struts2tutorial\css" directory.css" directory.
Here is the code of
Here is the code of main.cssmain.css::
@CHARSET "UTF-8";
@CHARSET "UTF-8";
body { body {
font: 12px verdana, arial, helvetica, sans-serif;
font: 12px verdana, arial, helvetica, sans-serif;
background-color:#FFFFFF;
background-color:#FFFFFF;
} }
table.wwFormTable { table.wwFormTable {
font: 12px verdana, arial, helvetica, sans-serif;
font: 12px verdana, arial, helvetica, sans-serif;
border-width: 1px;
table.wwFormTable tr td { table.wwFormTable tr td { background-color: #dfd;
border-width: 1px 1px 0px;
border-width: 1px 1px 0px;
border-color: black;
border-color: black;
padding: 5px;
padding: 5px;
background-color: #7a7;
/* margin-bottom: 12px; */
/* margin-bottom: 12px; */
} }
#buttonBar {
#buttonBar {
border-width: 0px 1px 1px;
border-width: 0px 1px 1px;
border-style: solid;
In the next section we
In the next section we will learn how to add validation to will learn how to add validation to the login application.the login application.