锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
Spring涓嶴truts闆嗘垚寮鍙?br />
鏈榪戝枩嬈㈠皢鎵瀛︾殑涓滆タ鐞嗛『涓涓嬶紝涓斿彂鐜板啓blog鍙互杈炬垚榪欑洰鐨勩?br />
閭e氨鏉ユ暣鐞嗕竴涓嬫垜瀵筍pring涓嶴truts闆嗘垚寮鍙戠殑涓浜涙兂娉曘?br />
棣栧厛紜緋葷粺鐨勭粨鏋勪負涓夊眰鐨凚/S妯″紡緇撴瀯錛屽涓嬪浘錛?br />
鍦ㄥ浘涓湅鍑猴紝Spring鍜孲truts闆嗘垚寮鍙戜腑錛孲pring鍦ㄤ笟鍔¢昏緫灞傝浣跨敤錛堥泦鎴愶級銆傚洜涓篠pring妗嗘灦鐨勪緷璧栨敞鍏ワ紝AOP鍙婂彲澹版槑鐨勪簨鍔$鐞嗘柟闈㈢殑鎶鏈紭鍔匡紝浣垮緱鐢⊿pring鏉ョ鐞嗕笟鍔″疄浣擄紝瀹炰綋涔嬮棿鐨勪緷璧栧叧緋伙紝涓氬姟閫昏緫鏈嶅姟鎺ュ彛鍙樺緱綆鍗曚笖鍙厤緗傝嚦姝ゆ垜浠娓呮錛?span style="color: red">Spring鍜孲truts闆嗘垚寮鍙戜腑錛孲pring鍦ㄤ笟鍔¢昏緫灞傝浣跨敤錛堥泦鎴愶級銆?/span>
娓呮Struts鍜孲pring鍦ㄧ郴緇熺粨鏋勪腑鍒嗗埆鍏呭綋鐨勮鑹插悗錛屾帴涓嬫潵瑕佽璁猴細濡備綍瀹炵幇闆嗘垚錛?br />
1銆佷嬌鐢⊿pring鐨凙ctionSurpert綾婚泦鎴怱truts銆?/strong>
org.springframework.web.struts.ActionSurpert鏄竴涓戶鎵縪rg.apache.struts.action.Action鐨勭被錛岀畝瑕佷唬鐮佸涓嬶細
public abstract class ActionSurpert extends Action {
private WebApplicationContext webApplicationContext;
public void setServlet(ActionServlet actionServlet) {//褰撳鍣ㄥ疄渚嬪寲姝ction鏃惰瀹瑰櫒璋冪敤
surper.setServlet(actionServlet);
if(actionServlet != null) {
this.webApplicationContext = initWebApplicationContext(actionServlet);//鑾峰彇webApplicationContext
//........
}else{
//.......
}
}
//閫氳繃getXXX()鏂規硶鑾峰彇 webApplicationContext 瀵硅薄
protected final WebApplictionContext getWebApplicationContext() {
return this.webApplicationContext;
}
}
閫氳繃涓婅堪浠g爜鍙互鐪嬪嚭錛屾墍鏈夌戶鎵夸簡ActionSupport綾葷殑Action灝嗗彲浠ラ氳繃WebApplicationContext瀵硅薄鐨刧etBean(beanKey)鏂規硶鑾峰緱Spring閰嶇疆鏂囦歡涓畾涔夌殑鍚勭Bean銆?br />
WebApplicationContext瑕佺敱Web Server鏉ュ姞杞斤紝鏈変袱縐嶆柟娉曪細
錛戙侀氳繃org.springframework.web.struts.ContextLoaderPlugIn鍔犺澆錛孋ontextLoaderPlugIn鏄釜鎻掍歡錛岄渶瑕佸湪Struts閰嶇疆鏂囦歡涓厤緗?br />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property = "contextConfigLocation" value="/WEB-INF/applicationContext.xml"></set-property>
</plug-in>
2銆佸湪web.xml鏂囦歡涓姞杞?br />
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet>
涓句釜綆鍗曠殑渚嬪瓙璇存槑涓涓嬬浉鍏抽厤緗俊鎭細
鍋囧畾鏈塃xampleAction錛孍xampleBean錛孍xampleService榪欏嚑涓被錛屽畠浠伐浣滄祦紼嬫槸錛?br />
鐢ㄦ埛璇鋒眰ExampleAction錛孍xampleAction璋冪敤ExampleService鐨勬帴鍙f柟娉曞ExampleBean榪涜鐩稿叧鐨勬搷浣溿?br />
1銆乤pplicationContext.xml涓厤緗浉鍏崇殑bean淇℃伅
<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<bean id="exampleBean" class="org.mypackge.beans.ExampleBean"/>
<bean id="exampleBeanService" class="org.mypackge.services.ExampleService"/>@
</beans>
閫氳繃榪欐牱閰嶇疆鍚庯紝鍦‥xampleAction涓彲浠ョ敤getWebApplicationContext() 鑾峰緱webApplicationContext瀵硅薄錛岀劧鍚庨氳繃
webApplicationContext鐨刧etBean(beanKey)鏂規硶鑾峰緱鐩稿簲鐨刡ean榪涜涓氬姟澶勭悊銆傛爣浜嗙孩鑹茬殑"beanKey"灝辨槸applicationContext.xml涓?lt;bean>鍏冪礌瀹氫箟鐨刡ean id錛屽:webApplicationContext.getBean("exampleBeanService")銆侤
褰撶劧錛孍xampleAction榪樿鍦╯tuts-config.xml閰嶇疆鏂囦歡涓厤緗紝榪欓噷涓嶄綔浠嬬粛銆?br />
2銆佷嬌鐢⊿pring鐨凙ction浠g悊闆嗘垚Struts
榪欑闆嗘垚鏂瑰紡鐨勬牳蹇冩濇兂鏄紝灝哠truts鐨勯厤緗枃浠朵腑鐨勬墍鏈堿ction鐨則ype灞炴ц涓簅rg.springframwork.web.struts.DelegationActionProxy銆傚綋鐢ㄦ埛璇鋒眰Action鏃訛紝灝辨墽琛岃繖浠g悊錛屼唬鐞嗕細鍦⊿pring搴旂敤涓婁笅鏂囦腑鎵懼埌鐪熸鐨凙ction錛岀劧鍚庝氦緇欏畠澶勭悊鐢ㄦ埛鐨勮姹傘傝岀湡姝g敤浜庡鐞嗙敤鎴瘋姹傜殑Action鐨勯厤緗斁鍦ㄤ簡Spring鐨勯厤緗枃浠朵腑銆傝繖鏍鳳紝Struts涓殑Action浠ュ強鍏朵緷璧栧叧緋誨氨鍙互鐢⊿pring瀹瑰櫒鏉ョ鐞嗭紝姣斿灝嗕笟鍔¢昏緫瀵硅薄娉ㄥ叆鍒癆ction涓紝渚涘叾浣跨敤銆傜畝鍗曠墖孌?lt;bean name="/exampleAction" class="org.myproj.struts.actions.ExampleAction">
....
</bean>
璇存槑錛氱敤name灞炴ц屼笉鏄敤id鏉ユ爣璇嗚繖涓狟ean,Spring涓嶅厑璁窱D涓嚭鐜?/"錛岃宯ame鍙互錛?name"灞炴у艱鍜宻truts-config.xml鏂囦歡涓浉搴?lt;action>鍏冪礌涓殑path灞炴у肩浉鍚?span style="color: #ff99cc">(<action path="/exampleAction")錛岃繖鏍稟ction浠g悊鎵嶈兘鎵懼埌鐩稿簲鐨凙ction鏉ュ鐞嗚姹傘?br />
鍏充簬Spring鍜孲truts鐨勯泦鎴?榪樻湁絎笁縐嶆柟寮?灝嗗湪涓嬩竴綃囨枃绔?Spring+Struts+Hibernate闆嗘垚搴旂敤瀹炰緥"涓粙緇嶃?br />
嬈㈣繋璁ㄨ錛屾彁鍑哄疂璐墊剰瑙併?br />