锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
2錛屼嬌鐢⊿pring 鐨?DelegatingRequestProcessor 綾匯?/p>
3錛屽叏鏉冨鎵樸?br />
鏃犺鐢ㄩ偅縐嶆柟娉曟潵鏁村悎絎竴姝ュ氨鏄涓簊truts鏉ヨ杞絪pring鐨勫簲鐢ㄧ幆澧冦?灝辨槸鍦?struts 涓姞鍏ヤ竴涓彃浠躲?/p>
struts-config.xml涓?/p>
錛減lug-in className="org.springframework.web.struts.ContextLoaderPlugIn"錛? 錛渟et-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/錛? 錛?plug-in錛?/pre> |
spring 鐨勯厤緗枃浠惰浣滀負(fù)鍙傛暟閰嶇疆榪涙潵銆傝繖鏍峰彲浠ョ渷鐣ュweb.xml 鏂囦歡涓殑閰嶇疆銆傜‘淇濅綘鐨刟pplicationContext.xml 鍦╓EB-INF鐩綍涓嬮潰
1銆佷嬌鐢⊿pring鐨凙ctionSupport .
Spring 鐨凙ctionSupport 緇ф壙鑷硂rg.apache.struts.action.Action
ActionSupport鐨勫瓙綾誨彲浠ユ垨寰?WebApplicationContext綾誨瀷鐨勫叏灞鍙橀噺銆傞氳繃getWebApplicationContext錛堬級(jí)鍙互鑾峰緱榪欎釜鍙橀噺銆?/span>
榪欐槸涓涓?servlet 鐨勪唬鐮侊細(xì)
public class LoginAction extends org.springframework.web.struts.ActionSupport { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub //鑾峰緱 WebApplicationContext 瀵硅薄 WebApplicationContext ctx = this.getWebApplicationContext(); LoginDao dao = (LoginDao) ctx.getBean("loginDao"); User u = new User(); u.setName(loginForm.getName()); u.setPwd(loginForm.getPwd()); if(dao.checkLogin(u)){ return mapping.findForward("success"); }else{ return mapping.findForward("error"); } } } applicationContext.xml 涓殑閰嶇疆 錛渂eans錛? 錛渂ean id="loginDao" class="com.cao.dao.LoginDao"/錛? 錛?beans錛? |
榪欎腑閰嶇疆鏂瑰紡鍚岀洿鎺ュ湪web.xml鏂囦歡閰嶇疆宸埆涓嶅ぇ銆?br />
娉ㄦ剰錛欰ction緇ф壙鑷?org.springframework.web.struts.ActionSupport 浣垮緱struts鍜宻pring鑰﹀悎鍦ㄤ竴璧楓?/p>
浣嗗疄鐜頒簡琛ㄧず灞傚拰涓氬姟閫昏緫灞傜殑瑙h︼紙LoginDao dao = 錛圠oginDao錛?ctx.getBean錛?loginDao"錛夛級(jí)銆?br />
2銆佷嬌鐢⊿pring 鐨?DelegatingRequestProcessor 綾?/strong>
DelegatingRequestProcessor 緇ф壙鑷?org.apache.struts.action.RequestProcessor 騫惰鐩栦簡閲岄潰鐨勬柟娉曘?/span>
sturts-config.xml 涓?/span> processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/> 閫氳繃 org.apache.struts.action.RequestProcessor 鐨勮姹傚鐞嗐?/span> 榪欓噷 name="/login" 涓巗truts 涓殑path鍖歸厤 class="com.cao.struts.action.LoginAction" 涓巗truts 涓殑type鍖歸厤 榪樿涓?LoginAction 鎻愪緵蹇呰鐨剆etXXX鏂規(guī)硶銆?鑾峰緱ApplicationCotext鍜屼緷璧栨敞鍏ョ殑宸ヤ綔閮藉湪DelegatingRequestProcessor涓畬鎴愩?/span> 3錛屽叏鏉冨鎵橈細(xì) Action 鐨勫垱寤哄拰瀵硅薄鐨勪緷璧栨敞鍏ュ叏閮ㄧ敱IOC瀹瑰櫒鏉ュ畬鎴愩備嬌鐢⊿pring鐨凞elegatingAcionProxy鏉ュ府鍔╁疄鐜頒唬鐞嗙殑宸ヤ綔 org.springframework.web.struts.DelegatingActiongProxy緇ф壙浜巓rg.apache.struts.action.Action . 鍏ㄦ潈濮旀墭鐨勯厤緗柟寮忓悓 鏂瑰紡 2 綾諱技 錛坅pplcationContext.xml鏂囦歡鐨勯厤緗拰 Action綾葷殑瀹炵幇鏂瑰紡鐩稿悓錛夈?/p>
鐞嗙敱錛?/strong> 1錛岀涓縐嶄嬌寰楄繃澶氱殑鑰﹀悎浜哠pring鍜孉ction . 2錛孯equestProcessor綾誨凡緇忚浠g悊 濡傛灉瑕佸啀瀹炵幇鑷繁鐨勫疄鐜版柟寮忥紙濡傦細(xì)緙栫爜澶勭悊錛夋曟湁鐐歸夯鐑?chǔ)銆?/p>
鎬葷粨涓涓嬶細(xì) 鏁村悎宸ヤ綔涓殑姝ラ錛?/strong> 1錛屼慨鏀箂truts-config.xml 2錛?閰嶇疆a(chǎn)pplicationContext.xml 3錛?涓篈ction娣誨姞get/set鏂規(guī)硶 鏉ヨ幏寰椾緷璧栨敞鍏ョ殑鍔熻兘銆?/p>
public class LoginAction extends Action {
//鍒╃敤spring鏉ユ敞鍏ヨ繖涓璞°?
private LoginDao dao ;
public void setDao(LoginDao dao) {
System.out.println("鎵ц娉ㄥ叆");
this.dao = dao;
}
public LoginDao getDao() {
return dao;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
//榪欐牱涓鏀硅繖琛屼唬鐮佷技涔庢病鏈夊繀瑕佷簡銆?
//WebApplicationContext ctx = this.getWebApplicationContext();
//LoginDao dao = (LoginDao) ctx.getBean("loginDao");
User u = new User();
u.setName(loginForm.getName());
u.setPwd(loginForm.getPwd());
//鐩存帴鐢╠ao鏉ヨ皟鐢╯pring浼?xì)灏啒q欎釜瀵硅薄瀹炰緥鍖栥?
if(dao.checkLogin(u)){
return mapping.findForward("success");
}else{
return mapping.findForward("error");
}
}
}
榪欓噷鐨勩?
LoginAction extends Action 璇存槑 struts娌℃湁鍜宻pring 鑰﹀悎銆?
鐪嬩竴涓?
applicationContext.xml 涓殑閰嶇疆銆?
錛渂eans錛?
錛渂ean id="loginDao" class="com.cao.dao.LoginDao"/錛?
錛渂ean name="/login" class="com.cao.struts.action.LoginAction"錛?
錛減roperty name="dao"錛?
錛渞ef local="loginDao"/錛?
錛?property錛?
錛?bean錛?
錛?beans錛?
錛渟truts-config錛?
錛渄ata-sources /錛?
錛渇orm-beans 錛?
錛渇orm-bean name="loginForm"
type="com.cao.struts.form.LoginForm" /錛?
錛?form-beans錛?
錛済lobal-exceptions /錛?
錛済lobal-forwards /錛?
錛渁ction-mappings 錛?
錛?-- type鎸囧悜鐨勬槸spring 鐨勪唬鐞嗙被 --錛?
錛渁ction
attribute="loginForm"
input="login.jsp"
name="loginForm"
path="/login"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy" 錛?
錛渇orward name="success" path="/ok.jsp" /錛?
錛渇orward name="error" path="/error.jsp" /錛?
錛?action錛?
錛?action-mappings錛?
錛渕essage-resources parameter="com.cao.struts.ApplicationResources" /錛?
錛減lug-in className=
"org.springframework.web.struts.ContextLoaderPlugIn"錛?
錛渟et-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml"/錛?
錛?plug-in錛?
錛?struts-config錛?
涓嶅悓涔嬪
1錛?錛渁ction錛炰腑 type鎸囧悜鐨勬槸spring 鐨勪唬鐞嗙被
2錛?鍘繪帀struts-config.xml涓?錛渃ontroller 錛?
涓夌鏁村拰鏂瑰紡涓垜浠紭鍏堥夌敤 鍏ㄦ潈濮旀墭鐨勬柟寮忋?/p>