Creating an Eclipse web project using Maven and Struts 1.x
In this article we are going to implement a web application using Struts framework which will have a login screen. Once the user is authenticated, user will be redirected to a welcome page.
Tools used :
- Struts 1.3.10
- Hibernate 4
- Eclipse Indigo
- JDK 6
- Tomcat 6.0
In this article we are going to implement a web application using Struts framework which will have a login screen. Once the user is authenticated, user will be redirected to a welcome page.
Tools used :
- Struts 1.3.10
- Hibernate 4
- Eclipse Indigo
- JDK 6
- Tomcat 6.0
Step 1 : Create Web Application Project using Maven Template
Create a Java web application project from “maven-archetype-webapp” template.
mvn archetype:generate -DgroupId=com.javawebtutor -DartifactId=StrutsWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
Step 2 : Eclipse IDE integration
Convert this project to Eclipse web project with Maven command “mvn eclipse:eclipse -Dwtpversion=1.5".Open command prompt and navigate to generated "StrutsWebApp" project and issue following command.
mvn eclipse:eclipse -Dwtpversion=1.5
Step 3 : Import the Project into eclipse
Import the project into eclipse IDE. Directory structure of projects is given bellow.
Step 4 : Update pom.xml file
Add the Struts dependencies in pom.xml. In Struts 1.x, you need the struts-core.jar for core module and struts-taglib.jar for tag library.
pom.xml<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javawebtutor</groupId> <artifactId>StrutsWebApp</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>StrutsWebApp Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-core</artifactId> <version>1.3.10</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-taglib</artifactId> <version>1.3.10</version> </dependency> </dependencies> <build> <finalName>StrutsLoginEx</finalName> </build> </project>
After updating the pom.xml again execute "mvn eclipse:eclipse -Dwtpversion=1.5". After executing this command Maven will download required libraries.
Step 5 : Create java directory :
Create a directory named java under main."/src/main".
Note:-If created java directory is not in class path of project,we need to add this directory to class path of your project.
Step 6 : Add java directory to classpath of the Project :
- Right click on Project -> Build Path -> Configure build path.a new screen will open in that screen click on Source tab and then click on Add folder as shown bellow.
- Again one new screen will open and in that screen select the java checkbox and click on ok again OK as shown bellow.
Step 7 : Create FormBean class
Create a package com.jwt.struts.forms in java folder and create a class LoginForm in that package.Add bellow lines of code in this class.
package com.jwt.struts.forms; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; public class LoginForm extends ActionForm { private static final long serialVersionUID = 1L; private String userName = null; private String password = null; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public void reset(ActionMapping mapping, HttpServletRequest request) { this.password = null; } }
Step 8 : Create Action Class
Create a package com.jwt.struts.action in java folder and create a class LoginAction in that package.Add bellow lines of code in this class.
package com.jwt.struts.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.jwt.struts.forms.LoginForm; public class LoginAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginForm loginForm = (LoginForm) form; if (loginForm.getUserName() == null || loginForm.getPassword() == null || !loginForm.getUserName().equalsIgnoreCase("Mukesh") || !loginForm.getPassword().equals("kumar")) { return mapping.findForward("failure"); } else return mapping.findForward("success"); } }
Step 9 : Create jsp files
Create two jsp files login.jsp and success.jsp inside webapp directory of project.
login.jsp<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Login Example</title> </head> <body> <html:form action="/login" focus="userName"> Username : <html:text property="userName" /> <br> Password : <html:password property="password" /> <br> <html:submit value="login" /> </html:form> </body> </html>
<%@ 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"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Welcome Page</title> </head> <body> <h1>Hello JavaWebTutor</h1> </body> </html>
Step 10 : Create struts-config.xml
Create struts-config.xml under WEB-INF directory and add following code into this.
struts-config.xml<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="loginForm" type="com.jwt.struts.form.LoginForm" /> </form-beans> <action-mappings> <action name="loginForm" path="/login" type="com.jwt.struts.action.LoginAction" scope="request" input="/login.jsp"> <forward name="failure" path="/login.jsp" redirect="true" /> <forward name="success" path="/success.jsp" redirect="true" /> </action> </action-mappings> </struts-config>
Step 11 : update the web.xml file
Change the contents of WEB-INF/web.xml 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>LoginFormStruts</display-name> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/struts-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
Final Structure Of Project
Final structure of project is given bellow:
Step 12 : Build the project
Open command prompt and navigate to "StrutsWebApp" project and issue mvn:package command.
mvn package
After executing above command "StrutsLoginEx.war" file will generate in target folder of the project.
Step 13 : Run it
Copy the generated "StrutsLoginEx.war" file from target folder to your tomcat webapp folder and start the server. After successful start of server open web browser and enter http://localhost:8080/StrutsLoginExample/login.jsp in URL bar we will see following output.
You can download the source code of the example by clicking on the Download link below.
Source (Developed in Eclipse) : Download |