嘿, 我使用的是 Spring MVC,我习惯于在不直接填充 servlet 请求的情况下将 DTO“发送”到 View ,这是非常灵活和有效的。我试图弄清楚如何在 Struts 中做类似的事情,因为据我所知,如何“发送” DTO 以查看的唯一方法是通过请求调度程序,其中 servlet 请求以键值样式填充 DTO由程序员手动。
与 Spring MVC 相比,这导致 View 层 (JSP) 中的逻辑过多。
这是 DTO 从处理程序传输到 View 层的唯一方法吗?
最佳答案
一般来说,我所看到的在 Struts 中完成的方式是通过 Form 对象。这个表单类扩展了 ActionForm。然后在 struts-config.xml 中将此表单定义为表单 bean。然后在 Action 类 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>
关于java - Struts 替代 Spring MVC 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4108690/