In this example we will see how to create a simple helloworld application using Struts 2 framework
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 servlet 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 (HelloWorldAction.java)
package com.vinod.test;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() {
setName("Hello "+name+" Welcome to Struts 2");
return SUCCESS;
}
}
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() {
setName("Hello "+name+" Welcome to Struts 2");
return SUCCESS;
}
}
4. Create a JSP page (index.jsp)
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<s:form action="helloaction">
<s:textfield name="name" label="Enter Your Name" /><br>
<s:submit value="Submit" align="center" />
</s:form>
</body>
</html>
<html>
<body>
<s:form action="helloaction">
<s:textfield name="name" label="Enter Your Name" /><br>
<s:submit value="Submit" align="center" />
</s:form>
</body>
</html>
5. Create Success.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head></head>
<body>
<h4>
<s:property value="name" />
</h4>
</body>
</html>
<html>
<head></head>
<body>
<h4>
<s:property value="name" />
</h4>
</body>
</html>
6. 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="helloaction" class="com.pretech.HelloWorldAction">
<result name="success">/success.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="helloaction" class="com.pretech.HelloWorldAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
7. Deploy application in tomcat server
Run the application and give your name for input
No comments:
Post a Comment