Struts 2 + Spring

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    Struts 2 + Spring - Presentation Transcript

    1. Struts 2 + Spring Aug 19th, 2009 Bryan Hsueh
    2. 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
      • Other thoughts
    3. Layer Architecture
      • Client :
        • Presentation
      • Application Server :
        • Business Logic
      • Database :
        • Data Modeling
    4. Spring Modules Core Struts or Spring MVC Context Web AOP JMS JCA DAO ORM (Hibernate) JMX … … DB browser
    5. Model-View-Controller Controller Model DB Business Service Object Front Controller Servlet Controller View JSP Browser Server API getXyz()
    6. 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
    7. Struts 2 DB * One way of interpreting Struts 2 MVC. Why put Action in Model?
    8. 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
    9. In struts.xml <action name=“user_add&quot; class=&quot; xxx.User &quot; method=&quot; add &quot;> <result>/jsp/user.jsp</result> <result name=“error”>/jsp/error.jsp</result> </action> <action name=“user_delete&quot; class=&quot; xxx.User &quot; method=“ update &quot;> <result>/jsp/user.jsp</result> </action> <action name=“user_login&quot; class=&quot; xxx.User &quot; method=“ login &quot;> <result name=“ success ”>/jsp/user.jsp</result> <result name=“ error ”>error.jsp</result> </action> <action name=“user_ * &quot; class=&quot; xxx.User &quot; method=“ {1} &quot;> <result>/jsp/user.jsp</result> <result name=“error”>/jsp/error.jsp</result> </action>
    10. 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
    11. 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
    12. In struts.xml <action name=“user_add&quot; class=&quot; xxx.UserAction &quot; method=&quot; add &quot;> <result>/jsp/user.jsp</result> <result name=“ error ”>/jsp/error.jsp</result> </action> <action name=“user_delete&quot; class=&quot; xxx.UserAction &quot; method=“ update &quot;> <result>/jsp/user.jsp</result> </action> <action name=“user_login&quot; class=&quot; xxx.UserAction &quot; method=“ login &quot;> <result name=“ success ”>/jsp/user.jsp</result> <result name=“ error ”>error.jsp</result> </action> <action name=“user_ * &quot; class=&quot; xxx.UserAction &quot; method=“ {1} &quot;> <result>/jsp/user.jsp</result> <result name=“error”>/jsp/error.jsp</result> </action>
    13. 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)
    14. 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
    15. 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
    16. 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
    17. Other Toughts
      • Struts vs Spring MVC?
      • Web content: static vs dynamic
        • Apache: html, image, css, JavaScript, Flash, doc, etc.
        • Web container: JSP
      • Deployment and Operation
      • Development environment
    SlideShare Zeitgeist 2009

    + Bryan HsuehBryan Hsueh Nominate

    custom

    556 views, 1 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 556
      • 556 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 39
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories