Struts 替代 Spring MVC 模型

发布于 2024-09-30 16:05:30 字数 246 浏览 2 评论 0原文

嘿, 我正在使用 Spring MVC,并且习惯于“发送”DTO 到视图而不直接填充 servlet 请求,这是非常灵活和有效的。我正在尝试弄清楚如何在 Struts 中执行类似的操作,因为据我所知,“发送”DTO 进行查看的唯一方法是通过请求调度程序,其中 servlet 请求以键值样式填充 DTO由程序员手动操作。

与 Spring MVC 相比,这导致视图层 (JSP) 中有太多逻辑。

这是 DTO 从处理程序传输到视图层的唯一方法吗?

Hey,
I'm using Spring MVC and I got used to "sending" DTO to a view without populating servlet request directly, which is very flexible and effective. I'm trying to figure out how to do something similar in Struts, because as far as I can see, the only way how to "send" DTO to view is via request dispatcher where servlet request is populated with DTOs in key - value style manually by programmer.

This results in having too much logic in view layer (JSPs) in comparison with Spring MVC.

Is this the only way of DTO transfer from handlers to a view layer ?

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

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

发布评论

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

评论(1

小伙你站住 2024-10-07 16:05:30

一般来说,我在 Struts 中看到的完成方式是通过 Form 对象。该表单类扩展了 ActionForm。然后在 struts-config.xml 中将此表单定义为表单 bean。然后在操作类 bean 定义中添加对表单 bean 的引用。然后在 jsp 中重新引用表单以从 DTO 获取数据。

例如:-

The Action class:

public class SomeAction extends DispatchAction {
 public ActionForward someRequest(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
            throws Exception {
  SomeForm someForm = ( SomeForm) form;
  List<SomeDTO> someList = populateDto();
  someForm.setSomeList(someList);
  return mapping.findForward("someAction");

}
The Form class:

public class SomeForm extends ActionForm{

   List<SomeDTO> someList;
   //getter and setters for someList
}

StrutsConfig:

<form-beans>
    <form-bean name="someForm" type="my.forms.SomeForm" />
</form-beans>

<action path="/someRequest"
            type="my.actions.SomeAction"
            name="someForm" scope="request" >
<forward name="someAction" path="goesSomeWhere" />

</action>

View:

<c:forEach items="${someForm.someList}" var="someThing" varStatus="someCounter">
    <c:out value="${someThing.foo}" /> <!-- assuming foo is a member in SomeThing DTO -->
</c:forEach>

Generally the way I have seen it being done in Struts is via Form objects. This form class extends ActionForm. Then in the struts-config.xml you define this form as a a form bean. Then add a reference to the form bean in the action class bean definition. Then in the jsp you rerefence the form to get data from the DTO.

For example:-

The Action class:

public class SomeAction extends DispatchAction {
 public ActionForward someRequest(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
            throws Exception {
  SomeForm someForm = ( SomeForm) form;
  List<SomeDTO> someList = populateDto();
  someForm.setSomeList(someList);
  return mapping.findForward("someAction");

}
The Form class:

public class SomeForm extends ActionForm{

   List<SomeDTO> someList;
   //getter and setters for someList
}

StrutsConfig:

<form-beans>
    <form-bean name="someForm" type="my.forms.SomeForm" />
</form-beans>

<action path="/someRequest"
            type="my.actions.SomeAction"
            name="someForm" scope="request" >
<forward name="someAction" path="goesSomeWhere" />

</action>

View:

<c:forEach items="${someForm.someList}" var="someThing" varStatus="someCounter">
    <c:out value="${someThing.foo}" /> <!-- assuming foo is a member in SomeThing DTO -->
</c:forEach>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文