Writing JSP, Java and Configuration for Hello World Application
In this section we will write JSP, Java and required configuration files for
our Struts 2 Hello World application. Now in struts 2 struts.xml is used
to configure the applications.
Understanding the application
Our application is very simple application that
displays Hello World message along with current date and time of the server.
When user clicks on the "Run Struts 2 Hello World Application"
link on the tutorial home page, a request is sent to the struts framework. Then
struts framework sends the input to the action class (in our case Struts2HelloWorld.java).
After action is fired the Result selects the resource "/pages/HelloWorld.jsp"
to render the response.
In this example we have to develop three parts view,
Action class and mapping (struts.xml) to couple action and page. By creating
these three components we are separating the application in three parts View,
Model and Controller.
Developing View:
This page is used to display the result on the browser. The HelloWorld.jsp is
view part of our application. Create "HelloWorld.jsp" in the struts2tutorial\pages
directory and add the following content:
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts 2 Hello World Application!</title>
</head>
<body>
<h2><s:property value="message" /></h2>
<p>Current date and time is: <b><s:property value="currentTime" /></b>
</body>
</html>
|
The line <%@ taglib prefix="s" uri="/struts-tags" %>
declares data tag library of struts. The struts data tag is used to display
the dynamic data. The tag <s:property value="message" /> and
<s:property value="currentTime" /> calls
the methods getMessage() and getCurrentTime() respectively of the Struts2HelloWorld
action class and merges the values with response.
Developing Action (to interact with Model):
Now create Struts2HelloWorld.java and saves it
to the "struts2tutorial\WEB-INF\src\java\net\roseindia"
directory. This action class creates the message to be displayed on the screen.
Here is the code of Struts2HelloWorld.java:
package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
public class Struts2HelloWorld extends ActionSupport {
public static final String MESSAGE = "Struts 2 Hello World Tutorial!";
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;
}
public String getCurrentTime(){
return new Date().toString();
}
}
|
Developing Controller Configuration File:
Struts 2 uses the struts.xml file for
configuring the application. Create struts.xml file and save it in the
"struts2tutorial\WEB-INF\src" directory with the following
content.
<?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="true" />
<package name="roseindia" namespace="/roseindia" extends="struts-default">
<action name="HelloWorld" class="net.roseindia.Struts2HelloWorld">
<result>/pages/HelloWorld.jsp</result>
</action>
<!-- Add actions here -->
</package>
<!-- Add packages here -->
</struts> |
The struts.xml file should be present in the class path
of the application, you can either include it in the jar and place in the lib
directory of the application or place it in the classes directory of the web
application. In our application we are using ant build tool which is including
it in the jar file.
Building the application
I am assuming that you have already installed ant build
tool on your machine. Since we are using the ant built tool, building application
by using is very easy. To build the application open command prompt and go to
"struts2tutorial\WEB-INF\src" directory of the web application and
issue the "ant" command. The ant build tool will compile the java file
and create jar file "struts2tutorial.jar" into the lib
directory of your web application. Here is the output of ant build tool:
C:\apache-tomcat-6.0.10\apache-tomcat-6.0.10\webapps\struts2tutorial\WEB-INF\src
>ant
Buildfile: build.xml
clean:
[delete] Deleting directory C:\apache-tomcat-6.0.10\apache-tomcat-6.0.10\webapps\struts2tutorial\WEB-INF\classes
[mkdir] Created dir: C:\apache-tomcat-6.0.10\apache-tomcat-6.0.10\webapps\struts2tutorial\WEB-INF\classes
prepare:
resources:
compile:
[javac] Compiling 1 source file to C:\apache-tomcat-6.0.10\apache-tomcat-6.0.10\webapps\struts2tutorial\WEB-INF\src\classes
[jar] Building jar: C:\apache-tomcat-6.0.10\apache-tomcat-6.0.10\webapps\struts2tutorial\WEB-INF\lib\struts2tutorial.jar
project:
all:
BUILD SUCCESSFUL
Total time: 7 seconds
C:\apache-tomcat-6.0.10\apache-tomcat-6.0.10\webapps\struts2tutorial\WEB-INF\src
> |
Testing Struts 2 Hello World Application
In the above section we have compiled our application and now finally we will
test our application. To test the application start the tomcat server and type http://localhost:8080/struts2tutorial/
and then select "Run Struts 2 Hello World Application" from the list.
Here is the screen shot of our struts 2 tutorial home page:
Select "Run Struts 2 Hello World Application" link. Our first
application "Struts 2 Hello World" will look like the following:
The application will display message "Struts 2
Hello World Tutorial!" along with current date and time of the server.
How application works?
Here is the brief description on how Struts 2 Hello
World Application works:
Your browser sends a request to the web server for the URL http://localhost:8080/tutorial/HelloWorld.action.
- When you click on the "Run Struts 2 Hello World Application"
link, the browser sends a request for the url http://localhost:8080/struts2tutorial/roseindia/HelloWorld.action.
The container requests for the resource "HelloWorld.action". By
default web.xml file of struts blank application is configured to route all
the request for *.action through
org.apache.struts2.dispatcher.FilterDispatcher. Here is the configuration
from web.xml file:
<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>
- Then the framework looks for the mapping for the
action "HelloWorld" and then framework instantiates the
appropriate class and calls the execute method. In this case action class is
Struts2HelloWorld. Here is the configuration file from struts.xml which
defines the action mapping:
<action name="HelloWorld"
class="net.roseindia.Struts2HelloWorld">
<result>/pages/HelloWorld.jsp</result>
</action>
- Then the execute method sets the message and returns
SUCCESS.
public String execute() throws Exception {
setMessage(MESSAGE);
return SUCCESS;
}
Then framework determines which page is to be loaded if SUCCESS is returned.
In our case framework tells the container to load HelloWorld.jsp and render
the output.
In the struts 2 framework Actions are used to process the form and user
request. The execute method of the action returns SUCCESS, ERROR, or INPUT
value. Then based on these values framework tells the container to load and
render the appropriate result.
- Container process the HelloWorld.jsp and generates
the output.
- Then the output in the HTML format is sent to the
browser.
Download the application and source code:
You can download the application and source code of the tutorial and to save
your time and efforts spent on configuring the application. Click
here to download the code.
Our goal is to provide the working skeleton of the
application based on ant tool. So you can download the application and then
use ant tool to compile the java classes. Application is in the exploded
directory structure and can be deployed as it is on tomcat server. If any of
our visitor finds and problem please discuss it. We request you to share your
experiences with us.
|
Current Comments
57 comments so far (post your own) View All Comments Latest 10 Comments:Hi Andrew
I tried the solution posted by you to remove the namespace entry in the package tag. I still see the same error. Was anyone else successful in resolving this error?
Thanks
Krishna
Posted by Krishna on Wednesday, 05.7.08 @ 22:41pm | #58819
every thing is good but implement extra features in jdbc it not enough so pls do this .
Posted by praveen on Wednesday, 05.7.08 @ 17:24pm | #58797
Make sure you stop tomcat then remove the jar file you built ..
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\struts2tutorial\WEB-INF\lib
then restart with the required change and your good to go
Posted by Will on Thursday, 05.1.08 @ 20:38pm | #58316
The solution was posted above:
This was set as /roseindia in the struts.xml file:
<package name="roseindia" namespace="/roseindia" extends="struts-default">
Instead, do this:
<package name="roseindia" extends="struts-default">
Posted by Andrew on Thursday, 05.1.08 @ 13:18pm | #58280
Change the index.html file it will work. the tutorial doesnt say that explicitly. The file is available at the source code struts2tutorial.zip
http://www.roseindia.net/struts/struts2/struts2tutorial.zip
Posted by Anuradha on Thursday, 04.17.08 @ 01:27am | #56746
can some body explain why Iam getting the following error
"SEVERE: Could not find action or result
There is no Action mapped for action name testAction. - [unknown location]".
I guess there is lot of mismatch with the jars that apache published, and there is no solution either form apache.Iam running a very simple application in MyEclipse6,if any body know the answer pl do post the answer..it saves lots of time
Thanks
Posted by prashanth on Wednesday, 04.2.08 @ 08:18am | #55088
I wonder why the ant doesn't build the war file so that I can work within Eclipse and deploy the war file to TomCat webapp directory, instead of working in TomCat webapp directory.
Only jar file is built but war is mention in 'dist' target.
Posted by Michael Wan on Monday, 03.24.08 @ 09:51am | #54030
I tried the below change, but I'm still getting the same error. Does this thing work?
Posted by Joshua Glickman on Monday, 03.24.08 @ 03:56am | #54006
I found the solution. The problem is the namespace.
This was set as /roseindia in the struts.xml file:
<package name="roseindia" namespace="/roseindia" extends="struts-default">
Instead, do this:
<package name="roseindia" extends="struts-default">
(which means the namespace will default to ""), which is what is needed.
Posted by bill edwards on Thursday, 03.20.08 @ 19:47pm | #53489
Trying to follow this tutorial but I too get
# Messages: There is no Action mapped for action name HelloWorld.
Stacktraces
There is no Action mapped for action name HelloWorld. - [unknown location]
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:186)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:41)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:494)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
java.lang.Thread.run(Thread.java:619)
If anyone can help, please let me know at darrenkane@hotmail.com
Posted by Darren Kane on Friday, 02.29.08 @ 16:25pm | #50715