0% found this document useful (0 votes)
48 views16 pages

Struts Examples Using Eclipse: Step: 1

The document describes the steps to create a simple login application using Struts in Eclipse: 1. Create a new Dynamic Web Project in Eclipse and add required Struts jar files. 2. Create a LoginAction class that validates username and password and returns success or error results. 3. Create JSP files for the login form and welcome page. 4. Configure the Struts XML file to map the login action and define success and error results. Running the project allows validating a username and password and navigating to the welcome page on success.

Uploaded by

ravimmcc
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views16 pages

Struts Examples Using Eclipse: Step: 1

The document describes the steps to create a simple login application using Struts in Eclipse: 1. Create a new Dynamic Web Project in Eclipse and add required Struts jar files. 2. Create a LoginAction class that validates username and password and returns success or error results. 3. Create JSP files for the login form and welcome page. 4. Configure the Struts XML file to map the login action and define success and error results. Running the project allows validating a username and password and navigating to the welcome page on success.

Uploaded by

ravimmcc
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

Struts Examples using Eclipse

Step : 1

First create a new project, go to FileNew  and select DynamicWebProject.

Enter the project name and click the Finish button.

Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17


 24323605, 9444037410 47691793.doc Page 1 of 16
Step : 2
Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17
 24323605, 9444037410 47691793.doc Page 2 of 16
Add the following jar files to the WEB-INF\lib directory.

The following picture shows the directory structure of the Struts application.

Right click the Jave Resources:src folder and right click select NewSource Folder.

Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17


 24323605, 9444037410 47691793.doc Page 3 of 16
Enter the folder name as resources, Select the project name as Struts Example1 and
click Finish.

Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17


 24323605, 9444037410 47691793.doc Page 4 of 16
Now right click the newly created package and select NewClass.

Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17


 24323605, 9444037410 47691793.doc Page 5 of 16
Enter the class name as LoginAction and the package name as mmcc.struts2 and click
Finish.

LoginAction.java
Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17
 24323605, 9444037410 47691793.doc Page 6 of 16
package mmcc.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
private String username;
private String password;
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;
}
public String execute()
{
if (this.username.equals("mmcc")
&& this.password.equals("mmcc123")) {
return "success";
} else {
addActionError(getText("error.login"));
return "error";
}
}
}

Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17


 24323605, 9444037410 47691793.doc Page 7 of 16
Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17
 24323605, 9444037410 47691793.doc Page 8 of 16
ApplicationResources.properties
label.username= Username
label.password= Password
label.login= Login
error.login= Invalid Username/Password. Please try again.

Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17


 24323605, 9444037410 47691793.doc Page 9 of 16
Login.jsp

Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17


 24323605, 9444037410 47691793.doc Page 10 of 16
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 - Login Application | ViralPatel.net</title>
</head>

<body>
<h2>Struts 2 - Login Application</h2>
<s:actionerror />
<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="center" />
</s:form>
</body>
</html>

Welcome.jsp

<%@ page contentType="text/html; charset=UTF-8"%>


<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Mmcc Computer Centre</title>
</head>
<body>
<h2>Hello, <s:property value="username" />...!</h2>
</body>
</html>

Web.xml

<?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>Struts2 Application</display-name>
<listener>
<listener-class>
org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
</listener>
Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17
 24323605, 9444037410 47691793.doc Page 11 of 16
<context-param>
<param-name>tilesDefinitions</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>
<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>

Struts.xml

Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17


 24323605, 9444037410 47691793.doc Page 12 of 16
<?xml version="1.0" encoding="UTF-8" ?>
Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17
 24323605, 9444037410 47691793.doc Page 13 of 16
<!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" method="authenticate"
class="mmcc.struts2.LoginAction">
<result name="success">Welcome.jsp</result>
<result name="error">Login.jsp</result>
</action>
</package>
</struts>

Run the Project

Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17


 24323605, 9444037410 47691793.doc Page 14 of 16
Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17
 24323605, 9444037410 47691793.doc Page 15 of 16
Output :

Mmcc Computer Centre, 38 Burkit Road, T.Nagar, Chennai-17


 24323605, 9444037410 47691793.doc Page 16 of 16

You might also like