At a high level, how does struts2 work? I'm coming from a mvc background
Looking at a sample project, I see allot of these ___action type classes.
Is it just a action references to a controller action? i.e. a response to a particular url based on get/post?
Typical Struts2 workflow (bear in mind that Struts2 is extremely configurable, its parts are well decoupled)
struts.xml
=> defines 'mappings' :action
is executed for each URLresults
: which resource (typically a JSP) generates the view for each result returned by the actionHence, for example, say a
struts.xml
containsAnd your Java action is:
Then the request
http://mysite.com/mywebapp/add.action?x=10&y=20
would make Struts2 to instantiate aSumAction
object, set thex
andy
properties and call theexecute
method. If "success" is returned, then it will place the action in some "scope", forward to "/SumResult.jsp" in which typically one use some struts2 tag to show the result, pulling it from the action object.Of course, in less trivial scenarios the
execute()
method would call the service layer.So, it's not very clear if the action is controller or controller+model, I'd say the later, because it not only has the logic to process the request but also acts as a container of the data (input and result). But only during the scope of a request.
The way I always understood it is that actions are your controllers. You hit a url, you map it to an action, your action controls what business logic happens, like "load domain object" or "just go to jsp".
The jsps are your views. I think if you are building a RIA where actions are handling xhrs and returning json, The V component of MVC is outside the framework -- its your extjs or whatever you are using for presentation.
Struts has always seemed like really a VC framework, because you don't define your model with struts. You do that yourself. Although you use the model in struts, when you convert your model to whatever your presentation needs.