5.11.08

Spring integrate Struts

ข้อดีของการใช้ Spring ติดต่อกับ Struts
ได้ข้อดีของ 2 framework รวมกันคือการได้ใช้ syntax การ mapping request ของ struts และดึงเอาจุดเด่นของ spring มาใช้คือ Dependency Injection และ AOP มาใช้

วิธีการทำมี 2 วิธีใหญ่ๆ
  • วิธี1 จัดการผ่าน Spring ด้วยการใช้ ContextLoaderPlugin กำหนด Dependency ใน Spring context file ซึ่งจะแบ่งออกได้ 2 วิธี
      • Override Struts default RequestProcessor ด้วย Spring DelegatingRequestProcessor
      • Delegate action management ไปที่ Spring
    • ก่อนอื่นเราต้องเปลี่ยน request process ที่ใช้ใน struts ไปใช้ตัวนี้ของ Spring แทนโดยการ override ค่าเดิมก่อนแล้วนำไปใช้กับสองวิธีข้างต้นโดยไปแก้ที่ struts-config.xml ดังนี้







    • จากนั้นหาหา library Spring-Struts ไว้ใช้งานในนี้ทำ Spring2.5 ซึ่งจะอยู่ใน package ที่โหลดมาที่ dist\modules\spring-webmvc-struts.jar แล้วดึงเขามาใช้ในโปรเจค

    • ถ้าใช้วิธี Override Struts default RequestProcessor ด้วย Spring DelegatingRequestProcessor เราก็เขียน mapping url กับ Struts Action Class ใน struts-config.xml ตามปกติ





    • เมื่อมี request มาจาก /registerPersonalAction.do ตัว DelegatingRequestProcessor จะทำการอ้างถึงตัว applicationContext และหา bean name /registerPersonalAction แต่เราต้องเข้าไป config ไว้ก่อนใน applicationContext.xml











    • จบพร้อมทำงาน
    • ถ้าใช้วิธี Delegate action management ไปที่ Spring วิธีนี้เป็นวิธีที่ดีที่สุดเพราะเราสามารถนำเอาข้อดีของ Spring AOP มาใช้งานได้
    • ถ้าเราไม่สามารถใช้วิธี DelegatingRequestProcessor หรือ DelegatingTilesRequestProcessor เราก็สามารถใช้วิธีนี้ได้ โดยการเขียนที่ struts-config.xml




    • จากนั้นไปเขียนใน applicationContext.xml










    • จบการทำงาน
  • วิธี2 ให้ Struts Action ไปสืบทอด ActionSupport classes และใช้ Spring จัดการ bean ด้วยการใช้ getWebApplicationContext() method

Reference:
http://static.springframework.org/spring/docs/2.5.x/reference/web-integration.html#struts
http://www.ibm.com/developerworks/java/library/j-sr2.html

No comments:

Post a Comment