In this example we will see how to create a simple login page ,validation and navigation using Struts 2
Environment Used
Java 1.7
Eclipse Juno
Apache Tomcat 7
Struts 2 Jars
1. Create a dynamic web project in Eclipse
Create a Dynamic web project in Eclipse and configure apache tomcat as Server. Once it is created download Struts 2 jars and place in to WEB-INF/lib folder. Here is the final structure of the project. (Click here to download this example with jars)
2. Update Web.xml file
Add Struts FilterDispatcher details in to web.xml
<?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>Struts2HelloWorldExample</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.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
<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>Struts2HelloWorldExample</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.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
3. Create an Action class (LoginAction.java)
package com.pretech;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String name;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() {
if (name.equals("pretech") & password.equals("pretech")) {
return SUCCESS;
} else {
return ERROR;
}
}
}
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String name;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() {
if (name.equals("pretech") & password.equals("pretech")) {
return SUCCESS;
} else {
return ERROR;
}
}
}
4. Create a loginform (index.jsp)
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<s:form action="loginaction">
<s:textfield name="name" label="Enter Username" /><br>
<s:password name="password" label="Enter Password" /><br>
<s:submit value="Submit" align="center" />
</s:form>
</body>
</html>
<html>
<body>
<s:form action="loginaction">
<s:textfield name="name" label="Enter Username" /><br>
<s:password name="password" label="Enter Password" /><br>
<s:submit value="Submit" align="center" />
</s:form>
</body>
</html>
5. Create a success page (success.jsp)
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head></head>
<body>
<h4> You
<s:property value="name" /> Successfully logged
</h4>
</body>
</html>
<html>
<head></head>
<body>
<h4> You
<s:property value="name" /> Successfully logged
</h4>
</body>
</html>
6. Create an error page (error.jsp)
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head></head>
<body>
<h4> Login Failed
</h4>
</body>
</html>
<html>
<head></head>
<body>
<h4> Login Failed
</h4>
</body>
</html>
7. Create Struts.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="com" extends="struts-default">
<action name="loginaction" class="com.pretech.LoginAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="com" extends="struts-default">
<action name="loginaction" class="com.pretech.LoginAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
8. Deploy and Run application in Tomcat
Run the application and give your input
0 comments:
Post a Comment