ssh學習(一)struts-spring整合

使用 Spring 的 ContextLoaderPlugin 爲 Struts 的 ActionServlet 裝載 Spring 應用程序環境。在struts-config.xml中加入以下代碼:
	<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="classpath*:modules/**/struts-spring-conf/*.xml" />
</plug-in>

1. Action 中,使用IOC 獲得服務,配置struts-config.xml
	<!-- 一個Action 注意其type Spring 代理類 -->
<action path="/searchSubmit"
type="org.springframework.web.struts.DelegatingActionProxy"
input="/searchEntry.do" validate="true" name="searchForm">
<forward name="success" path="/WEB-INF/pages/detail.jsp" />
<forward name="failure" path="/WEB-INF/pages/search.jsp" />
</action>

2. Spring 配置文件中註冊該動作
	<bean id="bookService"
class="ca.nexcel.books.business.BookServiceImpl" />
<bean name="/searchSubmit"
class="ca.nexcel.books.actions.SearchSubmit">
<property name="bookService">
<ref bean="bookService" />
</property>
</bean>

3. 寫具有 JavaBean 屬性的 Struts 動作
public class SearchSubmit extends Action {
// 一個Service屬性
private BookService bookService;

// getter...
public BookService getBookService() {
return bookService;
}

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

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// 調用bookService,不需要new
Book book = getBookService().read(isbn.trim());
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章