Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
484 views
in Technique[技术] by (71.8m points)

model view controller - At a high level, how does struts2 work? I'm coming from a mvc background

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Typical Struts2 workflow (bear in mind that Struts2 is extremely configurable, its parts are well decoupled)

struts.xml => defines 'mappings' :

  • which action is executed for each URL
  • one or more results : which resource (typically a JSP) generates the view for each result returned by the action

Hence, for example, say a struts.xml contains

   <action name="add" class="example.SumAction">
     <result name="error">/Error.jsp</result>
     <result name="success">/SumResult.jsp</result>
   </action>

And your Java action is:

   public class SumAction { 
       private int x;
       private int x;
       private int z;
       // getters and setters ommited
       public String execute() {
           z = x + y; 
           return "success";
       }
   }

Then the request http://mysite.com/mywebapp/add.action?x=10&y=20 would make Struts2 to instantiate a SumAction object, set the x and y properties and call the execute 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.

 Result: <b><s:property value="z" /></b>

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...