Saturday, May 22, 2010

Struts2 Login

Here I am going to simple login application using struts2.

First create a dynamic web project.

Jar files that are used required :
you can download jar files from here.

Lets modify the web.xml File. Location of the file is : WebContent/WEB-INF
-------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>test1</display-name>
   <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
   <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
    </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
-------------------------------------------------------------------------------------------------------------



Now Lets create index.jsp under WebContent directory
Copy and paste the following code :
-------------------------------------------------------------------------------------------------------------
<%@ 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">
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="loadMitesh">
<s:textfield label="User Name" name="userName"></s:textfield>
<s:submit name="submit"></s:submit>
</s:form>
</body>
</html>
-------------------------------------------------------------------------------------------------------------


Note : Here loadMitesh is the logical name that is mapped by struts.xml file by the actual file name.
Now lets create struts.xml file  This file should be created under the src folder.

-------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
        <package name="Mitesh" namespace="/" extends="struts-default">
        <action name="displayLogin">
        <result>/index.jsp</result>
        </action>
        <action name="loadMitesh" class="org.mitesh.miteshAction">
                        <result name="success">/welcome.jsp</result>
                        <result name="error">/goout.jsp</result>
                </action>
        </package>
</struts>
-------------------------------------------------------------------------------------------------------------


Here org.mitesh.miteshAction is fully classified name of the java class that is to be executed when action name is loadMitesh. So, This is called when the form is submitted.
miteshAction contains execute() method that is executed and based on the user name provided it will return success or error. And according to that flow of program will go ahead.

Lets see miteshAction.java

create a package org.mitesh under src directory and create a class miteshAction.java.

-------------------------------------------------------------------------------------------------------------
package org.mitesh;
import com.opensymphony.xwork2.ActionSupport;
public class miteshAction extends ActionSupport{
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
System.out.println("setting user name : " + userName);
this.userName = userName;
}
public String execute() {
if(userName.equals("mitesh"))
{
return SUCCESS;
}
else
{
System.out.println("in else");
return "error";
}
}
}
-------------------------------------------------------------------------------------------------------------


when the user will submit the user name as mitesh the execute method will return SUCCESS that is caught by strut.xml's result under

<action name="loadMitesh" class="org.mitesh.miteshAction"> 
                         
                        <result name="success">/welcome.jsp</result>
                        <result name="error">/goout.jsp</result> 
</action>

 and the control will go to welcome.jsp



Lets see welcome.jsp
-------------------------------------------------------------------------------------------------------------

this file uses struts property tag to display the userName value that is set in miteshAction class.

if user name is wrong execute() method will return error and the  control will transfer to goout.jsp
lets see goout.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">
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Sorry Wrong User Name
<a href="displayLogin">Try Again</a>
</body>
</html>
-------------------------------------------------------------------------------------------------------------

here I have provided link with logical name displayLogin which will be mapped by struts.xml file to index.jsp

<action name="displayLogin">
        <result>/index.jsp</result>
</action>


right click on the project and run it........


Final directory structure : 



No comments:

Post a Comment