|
<script language="JavaScript" type="text/javascript">
<!---->
</script>
<noscript>
</noscript>
|
<!----><!----> <!---->
|
级å«: ä¸çº§
George Franciscus (george.franciscus@nexcel.ca), è´è´£äºº, Nexcel
2005 å¹´ 10 æ 11 æ¥
Struts Recipes çåèè
George Franciscus å°ä»ç»å¦ä¸ä¸ªé大ç Struts æ´åçªé¨ ââ è¿æ¬¡æ¯å° Struts åºç¨ç¨åºå¯¼å
¥ Spring æ¡æ¶ã请è·é Georgeï¼ä»å°åæ¨å±ç¤ºå¦ä½æ¹å Struts å¨ä½ï¼ä½¿å¾ç®¡ç Struts å¨ä½å°±å管ç Spring beans é£æ ·ãç»ææ¯ä¸ä¸ªå¢å¼ºç web æ¡æ¶ï¼è¿ä¸ªæ¡æ¶å¯ä»¥æ¹ä¾¿å°å©ç¨ Spring AOP çä¼å¿ã <!----><!----><!---->
æ¨è¯å®å·²ç»å¬è¯´è¿æ§å¶å转 (IOC) 设计模å¼ï¼å 为å¾é¿ä¸æ®µæ¶é´ä»¥æ¥ä¸ç´å¨æµä¼ å
³äºå®çä¿¡æ¯ãå¦ææ¨å¨ä»»ä½åè½ä¸ä½¿ç¨è¿ Spring æ¡æ¶ï¼é£ä¹æ¨å°±ç¥éå
¶åççä½ç¨ãå¨æ¬æä¸ï¼æå©ç¨è¿ä¸åçæä¸ä¸ª Struts åºç¨ç¨åºæ³¨å
¥ Spring æ¡æ¶ï¼æ¨å°äº²èº«ä½ä¼å° IOC 模å¼ç强大ã
å°ä¸ä¸ª Struts åºç¨ç¨åºæ´åè¿ Spring æ¡æ¶å
·æå¤æ¹é¢çä¼ç¹ãé¦å
ï¼Spring æ¯ä¸ºè§£å³ä¸äºå
³äº JEE ççå®ä¸çé®é¢è设计çï¼æ¯å¦å¤ææ§ãä½æ§è½åå¯æµè¯æ§ï¼ççã第äºï¼Spring æ¡æ¶å
å«ä¸ä¸ª AOP å®ç°ï¼å
许æ¨å°é¢åæ¹é¢ææ¯åºç¨äºé¢å对象ç代ç ã第ä¸ï¼ä¸äºäººå¯è½ä¼è¯´ Spring æ¡æ¶åªæå¤ç Struts æ¯ Struts å¤çèªå·±å¥½ãä½æ¯è¿æ¯è§ç¹é®é¢ï¼ææ¼ç¤ºä¸ç§å° Struts åºç¨ç¨åºæ´åå° Spring æ¡æ¶çæ¹æ³åï¼å
·ä½ç±æ¨èªå·±å³å®ä½¿ç¨åªä¸ç§ã
æææ¼ç¤ºçæ¹æ³é½æ¯æ§è¡èµ·æ¥ç¸å¯¹ç®åçï¼ä½æ¯å®ä»¬å´å
·æææ¾ä¸åçä¼ç¹ãæ为æ¯ä¸ç§æ¹æ³å建äºä¸ä¸ªç¬ç«èå¯ç¨çä¾åï¼è¿æ ·æ¨å°±å¯ä»¥å®å
¨ç解æ¯ç§æ¹æ³ã请åé
ä¸è½½ é¨åè·å¾å®æ´ä¾åæºä»£ç ã请åé
åèèµæï¼ä¸è½½ Struts MVC å Spring æ¡æ¶ã
为ä»ä¹ Spring è¿ä¹äºä¸èµ·ï¼
Spring çåç«è
Rod Johnson 以ä¸ç§æ¹å¤çç¼å
çå¾
Java™ ä¼ä¸è½¯ä»¶å¼åï¼å¹¶ä¸æè®®å¾å¤ä¼ä¸é¾é¢é½è½å¤éè¿æç¥å°ä½¿ç¨ IOC 模å¼ï¼ä¹ç§°ä½ä¾èµæ³¨å
¥ï¼æ¥è§£å³ãå½ Rod åä¸ä¸ªå
·æå¥ç®ç²¾ç¥çå¼æ¾æºç å¼åè
å¢éå°è¿ä¸ªç论åºç¨äºå®è·µæ¶ï¼ç»æ就产çäº Spring æ¡æ¶ãç®è¨ä¹ï¼Spring æ¯ä¸ä¸ªè½»åç容å¨ï¼å©ç¨å®å¯ä»¥ä½¿ç¨ä¸ä¸ªå¤é¨ XML é
ç½®æ件æ¹ä¾¿å°å°å¯¹è±¡è¿æ¥å¨ä¸èµ·ãæ¯ä¸ªå¯¹è±¡é½å¯ä»¥éè¿æ¾ç¤ºä¸ä¸ª JavaBean å±æ§æ¶å°ä¸ä¸ªå°ä¾èµå¯¹è±¡çå¼ç¨ï¼çç»æ¨çç®åä»»å¡å°±åªæ¯å¨ä¸ä¸ª XML é
ç½®æ件ä¸æå®ä»¬è¿æ¥å¥½ã
|
IOC å Spring
IOC æ¯ä¸ç§ä½¿åºç¨ç¨åºé»è¾å¤å¨åç设计模å¼ï¼æ以å®æ¯è¢«æ³¨å
¥èä¸æ¯è¢«åå
¥å®¢æ·æºä»£ç ä¸ãå° IOC ä¸æ¥å£ç¼ç¨åºç¨ç»åï¼å°±å Spring æ¡æ¶é£æ ·ï¼äº§çäºä¸ç§æ¶æï¼è¿ç§æ¶æè½å¤åå°å®¢æ·æºå¯¹ç¹å®å®ç°é»è¾çä¾èµã请åé
åèèµæ äºè§£æ´å¤å
³äº IOC å Spring çä¿¡æ¯ã
|
|
ä¾èµæ³¨å
¥æ¯ä¸ä¸ªå¼ºå¤§çç¹æ§ï¼ä½æ¯ Spring æ¡æ¶è½å¤æä¾æ´å¤ç¹æ§ãSpring æ¯æå¯ææçäºå¡ç®¡çå¨ï¼å¯ä»¥ç»æ¨çäºå¡å¤çæä¾æ´å¹¿æ³çéæ©èå´ãå®éæäºé¢å
çæä¹
æ§æ¡æ¶ï¼å¹¶ä¸æä¾ä¸ä¸ªä¸è´çå¼å¸¸å±æ¬¡ç»æãSpring è¿æä¾äºä¸ç§ä½¿ç¨é¢åæ¹é¢ä»£ç 代æ¿æ£å¸¸çé¢å对象代ç çç®åæºå¶ã
Spring AOP å
许æ¨ä½¿ç¨æ¦æªå¨ å¨ä¸ä¸ªæå¤ä¸ªæ§è¡ç¹ä¸æ¦æªåºç¨ç¨åºé»è¾ãå 强åºç¨ç¨åºå¨æ¦æªå¨ä¸çæ¥å¿è®°å½é»è¾ä¼äº§çä¸ä¸ªæ´å¯è¯»çãå®ç¨ç代ç åºç¡ï¼æ以æ¦æªå¨å¹¿æ³ç¨äºæ¥å¿è®°å½ãæ¨å¾å¿«å°±ä¼çå°ï¼ä¸ºäºå¤ç横åå
³æ³¨ç¹ï¼Spring AOP åå¸äºå®èªå·±çæ¦æªå¨ï¼æ¨ä¹å¯ä»¥ç¼åæ¨èªå·±çæ¦æªå¨ã
æ´å Struts å Spring
ä¸ Struts ç¸ä¼¼ï¼Spring å¯ä»¥ä½ä¸ºä¸ä¸ª MVC å®ç°ãè¿ä¸¤ç§æ¡æ¶é½å
·æèªå·±çä¼ç¹å缺ç¹ï¼å°½ç®¡å¤§é¨å人åæ Struts å¨ MVC æ¹é¢ä»ç¶æ¯æ好çãå¾å¤å¼åå¢éå·²ç»å¦ä¼å¨æ¶é´ç´§è¿«çæ¶åå©ç¨ Struts ä½ä¸ºæé é«å质软件çåºç¡ãStruts å
·æå¦æ¤å¤§çæ¨å¨åï¼ä»¥è³äºå¼åå¢éå®æ¿æ´å Spring æ¡æ¶çç¹æ§ï¼èä¸æ¿æ转æ¢æ Spring MVCã没å¿
è¦è¿è¡è½¬æ¢å¯¹æ¨æ¥è¯´æ¯ä¸ä¸ªå¥½æ¶æ¯ãSpring æ¶æå
许æ¨å° Struts ä½ä¸º Web æ¡æ¶è¿æ¥å°åºäº Spring çä¸å¡åæä¹
å±ãæåçç»æå°±æ¯ç°å¨ä¸åæ¡ä»¶é½å
·å¤äºã
å¨æ¥ä¸æ¥çå°çªé¨ä¸ï¼æ¨å°ä¼äºè§£å°ä¸ç§å° Struts MVC æ´åå° Spring æ¡æ¶çæ¹æ³ãæå°æ示æ¯ç§æ¹æ³ç缺é·å¹¶ä¸å¯¹æ¯å®ä»¬çä¼ç¹ã ä¸æ¦æ¨äºè§£å°ææä¸ç§æ¹æ³çä½ç¨ï¼æå°ä¼åæ¨å±ç¤ºä¸ä¸ªä»¤äººå
´å¥çåºç¨ç¨åºï¼è¿ä¸ªç¨åºä½¿ç¨çæ¯è¿ä¸ç§æ¹æ³ä¸ææå欢çä¸ç§ã
ä¸ä¸ªå°çªé¨
æ¥ä¸æ¥çæ¯ç§æ´åææ¯ï¼æè
çªé¨ï¼é½æèªå·±çä¼ç¹åç¹ç¹ãæåç±å
¶ä¸çä¸ç§ï¼ä½æ¯æç¥éè¿ä¸ç§é½è½å¤å æ·±æ¨å¯¹ Struts å Spring çç解ãå¨å¤çåç§ä¸åæ
åµçæ¶åï¼è¿å°ç»æ¨æä¾ä¸ä¸ªå¹¿éçéæ©èå´ãæ¹æ³å¦ä¸ï¼
- ä½¿ç¨ Spring ç
ActionSupport ç±»æ´å Structs
- ä½¿ç¨ Spring ç
DelegatingRequestProcessor è¦ç Struts ç RequestProcessor
- å° Struts
Action 管çå§æç» Spring æ¡æ¶
è£
è½½åºç¨ç¨åºç¯å¢
æ 论æ¨ä½¿ç¨åªç§ææ¯ï¼é½éè¦ä½¿ç¨ Spring ç ContextLoaderPlugin 为 Struts ç ActionServlet è£
è½½ Spring åºç¨ç¨åºç¯å¢ãå°±åæ·»å ä»»ä½å
¶ä»æ件ä¸æ ·ï¼ç®åå°åæ¨ç struts-config.xml æ件添å 该æ件ï¼å¦ä¸æ示ï¼
<plug-in className=
"org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property=
"contextConfigLocation" value="/WEB-INF/beans.xml"/>
</plug-in>
|
åé¢å·²ç»æå°è¿ï¼å¨ ä¸è½½ é¨åï¼æ¨è½å¤æ¾å°è¿ä¸ä¸ªå®å
¨å¯ä½¿ç¨çä¾åçå®æ´æºä»£ç ãæ¯ä¸ªä¾åé½ä¸ºä¸ä¸ªä¹¦ç±æç´¢åºç¨ç¨åºæä¾ä¸ç§ä¸åç Struts å Spring çæ´åæ¹æ³ãæ¨å¯ä»¥å¨è¿éçå°ä¾åçè¦ç¹ï¼ä½æ¯æ¨ä¹å¯ä»¥ä¸è½½åºç¨ç¨åºä»¥æ¥çææçç»èã
çªé¨ 1. ä½¿ç¨ Spring ç ActionSupport
æå¨å建ä¸ä¸ª Spring ç¯å¢æ¯ä¸ç§æ´å Struts å Spring çæç´è§çæ¹å¼ã为äºä½¿å®åå¾æ´ç®åï¼Spring æä¾äºä¸äºå¸®å©ã为äºæ¹ä¾¿å°è·å¾ Spring ç¯å¢ï¼org.springframework.web.struts.ActionSupport ç±»æä¾äºä¸ä¸ª getWebApplicationContext() æ¹æ³ãæ¨æåçåªæ¯ä» Spring ç ActionSupport èä¸æ¯ Struts Action ç±»æ©å±æ¨çå¨ä½ï¼å¦æ¸
å 1 æ示ï¼
æ¸
å 1. ä½¿ç¨ ActionSupport æ´å Struts
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");
}
}
|
让æ们快éæèä¸ä¸è¿éå°åºåçäºä»ä¹ãå¨ (1) å¤ï¼æéè¿ä» Spring ç ActionSupport ç±»èä¸æ¯ Struts ç Action ç±»è¿è¡æ©å±ï¼å建äºä¸ä¸ªæ°ç Action ãå¨ (2) å¤ï¼æä½¿ç¨ getWebApplicationContext() æ¹æ³è·å¾ä¸ä¸ª ApplicationContext ã为äºè·å¾ä¸å¡æå¡ï¼æ使ç¨å¨ (2) å¤è·å¾çç¯å¢å¨ (3) å¤æ¥æ¾ä¸ä¸ª Spring beanã
è¿ç§ææ¯å¾ç®å并ä¸æäºç解ãä¸å¹¸çæ¯ï¼å®å° Struts å¨ä½ä¸ Spring æ¡æ¶è¦åå¨ä¸èµ·ãå¦ææ¨æ³æ¿æ¢æ Springï¼é£ä¹æ¨å¿
é¡»éå代ç ã并ä¸ï¼ç±äº Struts å¨ä½ä¸å¨ Spring çæ§å¶ä¹ä¸ï¼æ以å®ä¸è½è·å¾ Spring AOP çä¼å¿ãå½ä½¿ç¨å¤éç¬ç«ç Spring ç¯å¢æ¶ï¼è¿ç§ææ¯å¯è½æç¨ï¼ä½æ¯å¨å¤§å¤æ°æ
åµä¸ï¼è¿ç§æ¹æ³ä¸å¦å¦å¤ä¸¤ç§æ¹æ³åéã
çªé¨ 2. è¦ç RequestProcessor
å° Spring ä» Struts å¨ä½ä¸å离æ¯ä¸ä¸ªæ´å·§å¦ç设计éæ©ãå离çä¸ç§æ¹æ³æ¯ä½¿ç¨ org.springframework.web.struts.DelegatingRequestProcessor ç±»æ¥è¦ç Struts ç RequestProcessor å¤çç¨åºï¼å¦æ¸
å 2 æ示ï¼
æ¸
å 2. éè¿ Spring ç DelegatingRequestProcessor è¿è¡æ´å
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="searchForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="isbn" type="java.lang.String"/>
</form-bean>
</form-beans>
<global-forwards type="org.apache.struts.action.ActionForward">
<forward name="welcome" path="/welcome.do"/>
<forward name="searchEntry" path="/searchEntry.do"/>
<forward name="searchSubmit" path="/searchSubmit.do"/>
</global-forwards>
<action-mappings>
<action path="/welcome" forward="/WEB-INF/pages/welcome.htm"/>
<action path="/searchEntry" forward="/WEB-INF/pages/search.jsp"/>
<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>
</action-mappings>
<message-resources parameter="ApplicationResources"/>
<controller processorClass="org.springframework.web.struts.
DelegatingRequestProcessor"/> |(1)
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="csntextConfigLocation" value="/WEB-INF/beans.xml"/>
</plug-in>
</struts-config>
|
æå©ç¨äº <controller> æ è®°æ¥ç¨ DelegatingRequestProcessor è¦çé»è®¤ç Struts RequestProcessor ãä¸ä¸æ¥æ¯å¨æç Spring é
ç½®æ件ä¸æ³¨å该å¨ä½ï¼å¦æ¸
å 3 æ示ï¼
æ¸
å 3. å¨ Spring é
ç½®æ件ä¸æ³¨åä¸ä¸ªå¨ä½
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/>
<bean name="/searchSubmit"
class="ca.nexcel.books.actions.SearchSubmit"> |(1)
<property name="bookService">
<ref bean="bookService"/>
</property>
</bean>
</beans>
|
注æï¼å¨ (1) å¤ï¼æ使ç¨å称å±æ§æ³¨åäºä¸ä¸ª beanï¼ä»¥å¹é
struts-config å¨ä½æ å°å称ãSearchSubmit å¨ä½æ示äºä¸ä¸ª JavaBean å±æ§ï¼å
许 Spring å¨è¿è¡æ¶å¡«å
å±æ§ï¼å¦æ¸
å 4 æ示ï¼
æ¸
å 4. å
·æ JavaBean å±æ§ç Struts å¨ä½
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");
}
}
|
å¨æ¸
å 4 ä¸ï¼æ¨å¯ä»¥äºè§£å°å¦ä½å建 Struts å¨ä½ãå¨ (1) å¤ï¼æå建äºä¸ä¸ª JavaBean å±æ§ãDelegatingRequestProcessor èªå¨å°é
ç½®è¿ç§å±æ§ãè¿ç§è®¾è®¡ä½¿ Struts å¨ä½å¹¶ä¸ç¥éå®æ£è¢« Spring 管çï¼å¹¶ä¸ä½¿æ¨è½å¤å©ç¨ Sping çå¨ä½ç®¡çæ¡æ¶çææä¼ç¹ãç±äºæ¨ç Struts å¨ä½æ³¨æä¸å° Spring çåå¨ï¼æ以æ¨ä¸éè¦éåæ¨ç Struts 代ç å°±å¯ä»¥ä½¿ç¨å
¶ä»æ§å¶å转容å¨æ¥æ¿æ¢æ Springã
DelegatingRequestProcessor æ¹æ³çç¡®æ¯ç¬¬ä¸ç§æ¹æ³å¥½ï¼ä½æ¯ä»ç¶åå¨ä¸äºé®é¢ãå¦ææ¨ä½¿ç¨ä¸ä¸ªä¸åç RequestProcessor ï¼åéè¦æå¨æ´å Spring ç DelegatingRequestProcessor ãæ·»å ç代ç ä¼é æç»´æ¤ç麻ç¦å¹¶ä¸å°æ¥ä¼éä½æ¨çåºç¨ç¨åºççµæ´»æ§ãæ¤å¤ï¼è¿æè¿ä¸äºä½¿ç¨ä¸ç³»åå½ä»¤æ¥ä»£æ¿ Struts RequestProcessor çä¼ é»ã è¿ç§æ¹åå°ä¼å¯¹è¿ç§è§£å³æ¹æ³ç使ç¨å¯¿å½é æè´é¢çå½±åã
çªé¨ 3. å°å¨ä½ç®¡çå§æç» Spring
ä¸ä¸ªæ´å¥½ç解å³æ¹æ³æ¯å° Strut å¨ä½ç®¡çå§æç» Springãæ¨å¯ä»¥éè¿å¨ struts-config å¨ä½æ å°ä¸æ³¨åä¸ä¸ªä»£çæ¥å®ç°ã代çè´è´£å¨ Spring ç¯å¢ä¸æ¥æ¾ Struts å¨ä½ãç±äºå¨ä½å¨ Spring çæ§å¶ä¹ä¸ï¼æ以å®å¯ä»¥å¡«å
å¨ä½ç JavaBean å±æ§ï¼å¹¶ä¸ºåºç¨è¯¸å¦ Spring ç AOP æ¦æªå¨ä¹ç±»çç¹æ§å¸¦æ¥äºå¯è½ã
æ¸
å 5 ä¸ç Action ç±»ä¸æ¸
å 4 ä¸çç¸åãä½æ¯ struts-config æä¸äºä¸åï¼
æ¸
å 5. Spring æ´åçå§ææ¹æ³
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="searchForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="isbn" type="java.lang.String"/>
</form-bean>
</form-beans>
<global-forwards type="org.apache.struts.action.ActionForward">
<forward name="welcome" path="/welcome.do"/>
<forward name="searchEntry" path="/searchEntry.do"/>
<forward name="searchSubmit" path="/searchSubmit.do"/>
</global-forwards>
<action-mappings>
<action path="/welcome" forward="/WEB-INF/pages/welcome.htm"/>
<action path="/searchEntry" forward="/WEB-INF/pages/search.jsp"/>
<action path="/searchSubmit"
type="org.springframework.web.struts.DelegatingActionProxy" |(1)
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>
</action-mappings>
<message-resources parameter="ApplicationResources"/>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/beans.xml"/>
</plug-in>
</struts-config>
|
æ¸
å 5 æ¯ä¸ä¸ªå
¸åç struts-config.xml æ件ï¼åªæä¸ä¸ªå°å°çå·®å«ãå®æ³¨å Spring 代çç±»çå称ï¼èä¸æ¯å£°æå¨ä½çç±»åï¼å¦ï¼1ï¼å¤æ示ãDelegatingActionProxy 类使ç¨å¨ä½æ å°å称æ¥æ¾ Spring ç¯å¢ä¸çå¨ä½ãè¿å°±æ¯æä»¬ä½¿ç¨ ContextLoaderPlugIn 声æçç¯å¢ã
å°ä¸ä¸ª Struts å¨ä½æ³¨å为ä¸ä¸ª Spring bean æ¯é常ç´è§çï¼å¦æ¸
å 6 æ示ãæå©ç¨å¨ä½æ å°ä½¿ç¨ <bean> æ è®°çå称å±æ§ï¼å¨è¿ä¸ªä¾åä¸æ¯ "/searchSubmit "ï¼ç®åå°å建äºä¸ä¸ª beanãè¿ä¸ªå¨ä½ç JavaBean å±æ§åä»»ä½ Spring bean ä¸æ ·è¢«å¡«å
ï¼
æ¸
å 6. å¨ Spring ç¯å¢ä¸æ³¨åä¸ä¸ª Struts å¨ä½
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<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>
</beans>
|
å¨ä½å§æçä¼ç¹
å¨ä½å§æ解å³æ¹æ³æ¯è¿ä¸ç§æ¹æ³ä¸æ好çãStruts å¨ä½ä¸äºè§£ Springï¼ä¸å¯¹ä»£ç ä½ä»»ä½æ¹åå°±å¯ç¨äºé Spring åºç¨ç¨åºä¸ãRequestProcessor çæ¹åä¸ä¼å½±åå®ï¼å¹¶ä¸å®å¯ä»¥å©ç¨ Spring AOP ç¹æ§çä¼ç¹ã
å¨ä½å§æçä¼ç¹ä¸æ¢å¦æ¤ãä¸æ¦è®© Spring æ§å¶æ¨ç Struts å¨ä½ï¼æ¨å°±å¯ä»¥ä½¿ç¨ Spring ç»å¨ä½è¡¥å
æ´å¼ºçæ´»åãä¾å¦ï¼æ²¡æ Spring çè¯ï¼ææç Struts å¨ä½é½å¿
é¡»æ¯çº¿ç¨å®å
¨çãå¦ææ¨è®¾ç½® <bean> æ è®°ç singleton å±æ§ä¸º“false”ï¼é£ä¹ä¸ç®¡ç¨ä½ç§æ¹æ³ï¼æ¨çåºç¨ç¨åºé½å°å¨æ¯ä¸ä¸ªè¯·æ±ä¸æä¸ä¸ªæ°çæçå¨ä½å¯¹è±¡ãæ¨å¯è½ä¸éè¦è¿ç§ç¹æ§ï¼ä½æ¯æå®æ¾å¨æ¨çå·¥å
·ç®±ä¸ä¹å¾å¥½ãæ¨ä¹å¯ä»¥å©ç¨ Spring ççå½å¨ææ¹æ³ãä¾å¦ï¼å½å®ä¾å Struts å¨ä½æ¶ï¼<bean> æ è®°ç init-method å±æ§è¢«ç¨äºè¿è¡ä¸ä¸ªæ¹æ³ã类似å°ï¼å¨ä»å®¹å¨ä¸å é¤ bean ä¹åï¼destroy-method å±æ§æ§è¡ä¸ä¸ªæ¹æ³ãè¿äºæ¹æ³æ¯ç®¡çæ贵对象ç好åæ³ï¼å®ä»¬ä»¥ä¸ç§ä¸ Servlet çå½å¨æç¸åçæ¹å¼è¿è¡ç®¡çã
æ¦æª Struts
åé¢æå°è¿ï¼éè¿å° Struts å¨ä½å§æç» Spring æ¡æ¶èæ´å Struts å Spring çä¸ä¸ªä¸»è¦çä¼ç¹æ¯ï¼æ¨å¯ä»¥å° Spring ç AOP æ¦æªå¨åºç¨äºæ¨ç Struts å¨ä½ãéè¿å° Spring æ¦æªå¨åºç¨äº Struts å¨ä½ï¼æ¨å¯ä»¥ç¨æå°ç代价å¤ç横åå
³æ³¨ç¹ã
è½ç¶ Spring æä¾å¾å¤å
ç½®æ¦æªå¨ï¼ä½æ¯æå°åæ¨å±ç¤ºå¦ä½å建èªå·±çæ¦æªå¨å¹¶æå®åºç¨äºä¸ä¸ª Struts å¨ä½ã为äºä½¿ç¨æ¦æªå¨ï¼æ¨éè¦åä¸ä»¶äºï¼
- å建æ¦æªå¨ã
- 注åæ¦æªå¨ã
- 声æå¨ä½å¤æ¦æªä»£ç ã
è¿çèµ·æ¥é常ç®åçå å¥è¯å´é常强大ãä¾å¦ï¼å¨æ¸
å 7 ä¸ï¼æ为 Struts å¨ä½å建äºä¸ä¸ªæ¥å¿è®°å½æ¦æªå¨ã è¿ä¸ªæ¦æªå¨å¨æ¯ä¸ªæ¹æ³è°ç¨ä¹åæå°ä¸å¥è¯ï¼
æ¸
å 7. ä¸ä¸ªç®åçæ¥å¿è®°å½æ¦æªå¨
package ca.nexcel.books.interceptors;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class LoggingInterceptor implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("logging before!");
}
}
|
è¿ä¸ªæ¦æªå¨é常ç®åãbefore() æ¹æ³å¨æ¦æªç¹ä¸æ¯ä¸ªæ¹æ³ä¹åè¿è¡ãå¨æ¬ä¾ä¸ï¼å®æå°åºä¸å¥è¯ï¼å
¶å®å®å¯ä»¥åæ¨æ³åçä»»ä½äºãä¸ä¸æ¥å°±æ¯å¨ Spring é
ç½®æ件ä¸æ³¨åè¿ä¸ªæ¦æªå¨ï¼å¦æ¸
å 8 æ示ï¼
æ¸
å 8. å¨ Spring é
ç½®æ件ä¸æ³¨åæ¦æªå¨
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<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>
<!-- Interceptors -->
<bean name="logger"
class="ca.nexcel.books.interceptors.LoggingInterceptor"/> |(1)
<!-- AutoProxies -->
<bean name="loggingAutoProxy"
class="org.springframework.aop.framework.autoproxy.
BeanNameAutoProxyCreator"> |(2)
<property name="beanNames">
<value>/searchSubmit</valuesgt; |(3)
</property>
<property name="interceptorNames">
<list>
<value>logger</value> |(4)
</list>
</property>
</bean>
</beans>
|
æ¨å¯è½å·²ç»æ³¨æå°äºï¼æ¸
å 8 æ©å±äº æ¸
å 6 ä¸æ示çåºç¨ç¨åºä»¥å
å«ä¸ä¸ªæ¦æªå¨ãå
·ä½ç»èå¦ä¸ï¼
- å¨ (1) å¤ï¼æ注åäºè¿ä¸ªæ¦æªå¨ã
- å¨ (2) å¤ï¼æå建äºä¸ä¸ª bean å称èªå¨ä»£çï¼å®æè¿°å¦ä½åºç¨æ¦æªå¨ãè¿æå
¶ä»çæ¹æ³å®ä¹æ¦æªç¹ï¼ä½æ¯è¿ç§æ¹æ³å¸¸è§èç®ä¾¿ã
- å¨ (3) å¤ï¼æå° Struts å¨ä½æ³¨å为å°è¢«æ¦æªç beanãå¦ææ¨æ³è¦æ¦æªå
¶ä»ç Struts å¨ä½ï¼ååªéè¦å¨ "beanNames" ä¸é¢å建éå ç
<value> æ è®°ã
- å¨ (4) å¤ï¼å½æ¦æªåçæ¶ï¼ææ§è¡äºå¨ (1) å¤å建çæ¦æªå¨ bean çå称ãè¿éååºçæææ¦æªå¨é½åºç¨äº“beanNames”ã
å°±æ¯è¿æ ·ãå°±åè¿ä¸ªä¾åæå±ç¤ºçï¼å°æ¨ç Struts å¨ä½ç½®äº Spring æ¡æ¶çæ§å¶ä¹ä¸ï¼ä¸ºå¤çæ¨ç Struts åºç¨ç¨åºæä¾äºä¸ç³»åå
¨æ°çéæ©ãå¨æ¬ä¾ä¸ï¼ä½¿ç¨å¨ä½å§æå¯ä»¥è½»æ¾å°å©ç¨ Spring æ¦æªå¨æé« Struts åºç¨ç¨åºä¸çæ¥å¿è®°å½è½åã
ç» |
相关推荐
4. **3.8 æ°åå¾åå¤çå¨å»çé¢åçåºç¨.rar**:这部分可能涉及边缘检测,如Canny算法,它是图像处理中寻找图像边缘的常用方法,用于识别物体轮廓。...
为使用PostwomanUFOWeb应ç”ççåçæ››››› ç?åä‹è:registered:¿é:registered::registered:localhost应ç“åå……WEBAPI,请è:registered:¿é—:registered:...
ä¸å ´é¡¹ç®ç»-XXX-转æ£ç³è¯·è¡¨(1).doc
文件"éä¼ ç®æ³ç»¼åèµå½¢æ³¢æéµå天线åMatlabç¨åºå®ç°.pdf"可能包含了详细的遗传算法实现过程和Matlab代码示例,包括如何设置参数、...
在《éä¼ ç®æ³ç»¼åèµå½¢æ³¢æéµå天线åMatlabç¨åºå®ç°.pdf》文件中,作者可能详细介绍了如何使用Matlab的Global Optimization Toolbox...
另一篇文档"å©ç¨éº¦å é£å®ä½å£°æºçæ¹æ³åè£ ç½®.pdf"的标题(翻译为“采用sconv实现傅里叶变换的算法描述和应用”)表明它详细介绍了`sconv`函数的实现...
使您的ç½'上冲浪å åå¾—ç§å¯†ä¸”å:registered:‰å…¨ Avira浏览器安全保护您免受恶意网站和隐形跟踪器的侵害安全浏览-安全浏览:我们每月封锁超过8,000万个恶意网站。 -...
Î÷·½ÎÄ»¯Ö®ÍâµÄÒ»Öֲ¶Ô×ӵķç¿Ø¡£Èå¼ÒÎÄ»¯²»Ö¹Ö»ÊÇÒ»ÖÖ±à×¢·ç¿Ø¡¢¹Ø¼ü×Ö·ç¿Ø¡¢¹ý³Ì·ç¿Ø¡¢ÎÄ×Ö·ç¿ØµÄ×éºÏ...
TunnelBear Blockerå'¬å½è·Ÿè¸ªæ‚¨çš„ç½'ç«:trade_mark:ä¸æ”¾ï¼Œå¸:registered:åŠ:copyright:¿ çš„ç½'络å:registered:‰å…¨ä¸Žéšç§ã€, 获取TunnelBear ...
//¶¨Òåµç×ÓËø¿ØÖÆÒý½Å uchar code SEGCODE[] = {0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,//0...9µÄ¹²Ñô¼«¶ÎÂë 0XFF,//²»ÏÔʾ 0X8C,//ÏÔʾ×Ö·...
½·¨¿ÉÒÔ×îÖÕ±ÜÃâÔ±¹¤Î¥¹æºó²»»á³öÏÖ³ö²»³É¹¦µÄ³õ²»²ÉÓá£ÀýÈç¡£ºþ±±ÈºÒÕÔ±¹¤µÄ»ý·Ö¿ÉÒÔÖ®ºó¿É±ä³ÉÔ±¹¤×ÔÎÒ¼...
½·¨¡£3 / 10ÊèÉ¢Òýµ导读组:Á¬ºóÖ°Îñ³ö²é×éºÍ¿Í·þ×飬²é¿´Îï×ʲúÊÇ·ñºÏ·À»ð¹æÔò£¬È·ÈÏÉ豸Çå¹Ü¡¢ÉèÖú͹ÜÀíÈ·ÈϺܺ...
//ΪfatfsÏà¹Ø±äÁ¿ÉêÇëÄÚ´æ f_mount(fs[0],"0:",1); //¹ÒÔØSD¿¨ f_mount(fs[1],"1:",1); //¹ÒÔØFLASH. POINT_COLOR=RED; while(font_init()) //¼ì²é×Ö...
µç»úÂß¼µçƽ PB12(×óÇ°Ç°½ø),PB13(ÓÒÇ°ºóÍË),PB14£¨×óÇ°ºóÍË£©,PB15£¨ÓÒÇ°Ç°½ø£©,PE12£¨×óºóÇ°½ø£©,PE13£¨ÓÒºóÇ°½ø£©,PE14£¨×ó...
/********************************************************** *YM12864R-3 Òº¾§Çý¶¯(3Ïß´®Ðз½Ê½) LCD Module *¿ØÖÆоƬ:ST7920 *°æ±¾:V1.3 *ÈÕÆÚ:2007-6-6 *...
LECCO SQL Expert for sql server,Ò»¿îºÜºÃÓõÄSQLÓï¾äÓÅ»¯¹¤¾ß ѹËõ°üÀﺬÓÐ×¢²á»úºÍºº»¯Îļþ Èí¼þÆƽâ×¢²á¼°ºº»¯ËµÃ÷ Æƽⷽ...
ä½åœ¨å¦ä¸€é–èèèè€ï¼ééå‰:copyright:éœ€è¦ é〜…è¯»çš„æ –‡å–,è:registered::copyright:SpeakItæ¥è¯»ç»:trade_mark:ä½å¬ï¼ SpeakIt使用具有语音自动...
if ( PcdAnticoll ( ucArray_ID ) == MI_OK ) //·À³åײ£¨µ±ÓжàÕÅ¿¨½øÈë¶ÁдÆ÷²Ù×÷·¶Î§Ê±£¬·À³åÍ»»úÖÆ»á´ÓÆäÖÐÑ¡ÔñÒ»ÕŽøÐвÙ×÷£© ...
printf(" ÊäÈëµç»°:\n"); scanf("%s",p->data.telephone); printf(" ÊäÈëµØÖ·:\n"); scanf("%s",p->data.address); printf(" ÊäÈëÓʱà:\n"); scanf("%s",p->data.youbian); r->next=...
“°Ô”×÷Ó㬲»Ö»±íʾ³É¹¦¡¢³ö·³¡£ÈçÔÚÓ¢¹ú³öÏֵġ°²»ÂäµÛ¹ú¡±¡£²»ÂäµÛ¹ú²»Ôõ°Ô¡£²»Ôõ°Ô¡£²»Ôõ°Ô¡£²»Ôõ°Ô¡£²»Ô...