Struts 2.1.8 Login Form
In this section we will learn how we can create form based application in Struts 2.8.1. The Struts 2 framework provides tags for creating the UI forms easily.
After completing this session you will be able to develop form based application in Struts 2.8.1. In the next session we will also learn how to validate the login form using Struts 2 validator framework.
About the example:
This example will display the login form to the user. If user enters Login Name: Admin and Password: Admin then application display the success message. If user enters anything else it displays Invalid login message.
In the Login Form example we will be developing following files:
a) JSP files: login.jsp and loginsuccess.jsp
b) Java Class: Login.java
c) Mapping in struts.xml
d) package.properties: This file contains the error message text.
Let's start developing the example:
Action class:
We have used Login.java as the action class here. The default method execute() is begin called by the struts framework to execute the action. In this method we are checking the login name and password. If the validation is successful it displays the loginsuccess.jsp page. Here is the code of the Action class Login.java:
package net.roseindia;
|
JSP files:
JSP form page loginsuccess.jsp code:
<%@ 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>
<br>
<a href="/struts2/">Back to Index page</a>
</body>
</html>
Success page(loginsuccess.jsp) code:
<html>
<head>
<title>Login Success</title>
</head>
<body>
<p align="center"><font color="#000080" size="5">Login Successful</font></p>
<br>
<a href="/struts2/">Back to Index page</a>
</body>
</html>
Add the following configuration in the struts.xml file:
<!-- Login Form Configuration -->
<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>
Testing example:
Use ant tool to compile and the example. Read in detail at here. Now type http://localhost:8080/struts2/roseindia/showLogin.action in the browser. Your web page should look like this:
Enter Login Name: Admin and Password: Admin and then click on the Login button. The application should display the success message. If you enter anything else it will display the error message with the help of <s:actionerror /> and <s:fielderror /> tags in JSP.
Download the code of Login example.
In this section we developed the login application, in the next section we will develop user registration for and then see how to add the server side validation using the Struts valuator framework.