从高层次来看,struts2 是如何工作的?我来自 mvc 背景
从高层次来看,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
典型的 Struts2 工作流程(请记住,Struts2 的可配置性极高,其各个部分都很好地解耦)
struts.xml
=>定义 'mappings' :action
results
:哪个资源(通常是 JSP)为操作返回的每个结果生成视图因此,例如,假设
struts.xml
包含并且您的 Java 操作是:
然后请求
http://mysite.com/mywebapp/add.action?x=10&y=20< /code> 将使 Struts2 实例化一个
SumAction
对象,设置x
和y
属性并调用execute
方法。如果返回“成功”,那么它将将该操作放置在某个“范围”中,转发到“/SumResult.jsp”,其中通常使用一些 struts2 标签来显示结果,并将其从操作对象中拉出。当然,在不太重要的场景中,
execute()
方法将调用服务层。因此,目前还不清楚该操作是控制器还是控制器+模型,我会说后者,因为它不仅具有处理请求的逻辑,而且还充当数据(输入和结果)的容器。但仅限于请求范围内。
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.
我一直的理解是,行动是你的控制器。您点击一个 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.