概括地说,struts2 是如何工作的?我来自 mvc 背景 [英] At a high level, how does struts2 work? I'm coming from a mvc background

查看:19
本文介绍了概括地说,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 对特定 url 的响应?

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 包含

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>

您的 Java 操作是:

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";
       }
   }

然后请求 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天全站免登陆