Struts 2 Application Eclipse

This demo will show you how create Struts 2 log-in application eclipse.To create first Hello World application using struts 2 framework below are needed:

  • Eclipse 3.2 or above (Download eclipse from http://www.eclipse.org/downloads/ site)
  • JDK 1.6 or above (Download from here: http://www.oracle.com/technetwork/java/javase/downloads/index.html)
  • Tomcat 6 or above (Please follow link to install and configure tomcat in eclipse:Configure and Run Tomcat server in eclipse
  • Struts 2 jars which is already included in project for download in the bottom

Please follow below steps:

  • Create dynamic web project in eclipse name: Struts2HelloWorld (Please use this link if you are not familiar how to create dynamic project in eclipse: Create Dynamic Web Project Eclipse)
  • Copy below web.xml content to your project web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts 2 Hello World Application</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>Login.jsp</welcome-file>
    </welcome-file-list>

</web-app>

 

As we know for any web application first framework read web.xml file and if we use any framework we do mapping in web.xml file. Above we have filter mapping for struts 2 and corresponding mapping url pattern is /*.

We have welcome page as well HelloWorld.jsp which you will see when you deploy the application.

  • Next create struts.xml file. Basically whenever you deploy and run application struts.xml file should be there in classpath. It’s good practice if you create resource folder as source folder and create struts.xml file inside it then you don’t need to include in xml file in classpath and it will be available at runtime.
  • To create resource folder –Right click Project –>Click New –> Click Source folder as below:

Struts 2 Application Eclipse

  • Create source folder name resources –> Click Finish

Struts 2 Application Eclipse

  • Now create struts.xml file inside resource folder and copy paste below content in it.
<?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>
    <constant name="struts.enable.DynamicMethodInvocation"
        value="false" />
    <constant name="struts.devMode" value="false" />
    <constant name="struts.custom.i18n.resources"
        value="ApplicationResources" />

    <package name="default" extends="struts-default" namespace="/">
        <action name="login"
            class="com.javahorn.action.LoginAction">
            <result name="success">Welcome.jsp</result>
            <result name="error">Login.jsp</result>
        </action>
    </package>
</struts>
  • Now create ApplicationResources.properties inside resource folder as well and copy paste below content in it. This file we use to keep as message resource mapping and later you could use as internationalization support to your application
label.helloWorld = Struts 2 Hello World!!!
label.username= User Name
label.password= Password
label.login= Login
error.login= Invalid Username/Password. Please try again.
  • Now we have to create action class. Create package com.javahorn.action inside src folder and copy paste below LoginAction.java inside it
package com.javahorn.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String username;
	private String password;

	public String execute() {

		if (this.username.equals("admin") && this.password.equals("admin")) {
			return "success";
		} else {
			addActionError(getText("error.login"));
			return "error";
		}
	}

	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;
	}
}
  • Create Login.jsp inside WebContent folder and copy paste below content in it
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 - Login Application </title>
<style type="text/css">
.error{color: #ff0000;font-size: 14px;height: 3px;font-weight: bold;}
</style>
</head>

<body>
<h2>Login page:</h2>
<b>Login & Password hints<br></b>
Login Id: admin<br>
Password: admin  <br><br>
<div class="error">
<s:actionerror/>
</div>

<s:form action="login.action" method="post">
	<s:textfield name="username" key="label.username" size="20" />
    <s:password name="password" key="label.password" size="20" />
    <s:submit method="execute" key="label.login" align="left" />
</s:form>
</body>
</html>

 

  • Create Welcome.jsp inside WebContent folder and copy paste below content in it
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome Page</title>
</head>

<body>
    <h2>Welcome Struts 2 <s:property value="username" /></h2>
</body>
</html>

 

  • Below are the jars needed to run the application. You could download project source code in the bottom and copy paste below jars inside lib folder. Below is full project structure and jars files screen shot:

Struts 2 Application Eclipse

  • That’s it. Now we are ready to run this project.To run this project right click project –> Click Run As –> Run on Server as below.Finally you will see below Log in page. Enter login id and password (admin/admin) as below and click Login:

Struts 2 Application Eclipse

  • You have entered your login id and passworld once validatin successfull you will see Welcome page below:

Struts 2 Application Eclipse

download2 Download Project: Struts2HelloWorld

Leave a Reply

Your email address will not be published. Required fields are marked *