从总体上讲,struts2是如何工作的?我来自MVC背景 [英] At a high level, how does struts2 work? I'm coming from a mvc background

查看:92
本文介绍了从总体上讲,struts2是如何工作的?我来自MVC背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从总体上讲,struts2是如何工作的?我来自mvc背景

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

看一个示例项目,我看到了这些___action类型类的分配.

Looking at a sample project, I see allot of these ___action type classes.

这只是一个引用控制器动作的动作吗?即基于get/post对特定网址的响应?

Is it just a action references to a controller action? i.e. a response to a particular url based on get/post?

推荐答案

典型的Struts2工作流程(请记住,Struts2具有很高的可配置性,其各部分之间的耦合性很好)

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

struts.xml =>定义映射":

  • 为每个URL执行的action
  • 一个或多个results:哪个资源(通常是JSP)为操作返回的每个结果生成视图
  • 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

因此,例如说struts.xml包含

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

您的Java操作是:

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

然后,请求http://mysite.com/mywebapp/add.action?x=10&y=20将使Struts2实例化SumAction对象,设置xy属性并调用execute方法.如果返回成功",那么它将把动作放在某个作用域"中,前进到"/SumResult.jsp",其中通常使用一些struts2标签显示结果,并将其从动作对象中拉出.

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>

当然,在不太普通的情况下,execute()方法将调用服务层.

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.

这篇关于从总体上讲,struts2是如何工作的?我来自MVC背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆