Hi,

Jay SenSharma

Jay SenSharma

Here is a Very simple WebApplication which uses Struts 2.0 Framework With 2-Action Classes.
To run this Application inyour machine you need to have Struts2.0 Libraries..which is included in the following JARs…

ognl-2.6.11.jar
freemarker-2.3.8.jar
struts2-core-2.0.14.jar
xwork-2.0.7.jar
you may additionally require commons-logging-1.0.4.jar

Step1). Developing ActionSupport class “ExampleSupport.java

package example;
import com.opensymphony.xwork2.ActionSupport;
public class ExampleSupport extends ActionSupport {
}

Step2). “HelloWorldA.java”

package example;
public class HelloWorldA extends ExampleSupport {
public String execute() throws Exception {
setMessage("HelloWorld--A This is coming from execute()--->setMessage()");
return SUCCESS;
}

public static final String MESSAGE = "HelloWorld.message";
private String message;
public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}

Step3). “HelloWorld.java”

package example;
public class HelloWorld extends ExampleSupport {

public String execute() throws Exception {
setMessage("This is coming from execute()--->setMessage()");
return SUCCESS;
}

public static final String MESSAGE = "HelloWorld.message";

private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}

Step4). “example.xml”

<?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>
<package extends="struts-default">
<action>
<result>/example/HelloWorld.jsp</result>
</action>
<action>
<result>/example/HelloWorldA.jsp</result>
</action>
</package>
</struts>

Step5). Developing “struts.xml

<?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 value="false" />
<constant value="false" />
<include file="example.xml"/>
</struts>

Step 6). Writing “index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Struts</title>
<META HTTP-EQUIV="Refresh" CONTENT="1;URL=HelloWorld.action">
</head>

<body>
<BR><BR><BR><BR><center><h1>LOADING...<h1><BR>
<img src=loading.gif />
</body>
</html>

Step7). Writing “HelloWorldA.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title><s:text/>--A</title>
</head>
<body>
<h2> <marquee>This is HelloWorld----A</marquee>
</body>
</html>

Step 8). Writing “HelloWorld.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title><s:text/></title>
</head>
<body>
<h1><center><s:text/>
<form action="HelloWorldA.action">
<input value="Show Another JSP"/>
</form>
</body>
</html>

Step 9). Place all your Jars in “WEB-INF/lib“.

Step10). Place HelloWorld.jsp and HelloWorldA.jsp inside the Root Of WEBApplicationexample

Step 11). Place “struts.xml” and “example.xml” files inside “WEB-INF/classes

Step 12). Provide the “web.xml” file Like Below:

<?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 Blank</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-list>
</web-app>

Deploy the Applicaiotn on the Server..

.

.

Thanks

Jay SenSharma

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.