struts 2 + spring

16
Struts 2 + Spring Aug 19th, 2009 Bryan Hsueh

Upload: bryan-hsueh

Post on 10-May-2015

4.378 views

Category:

Technology


1 download

DESCRIPTION

Use Struts 2 along with Spring, and use Hibernate as DAO and ORM.

TRANSCRIPT

Page 1: Struts 2 + Spring

Struts 2 + Spring

Aug 19th, 2009Bryan Hsueh

Page 2: Struts 2 + Spring

Agenda

• 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

Page 3: Struts 2 + Spring

Layer Architecture

• Client : – Presentation

• Application Server : – Business Logic

• Database : – Data Modeling

Page 4: Struts 2 + Spring

Spring Modules

Core

Strutsor

SpringMVC Context

Web

AOP JMS JCA

DAO

ORM(Hibernate)

JMX

……

DBbrowser

Page 5: Struts 2 + Spring

Model-View-Controller

Controller Model

DBBusinessServiceObject

FrontController

ServletController

ViewJSP

Browser

Server API

getXyz()

Page 6: Struts 2 + Spring

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

Page 7: Struts 2 + Spring

Struts 2

DB

* One way of interpreting Struts 2 MVC. Why put Action in Model?

Page 8: Struts 2 + Spring

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

Page 9: Struts 2 + Spring

In struts.xml<action name=“user_add" class="xxx.User" method="add">

<result>/jsp/user.jsp</result><result name=“error”>/jsp/error.jsp</result>

</action> <action name=“user_delete" class="xxx.User" method=“update">

<result>/jsp/user.jsp</result> </action> <action name=“user_login" class="xxx.User" method=“login">

<result name=“success”>/jsp/user.jsp</result><result name=“error”>error.jsp</result>

</action>

<action name=“user_*" class="xxx.User" method=“{1}"> <result>/jsp/user.jsp</result><result name=“error”>/jsp/error.jsp</result>

</action>

Page 10: Struts 2 + Spring

The Action in Struts 2• The Action class actually plays more role as Controller.

– Handle the interaction between View and Model– Use the data and APIs from Model (business service)– Heavily coupled with the view page (navigation logic)

• (new in Struts 2) Can also add properties, to be used by View.– Combine ActionForm (in Struts 1) in the Action

• (new in Struts 2) Could use POJO (model) as Action– Works with simple object. But …– Heavy coupling of navigation logic in each function

• Forward to different view: also in action mapping (struts.xml)• Not easy to manage the coupling between layers

• Business layer should not tie to the web layer framework.– In most cases, Action will extend from ActionSupport class

Page 11: Struts 2 + Spring

ActionSupport class (case B)

public class UserAction extends ActionSupport

{

private User 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) { ... }

public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable

public String add() {

user.add();

....

return “success”;

}

public String update() {

user.update();

....

return “success”;

}

public String login() {

user.login();

....

if (ok) return “success”;

else return “error”;

}

}

Decouple business service from web layer

Page 12: Struts 2 + Spring

In struts.xml<action name=“user_add" class="xxx.UserAction" method="add">

<result>/jsp/user.jsp</result><result name=“error”>/jsp/error.jsp</result>

</action> <action name=“user_delete" class="xxx.UserAction" method=“update">

<result>/jsp/user.jsp</result> </action> <action name=“user_login" class="xxx.UserAction" method=“login">

<result name=“success”>/jsp/user.jsp</result><result name=“error”>error.jsp</result>

</action>

<action name=“user_*" class="xxx.UserAction" method=“{1}"> <result>/jsp/user.jsp</result><result name=“error”>/jsp/error.jsp</result>

</action>

Page 13: Struts 2 + Spring

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)

Page 14: Struts 2 + Spring

Service and DTOpublic 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 overheadBusiness 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

Page 15: Struts 2 + Spring

Web and Business Layers

DB

BusinessServiceObject

Web Service, XML-RPC, etc.

* Business layer should not tie to web layer framework.

Spring MVC

Struts

Page 16: Struts 2 + Spring

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