When using SSH(Struts+Spring+Hibernate), data transform and transfer is painful, usually, from ActionForm(Web) --> VO(Service) --> PO(DAO) .... --> PO --> VO --> ActionForm. As for EJB2, it is very similar that you have to convert ActionForm(or whatever other objects depending on what web layer framework you use) into DTO(Data Transfer Object) and then into Entity Bean.
However, that is the cost of layering your application, which makes your system more flexible and loosely coupled.
The problem is when using the above architectures, there is too much duplication of classes and its “getter and setter” and too much simple “copy” of code. For system maintenance and extension, it’s a nightmare.
So I would say your problem is not only about passing data from service layer into DAO layer, but also about transferring data between different layers in system.
The solution probably is using POJO and new web frameworks and new Persistence APIs.
If you want to continue to use Struts and Spring, using Struts2+spring+JPA is your choice. If you are going to adopt EJB, using JSF+EJB3 is your choice. The key of both architectures is their using POJO(+Annotation) as the vehicle of data transfer and manipulation.
For example, JSF components can be directly bound to POJO’s field; JPA EntityManager uses POJO+Annotion as Entity Bean to manipulate data in database. Struts2 also discarded the ActionForm and uses POJO as HTML input or output value binding.
So put it simple, forget VO,DTO,PO,ActionForm, only use POJO(+Annotation),which is also called “domain objects”, you will find that the system becomes so clean and no any duplication for data transfer at all.
Here is a very good example for Strut2+Spring+JPA:http://cwiki.apache.org/S2WIKI/struts-2-spring-2-jpa-ajax.html.
Anyway, you may still worry about using one single POJO in every layer; it probably makes the system tightly coupled. But in fact it does not. You have to change the way you regard the “domain objects”(POJO+Annotation), that move between different layers of the system,carring data, being presented, changed, saved into database, etc. These “domain objects” are the blood of the system, and can be used by Web Framework, Business Logic code, Persistence API. These “domain objects” almost only contain data and its getters and setters, or some small logic for conversion of data. They are the only artifacts created, used, changed, transferred by the components in every layers in the system.
As for JPA, don’t worry about it at all, it just absorbs all the essence of Hibernate and uses Annotation instead of hbm.xml. If you are familiar with Hibernate, it will not take you long time to master it.
Another issue is DAO layer seems to disappear. Yes, if you use JPA, write another layer of DAO seems to be meaningless. In my point of view, DAO is only useful when you use JDBC or a very big system with multi-layer even in business logic code. If you has already use ORM tool such as Hibernate, JPA, it really doesn’t give much benefit for the system, unless it’s a very big system.
I hope this will give you little help.
分享到:
相关推荐
-- 定义Form Bean --> </form-beans> <global-exceptions> <!-- 全局异常处理 --> </global-exceptions> <action-mappings> <!-- 定义Action --> ...
2. **<form-beans>**:配置 ActionForm 对象,每个 `<form-bean>` 元素表示一个 ActionForm 类实例,例如: ```xml <form-beans> <form-bean name="myForm" type="com.example.MyForm"/> </form-beans> ``` 3. ...
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-...
-- 配置Struts --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> ... </servlet> <servlet-mapping> <servlet-name>action...
--> ``` 实例(二)和实例(三)展示了如何创建一个简单的输入页面,其中`<html:html>`和`<html:form>`标签用于创建HTML表单,`<html:text>`用于创建输入字段,而`<html:submit>`则定义了一个提交按钮。这些标签都...
-- Struts actionServlet mapping --> <servlet-mapping> <servlet-name>actionServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- welcome file list --> <welcome-file-list>...
在MyEclipse中,选择`File` -> `New` -> `Project`,然后选择`Web Project`。在弹出的对话框中输入工程名称并点击`Finish`。这会生成一个基础的Web项目结构。 接下来,我们需要为项目添加Struts支持。右键点击刚...
-- 定义表单 bean --> </form-beans> <action-mappings> <!-- 定义 Action 映射 --> </action-mappings> </struts-config> ``` 3. **使用 FilterDispatcher**:Struts 1 使用 FilterDispatcher 作为其前端控制...
- `<form-beans>`:用于配置多个 `ActionForm`,包括一个或多个 `<form-bean>` 子元素。每个 `<form-bean>` 都有一个 `name` 属性来标识该 `ActionForm`,还有一个 `type` 属性来指定 `ActionForm` 类的全限定名称。...
例如,`<s:form>`、`<s:textfield>`等标签用于构建表单,`<s:actionexecute>`用于调用Action。 4. **国际化与本地化支持**:`struts-i18n-x.x.x.jar`包含国际化和本地化资源,使得应用能够根据用户的语言和地区提供...
<form-bean name="myForm" type="actionform.MyActionForm"/> </form-beans> ``` 2. **配置Action映射** ```xml <action-mappings> ...
-- login.jsp --> <form action="login.do" method="post"> <input type="text" name="username" placeholder="Username" /> <input type="password" name="password" placeholder="Password" /> ...
<h1>((HelloForm) request.getAttribute("helloForm")).getMessage() %></h1> </body> </html> ``` **步骤七:运行项目** 最后,在Eclipse中右键点击项目,选择“Run As” > “Run on Server”,确保服务器已经...
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> ...
-- Action 映射等配置 --> </struts-config> ``` - `<!DOCTYPE>`声明了DTD(Document Type Definition),这里指定了使用的是Struts 1.3版本的DTD。 - `<struts-config>`标签是根元素,里面可以包含各种子元素,如`...
-- Spring 上下文配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <listener> <listener-...
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 业务逻辑处理,如从数据库获取电影信息 // ... /...
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { if (!ServletFileUpload....