窍门 2. 覆盖 RequestProcessor
1. Action 中,使用IOC 获得服务,配置struts-config.xml:
<!-- 一个普通的Action-->
<action path="/searchSubmit"
type="ca.nexcel.books.actions.SearchSubmit"
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>
<!-- 利用了 <controller> 标记来用 DelegatingRequestProcessor覆盖默认的
Struts RequestProcessor -->
<controller
processorClass="org.springframework.web.struts.
DelegatingRequestProcessor" />
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());
}
}