3 cách tích hợp Struts và Spring

Mỗi cách tích hợp đều tốt, tùy theo sở thích mỗi người. Tôi thì chỉ thích có một, nhưng thực hiện chúng thì còn tùy vào lượng kiến thức của bạn về Spring và Struts:
Dùng Spring's ActionSupport class.
Khai báo Struts RequestProcessor cho Spring's DelegatingRequestProcessor
Kết hợp Struts Action quản lý Spring framework
Load application context
Bạn có thể dùng Spring ContextLoaderPlugin để load Spring application context cho Struts ActionServlet. Đơn giản bằng cách khai báo plug-in trong struts-config.xml:
"org.springframework.web.struts.ContextLoaderPlugIn">
"contextConfigLocation" value="/WEB-INF/application-context.xml"/>


Cách 1. Dùng Spring's ActionSupport
Tạo một Spring context là một cách trực quan nhất để kết hợp Struts với Spring. Việc này thì khá đơn giản, vì Spring hỗ trợ tất cả. Đối tượng org.springframework.web.struts.ActionSupport cung cấp phương thức getWebApplicationContext() để dễ dàng lấy Spring context. Bạn chỉ cần extend action do Spring cung cấp là Spring's ActionSupport thay cho Struts Action:Hình 1. Dùng ActionSupport để kết hợp
package ca.nexcel.books.actions;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.springframework.context.ApplicationContext;
import org.springframework.web.struts.ActionSupport;

import ca.nexcel.books.beans.Book;
import ca.nexcel.books.business.BookService;

public class SearchSubmit extends ActionSupport { (1)


public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

DynaActionForm searchForm = (DynaActionForm) form;
String isbn = (String) searchForm.get("isbn");

//the old fashion way
//BookService bookService = new BookServiceImpl();

ApplicationContext ctx =
getWebApplicationContext(); (2)
BookService bookService =
(BookService) ctx.getBean("bookService"); (3)

Book book = bookService.read(isbn.trim());

if (null == book) {
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError
("message.notfound"));
saveErrors(request, errors);
return mapping.findForward("failure") ;
}

request.setAttribute("book", book);
return mapping.findForward("success");
}
}

Dòng (1), Tôi tạo một Action bằng cách extends ActionSupport thay cho Action.
Dòng (2), Tôi dùng phương thức getWebApplicationContext() để gọi ApplicationContext.
Lấy business service, Tôi dùng context (2) tìm trong context (3).
Theo đánh gái của tôi thì kỹ thuật này đơn giản và dễ hiểu. Tuy nhiên, nó gắn liền Struts action với Spring framework. Nều sau đó bạn không dùng Spring, bạn cần viết lại code. Hơn nữa, Struts action không nằm dưới sự quản lý của Spring, nó không thể thừa hưởng những đặc tính của Spring AOP.

Cách 2. Khai báo Struts RequestProcessor cho Spring DelegatingRequestProcessor
Tách Spring action và truts action là một chọn lựa tốt. Một cách để làm nó là khai báo Struts RequestProcessor cho org.springframework.web.struts.DelegatingRequestProcessor,:Hình 2. Kết hợp bằng cách khai báo Sping's DelegatingRequestProcessor


"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">



type="org.apache.struts.validator.DynaValidatorForm">














type="ca.nexcel.books.actions.SearchSubmit"
input="/searchEntry.do"
validate="true"
name="searchForm">








(1)


value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>









Ở đây, Tôi sử dụng thẻ để khai báo Struts RequestProcessor cho DelegatingRequestProcessor.
Bước tiếp theo là khai báo action trong Spring config:Hình 3. Khai báo action torng Spring config

"http://www.springframework.org/dtd/spring-beans.dtd">




class="ca.nexcel.books.actions.SearchSubmit"> (1)






Chú ý dòng (1), Tôi khai báomột bean sử dụng thuộc tính name trùng với tên của action được khai báo trong struts-config. Hình 4. Struts action
package ca.nexcel.books.actions;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import ca.nexcel.books.beans.Book;
import ca.nexcel.books.business.BookService;

public class SearchSubmit extends Action {

private BookService bookService;
public BookService getBookService() {
return bookService;
}

public void setBookService(BookService bookService) { (1)
this.bookService = bookService;
}

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

DynaActionForm searchForm = (DynaActionForm) form;
String isbn = (String) searchForm.get("isbn");

Book book = getBookService().read(isbn.trim()); (2)

if (null == book) {
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("message.notfound"));
saveErrors(request, errors);
return mapping.findForward("failure") ;
}

request.setAttribute("book", book);
return mapping.findForward("success");
}

}

Hình 4, bạn thấy sẽ thấy cách tạo một Struts action. Dòng (1), khai báo khởi tạo một thuộc tính.
Cách thiế kế này thì Struts được quản lý bởi Spring, nó cho phép bạn sử dụng các lợi điểm của Spring.

Cách 3. Ủy nhiệm quản lý action cho Spring
Một giải pháp tốt hơn là ủy nhiệm việc quản lý Struts action sang cho Spring framework. Bạn có thể làm bắng cách khai báo proxy trong struts-config.xml. Proxy nàyse4 chụi trách nhiệm tìm Struts action đó trong Spring context. Vì action nằm dưới sự quản lý của Spring, nên ta có thể sử dụng tất cả các lợi điểm của Spring ví dụ như: Spring's AOP:Hình 5. Ủy nhiệm trong tích hợp Spring



"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">



type="org.apache.struts.validator.DynaValidatorForm">














type="org.springframework.web.struts.DelegatingActionProxy" (1)
input="/searchEntry.do"
validate="true"
name="searchForm">










property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>



className="org.springframework.web.struts.ContextLoaderPlugIn">






Hình 5 là một ví dụ điển hình. Thay vì khai báo một action class, thì ta lại khai báo Spring proxy (1). Class DelegatingActionProxy dùng tên của action mapping để tìm đến một action trong Spring context. Và dĩ nhiên context này phải được khai báo trong struts context (ContextLoaderPlugIn).
Khai báo một Struts action đơn giản như khai báo một Spring bean, như ta thấy trong Hình 6:Hình 6. Khai báo Struts action trong Spring context

"http://www.springframework.org/dtd/spring-beans.dtd">




class="ca.nexcel.books.actions.SearchSubmit">







Lợi điểm
Đây là giải pháp tốt nhất. Struts action không biết đến Spring và có thể dùng trong một ứng dụng non-Spring mà không cần thay đổi code. Không phải ủy nhiệm thay đổi RequestProcessor, và bao gồm cả các lợi điểm của Spring's AOP.Không chỉ có thế, vì Struts action được sự quản lý của Spring, bạn có thể phát triển một ứng dụng Spring. Bắng cách khai báo các thuộc tính trong trong thẻ , để sử dụng thế mạnh cuả Spring lifecycle. Ví dụ, thuộc tính init-method trong thẻ được sử dụng để run khi Struts action được khởi tạo. Tương tự đối với thuôc tính destroy-method khai báo phương thức sẽ chạy trước khi bean được loại ra khỏi container.

Cao Trong Hien

,

1 Response to "3 cách tích hợp Struts và Spring"

lúc 18:05 1 tháng 11, 2007
tren trang jsp get message error ??

Đăng nhận xét