您的位置:首页 > 编程语言 > Java开发

Spring 整合Struts的三个小窍门(二)

2008-11-06 13:28 399 查看
窍门 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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: