Home Struts Struts2.1.8 Struts 2.1.8 Login Form

Related Tutorials


 
 

Share on Google+Share on Google+

Struts 2.1.8 Login Form

Advertisement
Learn how to develop Struts 2.1.8 Login Form.

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;
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 {
  if(!getUsername().equals("Admin"|| !getPassword().equals("Admin")){
  addActionError(getText("Login.invalidLogin"));
  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;
  }

}

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.

    

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 9, 2010

Related Tutorials

Discuss: Struts 2.1.8 Login Form  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Jeevan Patil
March 15, 2011
struts2

In my application, the /struts-tags are not working. It is giving error. java.lang.NoClassDefFoundError: com/opensymphony/xwork2/util/ValueStack at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2427) at java.lang.Class.privateGetPublicMethods(Class.java:2547) at java.lang.Class.getMethods(Class.java:1410) at java.beans.Introspector.getPublicDeclaredMethods(Introspector.java:1284) at java.beans.Introspector.getTargetMethodInfo(Introspector.java:1158) at java.beans.Introspector.getBeanInfo(Introspector.java:408) at java.beans.Introspector.getBeanInfo(Introspector.java:167) at java.beans.Introspector.getBeanInfo(Introspector.java:230) at java.beans.Introspector.<init>(Introspector.java:389) at java.beans.Introspector.getBeanInfo(Introspector.java:167) at java.beans.Introspector.getBeanInfo(Introspector.java:230) at java.beans.Introspector.<init>(Introspector.java:389) at java.beans.Introspector.getBeanInfo(Introspector.java:167) at org.apache.jasper.runtime.JspRuntimeLibrary.getWriteMethod(JspRuntimeLibrary.java:799) at org.apache.jasper.compiler.Validator$ValidateVisitor.checkSetter(Validator.java:1031) at org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(Validator.java:1112) at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:834) at org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1482) at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2257) at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2307...
DMCA.com