Set up your tools for development.
- Download Eclipse IDE for Java EE Developers
- Download apache-tomcat 6 Server
- Download Struts 2 jar files
After downloading all those tools, extract them. I presume that you already installed Java 6.
Create helloWorld directory structure.
- Open eclipse IDE.
- Create a Dynamic Web Project.
File > New > Other > Dynamic Web Project > Next
Type "Sample" on Project Name and click Finish
- Copy necessary Struts 2 jar files to lib Folder.
commons-fileupload-1.2.1.jar, commons-logging-1.1.jar, freemarker-2.3.12.jar,
ognl-2.6.11.jar, struts2-core-2.1.6.jar, xwork-2.1.2.jar
- Create necessary files and folders.
> Sample
> Java Resources: src
>> sample(package)
>>> HelloWorld.java
>> struts.xml
> WebContent
>> pages
>>> helloWorld.jsp
>> WEB-INF
>>> lib
>>>> commons-fileupload-1.2.1.jar
>>>> commons-logging-1.1.jar
>>>> freemarker-2.3.12.jar
>>>> ognl-2.6.11.jar
>>>> struts2-core-2.1.6.jar
>>>> xwork-2.1.2.jar
>>> web.xml
>>index.html
Configure your web.xml by adding the following code.
<?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>Sample Struts 2</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</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>
Configure your struts.xml adding the following code below.
<?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"/>
<package name="sample" namespace="/sample" extends="struts-default">
<action name="HelloWorld" class="sample.HelloWorld">
<result>/pages/helloWorld.jsp</result>
</action>
</package>
</struts>
Code your helloWorld.java by adding the following code below.
package sample;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute() throws Exception {
setMessage("Hello World!");
return SUCCESS;
}
}
Code your index.html by adding the following code below.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World Application</title>
</head>
<body>
<div align=left><font color="#00000">To your information this is your <b>index.html</b> page under WebContent directiory.</font></div>
<br><br><br>
<a href="sample/HelloWorld.action">Click Here</a>
</body>
Code your helloWorld.jsp by adding the following code below.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<div align=left><font color="#00000">To your information this is your <b>helloWorld.jsp</b> page under WebContent directiory.</font></div>
<br>
<br>
<center><h1><s:property value="message"/></h1></center>
</body>
</html>
Run the helloWorld Application on a Tomcat Server.
- Left Click on the Servers tab on eclipse IDE.
- Right Click > New > Server
- Select Tomcat v6.0 Server under Apache and Left Click Next.
- Browse the Tomcat installation directory.
- Select java-6-sun-1.6.0.07 and Left Click Finish.
- Left Click Project.
- Go to Run > Run As > Run on Server
That's it! Eclipse web browser will appear on your IDE with your project up and running. Congratulations! You just created your first Struts 2 application.


Delicious
Digg
Google
Yahoo