Layer architecture: Presentation, Business Logic, DB
Spring framework
Model-View-Controller
Struts 2
Case A: Simple POJO
Case B: Decouple web layer (Action) from Service
Case C: Use Service and DTO
Integration between layers
Other thoughts
Layer Architecture
Client :
Presentation
Application Server :
Business Logic
Database :
Data Modeling
Spring Modules Core Struts or Spring MVC Context Web AOP JMS JCA DAO ORM (Hibernate) JMX … … DB browser
Model-View-Controller Controller Model DB Business Service Object Front Controller Servlet Controller View JSP Browser Server API getXyz()
Model-View-Controller
Provide a loose coupling between View and Model, which can make application significantly easier to create and maintain.
View:
Presentation: JSP, Tablib, Tiles, Validation, etc.
Controller:
application flow is mediated by a central/front controller
front controller delegates requests to a handler/controller/action
each handler acts as an adapter between the request and the Model
control is usually then forwarded to appropriate View
Model:
represents, or encapsulates, an application's business logic or state
provide business APIs to be used by upper layers
Struts 2 DB * One way of interpreting Struts 2 MVC. Why put Action in Model?
A Simple POJO (case A)
public String add () {
....
return “success”;
}
public String update () {
....
return “success”;
}
public String login () {
....
if (ok) return “success”;
else return “error”;
}
}
public class User { private String name; private String gender; private Date birth; public String getName() { return name; } void setName(String s) { name = s; } public Date getBirth() { ... } void setBirth(Date d) { ... } public String getGender() { ... } void setGender(String s) { ... } Could use simple POJO as Action in Struts 2
Service and DTO (case C) public class UserAction extends ActionSupport { private AccountService accountService; private User user; public AccountService getAccountServic () { return user; } void setAccountServic (AccountService s) { accountService = s; } public User getUser () { return user; } void setUser (User u) { user = u; } public String add () { accountService.add(user); .... return “success”; } public String update () { accountService.update(user); .... return “success”; } public String login () { accountService.login(user); .... if (ok) return “success”; else return “error”; } } Use Business Service and DTO (Data Transfer Object)
Service and DTO
public class AccountService {
public String add(User u) {
....
}
public String update(User u) {
....
}
public String login(User u) {
....
}
}
public class User { private String name; private String gender; private Date birth; public String getName() { return name; } void setName(String s) { name = s; } public Date getBirth() { ... } void setBirth(Date d) { ... } public String getGender() { ... } void setGender(String s) { ... } } DTO: simple Java bean, reduce overhead Business Service Object: - implement business logic and provide service API to upper layers - managed by container - usually fewer service objects than DTOs - use Data Access Object (DAO) to get data (DTO) from DB
Web and Business Layers DB Business Service Object Web Service, XML-RPC, etc. * Business layer should not tie to web layer framework. Spring MVC Struts
Integration between layers
Client: Presentation
struts.xml
For simple case, can configure business service object as Action
Most likely will need to implement ActionSupport class and invoke business service and server APIs
Server: Business Logic
applicationContext.xml
Provide business service object and APIs for upper layers
Should not tie to any frontend framework
Use Data Access Object (DAO) to access data
DB: Data Modeling
hibernate.xml
DAO, DTO
Other Toughts
Struts vs Spring MVC?
Web content: static vs dynamic
Apache: html, image, css, JavaScript, Flash, doc, etc.
0 comments
Post a comment