从高层次来看,struts2 是如何工作的?我来自 mvc 背景

发布于 2024-09-09 12:13:25 字数 132 浏览 3 评论 0原文

从高层次来看,struts2 是如何工作的?我来自 mvc 背景,

在查看示例项目时,我看到了这些 ___action 类型类的分配。

它只是对控制器操作的操作引用吗?即基于 get/post 对特定 url 的响应?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑凤梨 2024-09-16 12:13:25

典型的 Struts2 工作流程(请记住,Struts2 的可配置性极高,其各个部分都很好地解耦)

struts.xml =>定义 'mappings' :

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

因此,例如,假设 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< /code> 将使 Struts2 实例化一个 SumAction 对象,设置 xy 属性并调用 execute方法。如果返回“成功”,那么它将将该操作放置在某个“范围”中,转发到“/SumResult.jsp”,其中通常使用一些 struts2 标签来显示结果,并将其从操作对象中拉出。

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

当然,在不太重要的场景中,execute() 方法将调用服务层。

因此,目前还不清楚该操作是控制器还是控制器+模型,我会说后者,因为它不仅具有处理请求的逻辑,而且还充当数据(输入和结果)的容器。但仅限于请求范围内。

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.

守望孤独 2024-09-16 12:13:25

我一直的理解是,行动是你的控制器。您点击一个 url,将其映射到一个操作,您的操作控制发生的业务逻辑,例如“加载域对象”或“仅转到 jsp”。

jsp是你的观点。我认为,如果您正在构建一个 RIA,其中操作处理 xhrs 并返回 json,那么 MVC 的 V 组件就在框架之外——它是您的 extjs 或您用于演示的任何内容。

Struts 一直看起来就像一个真正的 VC 框架,因为您不用 struts 定义您的模型。你自己做。尽管您在 struts 中使用模型,但当您将模型转换为演示文稿需要的任何内容时。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文