The Greeting Application.
When a request comes to the container, it checks web.xml to decide how it should be dispatched. Here is the web.xml for our “Greeting” Application
<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>Struts2 Test </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>
On line numbers 3-6 , we are enabling struts2 filter. We map this filter to all urls on line numbers 7 through 10.ie all requests are passed through struts2 FilterDispatcher. The dispatcher looks in to the struts2.xml file for any matching mappings. Here is our struts2.xml.
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="HelloWorld">
<result>helloWorld.jsp</result>
</action>
<action name="Greet" class="org.techienet.tutorials.struts2.helloworld.Greet">
<result name="greet">/greet.jsp</result>
</action>
</package>
</struts>
line # 6-8 creates the HelloWorld Action (HelloWorld.action), that we used in the previous example. But we didn't give any class attribute to the action tag. if no class is defined for an action, struts assumes the action returns “SUCCESS”. We 'll discuss about the return values later. The Greeting action (Greet.action) is configured on line # 9 through 11. In this case we are giving a class attribute, meaning , we are going to create our own class for this action.
Now Let's try creating a struts2 Action class. Usually we sub class ActionSupport for creating our own Action class. Here is the code.
package org.techienet.tutorials.struts2.helloworld;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class Greet extends ActionSupport {
setMessage("Welcome to Struts2 Actions !");
return "greet";
}
return message;
}
public void setMessage
(String message
) {
this.message = message;
}
}
When struts finds “class” attribute for an action mapping, it create an instance of the class, and invokes it's execute method. In our action class we have one property apart from the execute method. In the execute method we are setting this property, to the greeting message. We can access this property from the jsp file using the tag library suplied by the struts2 package.
Note the value that we are returning from the execute method. it's the string "greet" !!!. On line number 10 of the struts2.xml we are configuring a result with the same name. that's what is telling struts to pick up the file "/greet.jsp" and merge it to the response.
Pretty Simple , Eh ?
Here is our "/greet.jsp".
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>Insert title here</title>
</head>
<body>
<h1> Hello World ! , through Action ...</h1>
<s:property value="message"/>
</body>
</html>
Comments
Good tutorial
Hi Sarath
Thanks for a nice tutorial... Though I got these errors that mentioned in couple of comments but get it fixed..Request all to take care of following
1) Struts2.xml may or may not work, try to rename it to struts.xml
2) Whenever xml file is added to a new project in eclipse (struts.xml/struts2.xml) and Eclipse auto deploy the war on JBoss/Tomcat make sure that the file you add is present in deployed war.
Build Path
This second example only worked for me once I'd changed the output folder in the build path back to the default ('HelloWorld/build/classes'). I think the instuction on the first exercise to set it to 'HelloWorld/WebContent/classes' was in error.
struts2
rajesh.v
91+9349791324
getting an error report when trying to run the given application
error:There is no Action mapped for namespace / and action name Greet. - [unknown location]
im trying to give namespace as default but no change
Need more info
Rajesh,
It's very difficult to guess what went wrong there. Can you send me the stack trace ? You can see that in the eclipse Console.Also make sure, you extended namespace from 'struts-default'
--Sarath PS
Rajesh, Try to move
Rajesh,
Try to move struts.xml to []ProjectRoot]\WebContent\classes.
Regards,
Dian Alamanda
System Analyst
XSIS Mitra Utama
Facing same problem
I am also facing the same problem. Your first example HelloWorld.action is working but not this one. Only one change i made in struts2.xml instead of your classname+package name in class syntax. Any clue?
Aug 5, 2007 2:02:28 PM org.apache.struts2.dispatcher.Dispatcher serviceAction
SEVERE: Could not find action or result
There is no Action mapped for namespace / and action name Greet. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:186)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:41)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:494)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
Hai Jitesh, I apologies
Hai Jitesh,
I apologies for the late response
You can fix this issue by moving struts.xml to your src folder from WEB-INF
Thanks
Laseelan PM
Struts file is missnamed
The example says to create file named Struts2.xml. It should be Struts.xml