aspose file tools*
The moose likes Struts and the fly likes is Action class in struts called as controller in mvc ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "is Action class in struts called as controller in mvc ?" Watch "is Action class in struts called as controller in mvc ?" New topic

is Action class in struts called as controller in mvc ?

Ratna Raju

Joined: Mar 30, 2012
Posts: 3

my doubt is :
in struct application is Action class is called as Controller ?
and if Action class is called controller then what about Action Servlet ?
friends please give a clarity about this..

and one more thing is in MVC design pattern how many controllers are used? i mean more than one ?
please give me clarity...

Joe Ess

Joined: Oct 29, 2001
Posts: 8912

Have a look at the Struts Documentation and let us know if that answers your questions.

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
A Struts-based Controller is a "component" consisting of many parts. Custom Action classes are written by application developers. Struts ActionServlet is provided by the framework. A struts-config.xml configuration file is written by application developers. The code that reads this file and creates Action**** objects is provided by the framework. All of these elements together are the "Controller."

For more information about the Struts Action package, see API docs below:
sahil nanda

Joined: Apr 11, 2012
Posts: 2
Hi, Please do not confused yourself with servlets and controllers. controller is a concept, servlet is a physical implementation.

in struts 2 a single servlet model in implemented. the action classes are called from within that single servlet.

e.g. we can have 3 different pages doing different action functionalities.... client 1 cleint 2 and client 3 ... all call the same servlet... but all have 3 different action classes assosiated with them... which are being called from that single servlet.

Please correct me if i am wrong.
Swetha Bhagavathula
Ranch Hand

Joined: Jan 04, 2011
Posts: 112

Ratna Raju wrote:my doubt is :
in struct application is Action class is called as Controller ?
and if Action class is called controller then what about Action Servlet ?
friends please give a clarity about this..

and one more thing is in MVC design pattern how many controllers are used? i mean more than one ?
please give me clarity...


hi..It's "struts" not structs... please don't mind hope you correct it....
struts is application framework software which supports only MVC2 architecture,used to develop medium and large projects. And moreover java based web application development is divided into two types of achitectures:
1.Model-1 architecture:either servlets components or JSP components will be used as the main web resource programs of web application containing multiple logics.
2.model-2(MVC architecture):this is further divided into MVC-1 and MVC-2 architecture.In mVC-1 ,MVC -2architecture multiple technlogies(like JSps,Servlet(only one servlet acting as controller that is ActionServlet ),EJB,java programs,ActoinForward,Action,Form Bean class all these and more also taken to develop various logics of different layers.
The MVC2 architecture gives more clean seperation of logics by having multiple layers compared to Model-I architecture.
see the flow:

1)Browser window gives the requests to MVC2 architecture.
2)Controller- servlet traps and takes the request and reads related data.
3)Controller-servlet uses integration logic,to pass the request to model layer resource.
4)+5)Model layer resource uses Business logic to process request and to generate results.In this process,it uses persistance logic to interact with database software if necessary.
6)The results given by persistance logic go to Controller-serve;t from Model layer resource.
7)controller-servlet once again uses integration logic to pass the results to the view layer JSP program(component).
8)the presentation logic of view layer JSP program formats the results by using presentation logic and sends the response to browser window in the form of dynamic web page.

the above steps are the flow that takes place in MVC2 architecture..
if we consider struts ,it is completely developed based on MVC2 architecture...

in order to understand how many controllers are there in MVC2 architecture.. (of course only one that is ActionServelt class) first one sholud understand what happens if inter change the roles of controller , view ,model layer components..?can I?If so what happens?

(why I am saying is javaranch is the best site that helped me to know many things so, if I post that may be usefull for any me people also.. so please take the things which you require and ignore rest of them..)

while developing MVC2 architecture based website,servlet is recommended as Controller,why not JSP componenets are recomemended as view layer resources and ect..?what happens if the roles are changed?

reasons are as follows:
we can interchange but not recommended

if servlet component is takes as view layer resource(for presentation logic)

1)the presentation logic of web application generally changes at regular intervals.If presentation logic is placed in servlet program,for every modification,we need to recompile the servlet program and we need to re-load the web application,which is quite an issue.

Moreover modifications done in JSP programs will be affected without recompile of JSP program and without re-loading of the web application.So,JSP programs are recommended to use as view layer resource...........

if JSP program is taken as controller..(for Integration logic ,form validation,session management and etc)

keeping java code in JSP program is not industry standard because,it makes JSP code as more and more complex and kills the readability of programs.Writing only tag based code in JSP programs is industry standard
When JSp program is taken as controller,the program will be forced to add java code representing integration logic, to talk with model layer resource like EJB components ,because , the built-in tags supplied by JSP technology are not at all sufficient to develop the integration logic.For this reason it is recommended to take servlet program as controller where there is no restriction to place java code...

If EJB program /Spring /hibernate is taken as controller or view layer resources..

It is completely wrong to think as above because ,the controller,view layer resources must deal with http /https requests ,responses.
since ejb component ,java class,spring,hibernate applications are not http based web components so they can't deal with request and responses.

If business logic is placed in servlet/jsp program the following problems occur:
middleware services ust be implemented by the programmer manually.
Businnes logic becomes specific to one web application because servelt,jsp programs reside inside the web application(kills the re-usability of the business logic) can't be used by multiple web applications.
servelt /jsp programs allows only http request to accept the business logic (that means normal java applications like AWT applications can't access the business logic)

to overcome these problems ejb,springs,hibernate technologies are used to deveop business logic.

hope now you got the actual purpose of differrent componenets roles in differrent layer of MVC2 architecture..

if we consider the point
in strut application is Action class is called as Controller ?
and if Action class is called controller then what about Action Servlet ?

ActionServlet is a built-in HttpServlet type servlet program of struts 1.x-api.
It acts as controller servlet of struts 1.x web application ,having the ability to generate integration logics of strts application dynamically.
It reads struts configuration file entries to genrate integration logics in this helper class called RequestProcessor class is used(which is pre-defined).ActionServelt is responsible to trap the coming request from browserwindow to struts application ,to pass the trapped request from clients to Model layer resources,to gather results from Model layer resources and to pass the results to view layer resources.
The java class that extends from "org.apache.struts.action.Action" is called as Action class.when programmer direclty places businees ,persitnace logic in Action class then Action class comes under Model layer resource.

if programmer places logics to interact with Model layer resource like ejb components,spring applications,spring with hibernates application then Action class comes under controller layer resource(note it is not treated still as controller but controller layer resource).moreover, ,Action class acts as helper reource for the Controller servlet called ActionServlet .In small scales struts application we can use Action class as model layer and in medium and large web applications we can use Action class as controller layer resource..

need of taking Action class or struts Action class as controller layer reource...

According to MVC2 architecture,the controller servlet is responsible to interact with model layer resource.In struts application,the controller is ActionServlet and is pre-defined servelt.So,programmers can't edit and palce logics in ActionServlet to communicate with model layer resources.
To overcome this problem,ActionServlet tries to pass every trapped request to the programmer supplied java class called struts Action class ,so that programmers can place logics to interact with model layer resource in this struts Action class with depending on ActionServet.ActionServlet also using ActionForward class to identify the view layer resource configured in struts configuration file.. hence though ActionServlet is treated as Controller it takes help of underlying many classes but creating objects to those classes ,trapping requesting etc all these done by ActionServlet but not those helper classes...

for clarity please see the attachment

[Thumbnail for flow of execution in strut 1.x or architecture of struts 1.jpg]

wood burning stoves
subject: is Action class in struts called as controller in mvc ?