Meaningless Drivel is fun!*
The moose likes Struts and the fly likes Struts Flow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts Flow" Watch "Struts Flow" New topic
Author

Struts Flow

Pankaj Shet
Ranch Hand

Joined: Sep 08, 2006
Posts: 227

Hello Friends,
Can any one explain me the detailed flow of Struts?right from the url entry in the browser by the client to the response it gets.?


PANKAJ SHET
B.Sc.(I.T.), S.C.J.P., S.C.W.C.D., PGDAC(CDAC)
maha lakshmi
Greenhorn

Joined: Feb 22, 2008
Posts: 7
find the below Url
http://www.roseindia.net/struts/
Devi Sri
Ranch Hand

Joined: Dec 20, 2005
Posts: 114

Hi,

Struts flow of execution is like this:

1. You would enter an url in the browser.

2. Then, from the url and messageresources.properties file, it will identify the path to the action class, and the method name.

3. Then, after execution of the method, it will take the action mapping from the method, and be forwarded to the specified path in the struts-config.xml file.

4. In the struts-config.xml file, it will find the appropriate jsp file through the mapping.findforward name, and executes that jsp file.

Hope you got it.

Regards,
--Devisri.


Devisri, SCJP 5.0, SCWCD 5.0
"Dream is Not what you see in sleep. Dream is that which never lets you sleep" - Abdul Kalam
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you're talking about Struts 1, the following link gives a good summary of the flow in Struts using UML diagrams. Pay particular attention to the sequece diagram.

http://rollerjm.free.fr/pro/Struts11.html


Merrill
Consultant, Sima Solutions
Pankaj Shet
Ranch Hand

Joined: Sep 08, 2006
Posts: 227

thanks a lot for all the replies, but where does that messageresources.properties file lie??
where does the actionServlet lie??
Pankaj Shet
Ranch Hand

Joined: Sep 08, 2006
Posts: 227

when i enter the url in the browser, it will be .do page?
suppose it is login.do.

this then goes to the Url-Pattern :
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>?
after that it maps to the actionServlet using this:
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
</servlet>

in the Web.Xml file?
then it goes to struts-config.xml file using :
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
after it goes to struts-config.xml

the .do page will be mapped to the path attribute in the

<action-mappings>
<action input="/LoginForm.jsp" name="LoginActionForm" path="/login"
scope="request" type="com.myapp.struts.LoginAction" validate= "true">
<forward name="success" path="/Cust_Reg.jsp"/>
</action>
<action-mappings>?

now this /path attribute in <action input="/LoginForm.jsp" name="LoginActionForm" path="/login"
scope="request" type="com.myapp.struts.LoginAction" validate= "true">

should the same as

<html:form action="login">in the <action input="/LoginForm.jsp"> so that this will show the LoginForm.jsp?

after i enter the login id and password in this page these values will be stored in LoginActionForm formbean?
if validations are to be performed,they will be performed in the validate method of the form bean itself?

after the validations are performed, it will go to
<type="com.myapp.struts.LoginAction> action class where the business logic is to be performed and the forwarding decisions are made?
after this if success then it will forward to cust_reg.jsp using <forward name="success" path="/Cust_Reg.jsp"/>?

please explain me where i am correct and where i am wrong and then interpret the full flow in this way.?

Thanks and Regards,
-pankaj
Pankaj Shet
Ranch Hand

Joined: Sep 08, 2006
Posts: 227

if required, iam pasting those files over here.:

LoginForm.jsp is as follows:


<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>


<!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>LoginForm</title>
</head>
<body bgcolor="blue">

<h1>LoginForm</h1>
<html:form action="login" focus="name" >
<table border="1" align="center">
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><bean:message key="login.name" /></td>
<td><html:text property="name" /></td>
</tr>
<tr>
<td><bean:message key="login.password"/></td>
<td><html assword property="password"/></td>
</tr>
<tr>
<td><html:submit value="Login" /></td>
<td><html:reset/></td>
</tr>
</tbody>
</table>
<html:errors />

</html:form>


</body>
</html>



LoginAction.java:

package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;


public class LoginAction extends Action {

private final static String CANCEL = "cancel";
private final static String SUCCESS = "success";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
if (isCancelled(request)){
return mapping.findForward(CANCEL);
}


return mapping.findForward(SUCCESS);

}
}

LoginActionForm

package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;


public class LoginActionForm extends org.apache.struts.action.ActionForm {

private String name;
private String password;

public String getName() {
return name;
}

public String getPassword(){
return password;
}


public void setName(String string) {
name = string;
}

public void setPassword(String Password){
password = Password;
}

public LoginActionForm() {
super();
// TODO Auto-generated constructor stub
}

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ((getName() == null || getName().length() < 1)||(getPassword()== null || getPassword().length()<1)) {
errors.add("name", new ActionMessage("error.name.required"));

}

return errors;

}
}

struts-config file:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">


<struts-config>
<form-beans>
<form-bean name="DelActionForm" type="com.myapp.struts.DelActionForm"/>
<form-bean name="LoginActionForm" type="com.myapp.struts.LoginActionForm"/>
<form-bean name="RegActionForm" type="com.myapp.struts.RegActionForm"/>
</form-beans>

<global-exceptions>

</global-exceptions>

<global-forwards>
<forward name="welcome" path="/welcome.do"/>

</global-forwards>

<action-mappings>
<action input="/LoginForm.jsp" name="LoginActionForm" path="/login" scope="request" type="com.myapp.struts.LoginAction" validate= "true">
<forward name="success" path="/Cust_Reg.jsp"/>
</action>
<action input="/Cust_Reg.jsp" name="RegActionForm" path="/Cust_Reg" scope="request" type="com.myapp.struts.RegAction" validate="true">
<forward name="success" path="/Del_Dest.jsp"/>
</action>
<action input="/Del_Dest.jsp" name="DelActionForm" path="/Del_Dest" scope="request" type="com.myapp.struts.Del_DestAction" validate="true"/>
<forward name="success" path="/Del_Dest.jsp"/>
<action/>
<action path="/welcome" forward="/welcomeStruts.jsp"/>
</action-mappings>

<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

<message-resources
parameter="com/myapp/struts/ApplicationResource"/>

<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

</struts-config>


Web.Xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config><session-timeout>
30
</session-timeout></session-config><welcome-file-list><welcome-file>
index.jsp
</welcome-file></welcome-file-list></web-app>

Thanks and Regards,
Pankaj
Pankaj Shet
Ranch Hand

Joined: Sep 08, 2006
Posts: 227

In the above mentioned files,
what is the
1.Struts Controller or ActionServlet?
2.RequestProcessor?
3.Action?
4.ActionForm?
5.Business Delegate?
6.Helper?
7.View?

It's my humble request to make me understand them so that i can move ahead.
Thanks and Regards,
-Pankaj
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

Pankaj,
Have you read a book or Struts tutorial yet? This is a difficult question to answer without writing an article length reply.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Pankaj Shet
Ranch Hand

Joined: Sep 08, 2006
Posts: 227

Yeah , I know that it is a difficult question to answer,but actually i don't want the meaning of those terms,but i want the explaination of the flow and the location of message-resources,Struts Controller or ActionServlet,RequestProcessor.
and i just want to know
the
1.Struts Controller or ActionServlet?
2.RequestProcessor?
3.Action?
4.ActionForm?
5.Business Delegate?
6.Helper?
7.View?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

Pankaj,
Repeating the question doesn't make it shorter to answer.

The Struts manual has three paragraphs about the page view including the location and several pages on the flow.

Try to answer your questions from the manual and then post more specifically which ones you still have questions on - including what you understand about it so far. Seven general questions requires an essay to answer.
sitaram
Greenhorn

Joined: Jan 24, 2008
Posts: 26
There is form.It as three fields and one button.client will enter the three fields and press the submit button. at the time,the request goes to the controller. The controller reads the Structs-config.xml.The controller will check where exactly matches the request that related form bean ,action class will be executed.then related forward page will be display on the browser.
Pankaj Shet
Ranch Hand

Joined: Sep 08, 2006
Posts: 227

Hello every one,
Thanks to all those who tried to help me...
I have got answer, i.e flow of struts.
but i am not able to guess where the ActionServlet and ProcessController lie.
if i ll be able to actually access those files so that i can know the contents of the same, i would be able to understand in detail.
I refered to the journal from javaranch
web page
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

The ActionServlet is in the default package of struts "org.apache.struts.action.ActionServlet". Download the struts-src.zip to view the class.
Pankaj Shet
Ranch Hand

Joined: Sep 08, 2006
Posts: 227

thanks a lot anubhav
 
jQuery in Action, 2nd edition
 
subject: Struts Flow