HelloWorld in Struts 2.0
IDE: Eclipse (Version: 3.4.0)
Struts2: Version 2.0.11.2
Web Server: Apache Tomcat 6.0.18
Step 1) Create a new "Dynamic Web Project" using File -> New -> Dynamic Web Project.
Project Name: Struts2Demo
Context Root: Struts2Demo
Content Directory: WebContent
Java Source Directory: src
Step 2) Copy the following jars from Struts' lib[C:\struts-2.0.11.2\lib] to the Web-Project's lib directory (WebContent/WEB_INF/lib) [Minimum required set]
commons-logging-.jar
freemarker-.jar
ognl-.jar
struts2-core-.jar
xwork-.jar
Step 3) Create a new JSP file (HelloWorld.jsp):
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>
<title>Hello World!</title>
</head>
<body>
<h1>This is a test!</h1>
<h2><s:property value="message" /></h2>
</body>
</html>
Step 4) Create the action class (HelloWorld):
package com.struts2demo.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
public static final String MESSAGE = "Struts is up and running ...";
public String execute() throws Exception {
setMessage(MESSAGE);
return SUCCESS;
}
private String message;
public void setMessage(String message){
this.message = message;
}
public String getMessage() {
return message;
}
}
Step 5) Modify web.xml:
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Struts2Demo</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>HelloWorld.jsp</welcome-file>
</welcome-file-list>
</web-app>
Step 6) Create struts.xml in classpath (here in java source "src" folder):
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2demo" extends="struts-default">
<action name="HelloWorld" class="com.struts2demo.action.HelloWorld">
<result>/HelloWorld.jsp</result>
</action>
<!-- Add your actions here -->
</package>
</struts>
Step 7) Deploy the application to tomcat and start the server:
http://localhost:8080/Struts2Demo/ : you can see HelloWorld.jsp (with no message from server)
Now try,
http://localhost:8080/Struts2Demo/HelloWorld.action
you can see message "Struts is up and running ..." which was set in HelloWorld.java
This article has moved now to new my new BLOG.
Struts2: Version 2.0.11.2
Web Server: Apache Tomcat 6.0.18
Step 1) Create a new "Dynamic Web Project" using File -> New -> Dynamic Web Project.
Project Name: Struts2Demo
Context Root: Struts2Demo
Content Directory: WebContent
Java Source Directory: src
Step 2) Copy the following jars from Struts' lib[C:\struts-2.0.11.2\lib] to the Web-Project's lib directory (WebContent/WEB_INF/lib) [Minimum required set]
commons-logging-
freemarker-
ognl-
struts2-core-
xwork-
Step 3) Create a new JSP file (HelloWorld.jsp):
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>
<title>Hello World!</title>
</head>
<body>
<h1>This is a test!</h1>
<h2><s:property value="message" /></h2>
</body>
</html>
Step 4) Create the action class (HelloWorld):
package com.struts2demo.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
public static final String MESSAGE = "Struts is up and running ...";
public String execute() throws Exception {
setMessage(MESSAGE);
return SUCCESS;
}
private String message;
public void setMessage(String message){
this.message = message;
}
public String getMessage() {
return message;
}
}
Step 5) Modify web.xml:
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Struts2Demo</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>HelloWorld.jsp</welcome-file>
</welcome-file-list>
</web-app>
Step 6) Create struts.xml in classpath (here in java source "src" folder):
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2demo" extends="struts-default">
<action name="HelloWorld" class="com.struts2demo.action.HelloWorld">
<result>/HelloWorld.jsp</result>
</action>
<!-- Add your actions here -->
</package>
</struts>
Step 7) Deploy the application to tomcat and start the server:
http://localhost:8080/Struts2Demo/ : you can see HelloWorld.jsp (with no message from server)
Now try,
http://localhost:8080/Struts2Demo/HelloWorld.action
you can see message "Struts is up and running ..." which was set in HelloWorld.java
This article has moved now to new my new BLOG.
Labels: action class, Apache Tomcat, Eclipse, JSP, struts.xml, Struts2, Struts2 Demo, web.xml
0 Comments:
Post a Comment
<< Home