Struts Examples using Eclipse
Step : 1
First create a new project, go to FileNew and select DynamicWebProject.
Enter the project name and click the Finish button.
Mmcc Computer Centre, 38 Burkit Road, [Link], Chennai-17
24323605, 9444037410 [Link] Page 1 of 16
Step : 2
Mmcc Computer Centre, 38 Burkit Road, [Link], Chennai-17
24323605, 9444037410 [Link] 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 NewSource Folder.
Mmcc Computer Centre, 38 Burkit Road, [Link], Chennai-17
24323605, 9444037410 [Link] 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, [Link], Chennai-17
24323605, 9444037410 [Link] Page 4 of 16
Now right click the newly created package and select NewClass.
Mmcc Computer Centre, 38 Burkit Road, [Link], Chennai-17
24323605, 9444037410 [Link] Page 5 of 16
Enter the class name as LoginAction and the package name as mmcc.struts2 and click
Finish.
[Link]
Mmcc Computer Centre, 38 Burkit Road, [Link], Chennai-17
24323605, 9444037410 [Link] Page 6 of 16
package mmcc.struts2;
import [Link];
public class LoginAction extends ActionSupport
{
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
[Link] = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
[Link] = password;
}
public String execute()
{
if ([Link]("mmcc")
&& [Link]("mmcc123")) {
return "success";
} else {
addActionError(getText("[Link]"));
return "error";
}
}
}
Mmcc Computer Centre, 38 Burkit Road, [Link], Chennai-17
24323605, 9444037410 [Link] Page 7 of 16
Mmcc Computer Centre, 38 Burkit Road, [Link], Chennai-17
24323605, 9444037410 [Link] Page 8 of 16
[Link]
[Link]= Username
[Link]= Password
[Link]= Login
[Link]= Invalid Username/Password. Please try again.
Mmcc Computer Centre, 38 Burkit Road, [Link], Chennai-17
24323605, 9444037410 [Link] Page 9 of 16
[Link]
Mmcc Computer Centre, 38 Burkit Road, [Link], Chennai-17
24323605, 9444037410 [Link] Page 10 of 16
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 - Login Application | [Link]</title>
</head>
<body>
<h2>Struts 2 - Login Application</h2>
<s:actionerror />
<s:form action="[Link]" method="post">
<s:textfield name="username" key="[Link]" size="20" />
<s:password name="password" key="[Link]" size="20" />
<s:submit method="execute" key="[Link]" align="center" />
</s:form>
</body>
</html>
[Link]
<%@ 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>
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<display-name>Struts2 Application</display-name>
<listener>
<listener-class>
[Link]
</listener-class>
</listener>
Mmcc Computer Centre, 38 Burkit Road, [Link], Chennai-17
24323605, 9444037410 [Link] Page 11 of 16
<context-param>
<param-name>tilesDefinitions</param-name>
<param-value>/WEB-INF/[Link]</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
[Link]
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>[Link]</welcome-file>
</welcome-file-list>
</web-app>
[Link]
Mmcc Computer Centre, 38 Burkit Road, [Link], Chennai-17
24323605, 9444037410 [Link] Page 12 of 16
<?xml version="1.0" encoding="UTF-8" ?>
Mmcc Computer Centre, 38 Burkit Road, [Link], Chennai-17
24323605, 9444037410 [Link] Page 13 of 16
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"[Link]
<struts>
<constant name="[Link]"
value="false" />
<constant name="[Link]" value="false" />
<constant name="[Link]"
value="ApplicationResources" />
<package name="default" extends="struts-default" namespace="/">
<action name="login" method="authenticate"
class="[Link]">
<result name="success">[Link]</result>
<result name="error">[Link]</result>
</action>
</package>
</struts>
Run the Project
Mmcc Computer Centre, 38 Burkit Road, [Link], Chennai-17
24323605, 9444037410 [Link] Page 14 of 16
Mmcc Computer Centre, 38 Burkit Road, [Link], Chennai-17
24323605, 9444037410 [Link] Page 15 of 16
Output :
Mmcc Computer Centre, 38 Burkit Road, [Link], Chennai-17
24323605, 9444037410 [Link] Page 16 of 16