Struts2 杩炴帴鏁版嵁搴撴€荤粨
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2 杩炴帴鏁版嵁搴撴€荤粨相关的知识,希望对你有一定的参考价值。
鏍囩锛?/p>
浜嗚ВMVC妯″紡
MVC 鏄竴绉嶄娇鐢?/strong>MVC Model View Controller妯″瀷-瑙嗗浘-鎺у埗鍣?/strong>璁捐鍒涘缓Web 搴旂敤绋嬪簭鐨勬ā寮?/strong>[1]
澶т竴鏃剁殑绗旇锛氱幇鍦ㄥ啓鎴愬崥瀹紝鏈変簺鐭ヨ瘑鐐圭湅鍒版兂绗戝搱鍝堬紝缁欏垰瀛︾殑鐪嬬湅鍚э紒
Model 妯″瀷 琛ㄧず搴旂敤绋嬪簭鏍稿績 姣斿鏁版嵁搴撹褰曞垪琛?/h2>
View 瑙嗗浘 鏄剧ず鏁版嵁 鏁版嵁搴撹褰?/h2>
Controller 鎺у埗鍣?澶勭悊杈撳叆 鍐欏叆鏁版嵁搴撹褰?/h2>
Controller 鎺у埗鍣?澶勭悊杈撳叆 鍐欏叆鏁版嵁搴撹褰?/h2>
MVC 妯″紡鍚屾椂鎻愪緵浜嗗HTMLCSS 鍜孞avaScript 鐨勫畬鍏ㄦ帶鍒?/p>
Model 妯″瀷鏄簲鐢ㄧ▼搴忎腑鐢ㄤ簬澶勭悊搴旂敤绋嬪簭鏁版嵁閫昏緫鐨勯儴鍒嗛€氬父妯″瀷瀵硅薄璐熻矗鍦ㄦ暟鎹簱涓瓨鍙栨暟鎹?/p>
View 瑙嗗浘鏄簲鐢ㄧ▼搴忎腑澶勭悊鏁版嵁鏄剧ず鐨勯儴鍒?/strong> 閫氬父瑙嗗浘鏄緷鎹ā鍨嬫暟鎹垱寤虹殑
Controller 鎺у埗鍣?/strong>鏄簲鐢ㄧ▼搴忎腑澶勭悊鐢ㄦ埛浜や簰鐨勯儴鍒?/strong> 閫氬父鎺у埗鍣ㄨ礋璐d粠瑙嗗浘璇诲彇鏁版嵁鎺у埗鐢ㄦ埛杈撳叆骞跺悜妯″瀷鍙戦€佹暟鎹?/strong>
MVC 鍒嗗眰鏈夊姪浜庣鐞嗗鏉傜殑搴旂敤绋嬪簭鍥犱负鎮ㄥ彲浠ュ湪涓€涓椂闂村唴涓撻棬鍏虫敞涓€涓柟闈?/strong>渚嬪鎮ㄥ彲浠ュ湪涓嶄緷璧栦笟鍔¢€昏緫鐨勬儏鍐典笅涓撴敞浜庤鍥捐璁?/strong>鍚屾椂涔熻搴旂敤绋嬪簭鐨勬祴璇曟洿鍔犲鏄?/strong>
MVC 鍒嗗眰鍚屾椂涔熺畝鍖栦簡鍒嗙粍寮€鍙?/strong>涓嶅悓鐨勫紑鍙戜汉鍛樺彲鍚屾椂寮€鍙戣鍥?/strong>鎺у埗鍣ㄩ€昏緫鍜屼笟鍔¢€昏緫
瑙嗗浘
銆€銆€銆€銆€瑙嗗浘鏄敤鎴风湅鍒板苟涓庝箣浜や簰鐨勭晫闈?/strong>瀵硅€佸紡鐨?/strong>Web搴旂敤绋嬪簭鏉ヨ瑙嗗浘灏辨槸鐢?/strong>HTML鍏冪礌缁勬垚鐨勭晫闈?/strong>鍦ㄦ柊寮忕殑Web搴旂敤绋嬪簭涓?/strong>HTML渚濇棫鍦ㄨ鍥句腑鎵紨鐫€閲嶈鐨勮鑹?/strong>浣嗕竴浜涙柊鐨勬妧鏈凡灞傚嚭涓嶇┓瀹冧滑鍖呮嫭Adobe Flash鍜屽儚XHTMLXML/XSL,WML绛変竴浜涙爣璇嗚瑷€鍜?/strong>Web services.
銆€銆€銆€銆€MVC濂藉鏄畠鑳戒负搴旂敤绋嬪簭澶勭悊寰堝涓嶅悓鐨?/strong>瑙嗗浘鍦ㄨ鍥句腑鍏跺疄娌℃湁鐪熸鐨勫鐞嗗彂鐢?/strong>涓嶇杩欎簺鏁版嵁鏄仈鏈哄瓨鍌ㄧ殑杩樻槸涓€涓泧鍛樺垪琛?/strong>浣滀负瑙嗗浘鏉ヨ瀹冨彧鏄綔涓轰竴绉嶈緭鍑烘暟鎹苟鍏佽鐢ㄦ埛鎿嶇旱鐨勬柟寮?/strong>
妯″瀷
銆€銆€銆€銆€妯″瀷琛ㄧず浼佷笟鏁版嵁鍜屼笟鍔¤鍒?/strong>鍦?/strong>MVC鐨勪笁涓儴浠朵腑妯″瀷鎷ユ湁鏈€澶氱殑澶勭悊浠诲姟渚嬪瀹冨彲鑳界敤鍍?/strong>EJBs鍜?/strong>ColdFusion Components杩欐牱鐨勬瀯浠?/strong>瀵硅薄鏉ュ鐞?/strong>鏁版嵁搴?/a>琚ā鍨嬭繑鍥炵殑鏁版嵁鏄腑绔嬬殑灏辨槸璇存ā鍨嬩笌鏁版嵁鏍煎紡鏃犲叧杩欐牱涓€涓ā鍨嬭兘涓哄涓鍥炬彁渚涙暟鎹?/strong>鐢变簬搴旂敤浜庢ā鍨嬬殑浠g爜鍙渶鍐欎竴娆″氨鍙互琚涓鍥鹃噸鐢?/strong>鎵€浠ュ噺灏戜簡浠g爜鐨勯噸澶嶆€?/strong>
鎺у埗鍣?/h2>
銆€銆€銆€銆€鎺у埗鍣ㄦ帴鍙楃敤鎴风殑杈撳叆骞惰皟鐢ㄦā鍨嬪拰瑙嗗浘鍘诲畬鎴愮敤鎴风殑闇€姹?/strong>鎵€浠ュ綋鍗曞嚮Web椤甸潰涓殑瓒呴摼鎺ュ拰鍙戦€?/strong>HTML琛ㄥ崟鏃?/strong>鎺у埗鍣ㄦ湰韬笉杈撳嚭浠讳綍涓滆タ鍜屽仛浠讳綍澶勭悊瀹冨彧鏄帴鏀惰姹傚苟鍐冲畾璋冪敤鍝釜妯″瀷鏋勪欢鍘诲鐞嗚姹?/strong>鐒跺悗鍐嶇‘瀹氱敤鍝釜瑙嗗浘鏉ユ樉绀鸿繑鍥炵殑鏁版嵁
绠€鍗曟€荤粨MVC鐨勬蹇碉細
銆€銆€銆€銆€V锛氬鐞嗙畝鍗曠殑jsp椤甸潰锛堣鍥撅級
銆€銆€銆€銆€M锛氬悗鍙扮殑涓€浜?/strong>Model锛屽鐞嗗鎴风鍙戦€佺殑璇锋眰
銆€銆€銆€銆€C锛氬悇绉嶅悇鏍风殑action锛岀敱C鏉ユ帶鍒?/strong>V鍜?/strong>M涔嬮棿鐨勯€氳锛岀劧鍚庢妸Model鍜?/strong>View鐨勮€﹀悎搴﹁В寮€鏉ワ紒锛?ldquo;璋佹潵鎺у埗鎴戝埌鍝釜瑙嗗浘鍘?rdquo;锛?/strong>
鐧惧害涓句緥瀛愶細
銆€銆€銆€銆€閫氬父涓€鑸啓jsp绋嬪簭閮芥槸jsp璇锋眰 servlet 鐒跺悗servlet杩斿洖鏁版嵁缁?/strong>jsp,瀵瑰惂锛熻繖鏍风殑璇?/strong> servlet涓鏌ヨ鏁版嵁搴撳拰澶勭悊涓氬姟閫昏緫浜?/strong>...
銆€銆€銆€銆€浣嗘槸鍛紝杩欐牱鐨勮瘽servlet灏变細涔变竷鍏碂鐨勪粈涔堜唬鐮侀兘鏈変簡,瀵瑰惂锛?/strong>
銆€銆€銆€銆€杩欎釜鏃跺€欙紝鎴戝張鎯宠捣鏉?/strong>MVC妯″紡浜?/strong> ...jsp 灏辨槸澶╃敓鐨勫睍绀烘暟鎹拰杈撳叆鏁版嵁鐢ㄧ殑 ...鍙互鐪嬪仛MVC鐨?/strong> V銆傘€傘€傘€傘€傘€傘€傘€傘€傘€傘€?/strong>
銆€銆€銆€銆€servlet鍛紝杩欎釜鏃跺€欐垜鎶婂湪servlet涓繛鎺ユ暟鎹簱鐨勪唬鐮佺嫭绔嬪嚭鏉ユ斁鍒颁竴涓?/strong>java绫婚噷闈㈠幓,servlet鍙瑕佽皟鐢ㄨ繖涓被灏辩洿鎺ユ嬁鍒扮粨鏋滄樉绀虹粰jsp銆傝繖鏍凤紝servlet灏卞彉鎴?/strong>MVC鐨?/strong> C浜嗭紝鑰岄偅涓鐞嗘暟鎹簱鐨?/strong>java绫诲氨鍙樻垚MVC鐨?/strong> M浜?/strong>銆傘€傘€傘€傘€傘€?/strong>
鎺у埗鍙嶈浆涓庝緷璧栨敞鍏?/h1>
鍏充簬鎺у埗鍙嶈浆鍜屼緷璧栨敞鍏ョ殑鏂囩珷鍜屼功绫嶅緢澶氾紝瀵瑰叾瀹氫箟涔熻В閲婄殑涔熶粊鑰呰浠侊紝杩欓噷灏变笉璧樿堪浜嗭紝杩欐槸鏈汉锛堝彧浠h〃涓汉瑙傜偣锛夌悊瑙d箣鍚庣敤閫氫織鐨勪緥瀛愬拰骞虫贰鐨勮瘽璇嶄负鎮ㄨВ閲婏紝甯屾湜瀵规偍鏈夋墍甯姪锛?/h2>
鎺у埗鍙嶈浆锛圛oC/Inverse Of Control锛? 璋冪敤鑰呬笉鍐嶅垱寤鸿璋冪敤鑰呯殑瀹炰緥锛岀敱spring妗嗘灦瀹炵幇锛堝鍣ㄥ垱寤猴級鎵€浠ョО涓烘帶鍒跺弽杞€?/strong>
渚濊禆娉ㄥ叆(DI/Dependence injection) : 瀹瑰櫒鍒涘缓濂藉疄渚嬪悗鍐嶆敞鍏ヨ皟鐢ㄨ€呯О涓轰緷璧栨敞鍏ャ€?/strong>
鎺у埗鍙嶈浆锛圛oC/Inverse Of Control锛? 璋冪敤鑰呬笉鍐嶅垱寤鸿璋冪敤鑰呯殑瀹炰緥锛岀敱spring妗嗘灦瀹炵幇锛堝鍣ㄥ垱寤猴級鎵€浠ョО涓烘帶鍒跺弽杞€?/strong>
渚濊禆娉ㄥ叆(DI/Dependence injection) : 瀹瑰櫒鍒涘缓濂藉疄渚嬪悗鍐嶆敞鍏ヨ皟鐢ㄨ€呯О涓轰緷璧栨敞鍏ャ€?/strong>
褰撴煇涓鑹?/strong>(鍙兘鏄竴涓?/strong>Java瀹炰緥锛岃皟鐢ㄨ€?/strong>)闇€瑕佸彟涓€涓鑹?/strong>(鍙︿竴涓?/strong>Java瀹炰緥锛岃璋冪敤鑰?/strong>)鐨勫崗鍔╂椂锛屽湪浼犵粺鐨勭▼搴忚璁¤繃绋嬩腑锛岄€氬父鐢辫皟鐢ㄨ€呮潵鍒涘缓琚皟鐢ㄨ€呯殑瀹炰緥,銆傚鏋滃垱寤鸿璋冪敤鑰呭疄渚嬬殑宸ヤ綔涓嶅啀鐢辫皟鐢ㄨ€呮潵瀹屾垚锛岃€屾槸鐢卞閮ㄥ鍣ㄥ畬鎴愶紝鍥犳绉颁负鎺у埗鍙嶈浆; 鍒涘缓琚皟鐢ㄨ€?/strong> 瀹炰緥鐨勫伐浣滈€氬父鐢卞閮ㄥ鍣ㄦ潵瀹屾垚锛岀劧鍚庢敞鍏ヨ皟鐢ㄨ€咃紝鍥犳涔熺О涓轰緷璧栨敞鍏ャ€備笅闈竴涓皬瀹炰緥锛?/strong>
瀹氫箟涓€涓帴鍙?/strong>
public interface Person { void sayHello(); }
绗竴涓疄鐜扮被锛?/strong>
public class Chinese implements Person { public void sayHello() { System.out.println("鎮ㄥソ !"); } }
绗簩涓疄鐜扮被锛?/strong>
public class American implements Person { public void sayHello() { System.out.println("How do you do ."); } }
娉ㄦ剰杩欎釜绫讳笌浼犵粺璁捐鏈変粈涔堝尯鍒細璇ョ被璋冪敤Person瀛愮被鐨勬柟娉曪紝浼犵粺璁捐鍦ㄦ湰绫讳腑鍒涢€犲疄渚嬶紝鑰屽湪姝ょ被閲屽苟娌℃湁鍒涢€犲疄渚?/strong>
public class User {
Person p;
public Person getP() {
return p;
}
//浣跨敤setter娉ㄥ叆
public void setP(Person p) {
this.p = p;
}
//璋冪敤person瀛愮被閲嶅啓鐨?/strong>sayHello鏂规硶锛岃繖閲岀殑p骞舵病鏈夊疄渚嬪寲
public void function(){
p.sayHello();
}
}
澶栭儴‘瀹瑰櫒’
public class Container{
public static User getBean(){
Person p=new Chinese();
User user = new User();
//鐢卞鍣?lsquo;娉ㄥ叆’瀹炰緥
user.setP(p);
return user;
}
}
娴嬭瘯绫伙細
public class Test{
public static void main(String[] args){
User user = Container.getBean();
user.function();
}
}
//鍚庡彴杈撳嚭‘鎮ㄥソ’
閫氳繃杩欎釜渚嬪瓙搴旇鐪嬫噦浜嗘帶鍒跺弽杞紝鍜屼緷璧栨敞鍏ヤ簡鍚э紝杩欎釜鏄笉鏄笌浼犵粺璁捐鐩?lsquo;鍙嶄簡’銆?/strong>
鐩稿叧鐭ヨ瘑
渚濊禆鍜岃€﹀悎锛?/strong>Dependency and Coupling锛?/strong>
濡傛灉妯″潡A璋冪敤妯″潡B鎻愪緵鐨勬柟娉曪紝鎴栬闂ā鍧?/strong>B涓殑鏌愪簺鏁版嵁鎴愬憳锛堝綋鐒讹紝鍦ㄩ潰鍚戝璞″紑鍙戜腑涓€鑸笉鎻愬€¤繖鏍峰仛锛夛紝鎴戜滑灏辫涓烘ā鍧?/strong>A渚濊禆浜庢ā鍧?/strong>B锛屾ā鍧?/strong>A鍜屾ā鍧?/strong>B涔嬮棿鍙戠敓浜嗚€﹀悎銆傞偅涔堬紝渚濊禆瀵逛簬鎴戜滑鏉ヨ绌剁珶鏄ソ浜嬭繕鏄潖浜嬪憿锛熺敱浜庝汉绫荤殑鐞嗚В鍔涙湁闄愶紝澶у鏁颁汉闅句互鐞嗚В鍜屾妸鎻¤繃浜庡鏉傜殑绯荤粺銆傛妸杞欢绯荤粺鍒掑垎鎴愬涓ā鍧楋紝鍙互鏈夋晥鎺у埗妯″潡鐨勫鏉傚害锛屼娇姣忎釜妯″潡閮芥槗浜庣悊瑙e拰缁存姢銆備絾鍦ㄨ繖绉嶆儏鍐典笅锛屾ā鍧椾箣闂村氨蹇呴』浠ユ煇绉嶆柟寮忎氦鎹俊鎭紝涔熷氨鏄繀鐒惰鍙戠敓鏌愮鑰﹀悎鍏崇郴銆傚鏋滄煇涓ā鍧楀拰鍏跺畠妯″潡娌℃湁浠讳綍鍏宠仈锛堝摢鎬曞彧鏄綔鍦ㄧ殑鎴栭殣鍚殑渚濊禆鍏崇郴锛夛紝鎴戜滑灏卞嚑涔庡彲浠ユ柇瀹氾紝璇ユā鍧椾笉灞炰簬姝よ蒋浠剁郴缁燂紝搴旇浠庣郴缁熶腑鍓旈櫎銆傚鏋滄墍鏈夋ā鍧椾箣闂撮兘娌℃湁浠讳綍鑰﹀悎鍏崇郴锛屽叾缁撴灉蹇呯劧鏄細鏁翠釜杞欢涓嶈繃鏄涓簰涓嶇浉骞茬殑绯荤粺鐨勭畝鍗曞爢绉紝瀵规瘡涓郴缁熻€岃█锛屾墍鏈夊姛鑳借繕鏄鍦ㄤ竴涓ā鍧椾腑瀹炵幇锛岃繖绛変簬娌℃湁鍋氫换浣曟ā鍧楃殑鍒嗚В銆?/strong>
鍥犳锛屾ā鍧椾箣闂村繀瀹氫細鏈夎繖鏍锋垨閭f牱鐨勪緷璧栧叧绯伙紝姘歌繙涓嶈骞绘兂娑堥櫎鎵€鏈変緷璧栥€備絾鏄紝杩囧己鐨勮€﹀悎鍏崇郴锛堝涓€涓ā鍧楃殑鍙樺寲浼氶€犳垚涓€涓垨澶氫釜鍏朵粬妯″潡涔熷悓鏃跺彂鐢熷彉鍖栫殑渚濊禆鍏崇郴锛変細瀵硅蒋浠剁郴缁熺殑璐ㄩ噺閫犳垚寰堝ぇ鐨勫嵄瀹炽€傜壒鍒槸褰撻渶姹傚彂鐢熷彉鍖栨椂锛屼唬鐮佺殑缁存姢鎴愭湰灏嗛潪甯搁珮銆傛墍浠ワ紝鎴戜滑蹇呴』鎯冲敖鍔炴硶鏉ユ帶鍒跺拰娑堣В涓嶅繀瑕佺殑鑰﹀悎锛岀壒鍒槸閭g浼氬鑷村叾瀹冩ā鍧楀彂鐢熶笉鍙帶鍙樺寲鐨勪緷璧栧叧绯汇€備緷璧栧€掔疆銆佹帶鍒跺弽杞€佷緷璧栨敞鍏ョ瓑鍘熷垯灏辨槸浜轰滑鍦ㄥ拰渚濊禆鍏崇郴杩涜鑹拌嫤鍗撶粷鐨勬枟浜夎繃绋嬩腑涓嶆柇浜х敓鍜屽彂灞曡捣鏉ョ殑銆?/strong>
Struts2鏍囩鐭ヨ瘑锛?/h1>
Include鏍囩锛氬寘鍚殑鎰忔€濄€?/h2>
榛樿Action(default_Action): <default-action-ref name="index"></default-action-ref>
榛樿Action(default_Action): <default-action-ref name="index"></default-action-ref>
榛樿action鐨勪綔鐢?/span>锛氶」鐩悗闈㈤摼鎺ヨ緭鍏ヤ笉姝g‘浼氶摼鎺ュ埌鎸囧畾鐨勯粯璁ction閲岄潰銆?/strong>
Action鐨勮浆鍙?/h2>
鍏ㄥ眬鏍囩<global-results></global-results>
鍏ㄥ眬鏍囩鐨勪綔鐢細鎶婂叡鍚屾垨鑰呯浉鍚岄儴鍒嗘娊鍙栧嚭鏉ュ叡浜紝鍑忓皯閰嶇疆鐨勭箒鏉傘€傦紙鍥剧墖瀹炰緥涓烘娊鍙栧嚭鍏卞悓鐨勮繑鍥炵粨鏋滈〉闈級
extends鏍囩
extends鏍囩鐨勪綔鐢細缁ф壙鍏叡鐨勭壒鎬э紝鍑忓皯閰嶇疆鐨勭箒鏉傘€傦紙渚嬪锛氭潈闄愭ā鍧椼€佹敹閾舵ā鍧椼€佺鐞嗗憳妯″潡閮介渶瑕佷竴涓?/strong>404椤甸潰锛岀敤extends缁ф壙鎸囧畾鐨?/strong>package锛堝寘鍚嶏級鍑忓皯閰嶇疆銆傦級
鍔ㄦ€佺殑缁撴灉锛坮esult锛?/h2>
redirect 閲嶅畾鍚?/h2>
redirect:action澶勭悊瀹屽悗閲嶅畾鍚戝埌涓€涓鍥捐祫婧愶紙濡傦細jsp椤甸潰锛夛紝璇锋眰鍙傛暟鍏ㄩ儴涓㈠け锛宎ction澶勭悊缁撴灉涔熷叏閮ㄤ涪澶便€?/strong>鍙互閫氳繃杩斿洖缁撴灉鍚庨潰甯﹀弬鏁帮紝濡備笅锛?/strong>
<a href=“”></a>瓒呴摼鎺ュ甫鍙備紶鍊?/h2>
濡備笅锛?/strong>
Jsp椤甸潰锛?/strong>
Java浠g爜锛?/strong>
Struts.xml閰嶇疆锛?/strong>
Struts2鍔ㄦ€佹柟娉曠殑璁块棶
锛堜笅闈㈢粰鍑虹殑3涓緥瀛愭敞鎰忎笉鍚屼箣澶勫拰鍖哄埆鍦ㄥ摢锛?/strong>
璇ュ睘鎬ц缃甋truts2 鏄惁鏀寔鍔ㄦ€佹柟娉曡皟鐢紝璇ュ睘鎬х殑榛樿鍊兼槸true銆傚鏋滈渶瑕佸叧闂姩鎬佹柟娉曡皟鐢紝鍒欏彲璁剧疆璇ュ睘鎬т负false銆備互涓嬭缃弬鏁板€煎彲浠ュ湪struts2-core.jar涓璷rg.apache.struts涓嬬殑default.properties鏂囦欢涓煡鍒?/p>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
渚嬪瓙1锛?lt;a href="login!add"></a>
Jsp椤甸潰锛?/strong>
Java浠g爜锛?/strong>
Struts.xml閰嶇疆
渚嬪瓙2锛?lt;a href="loginadd"></a>
Jsp椤甸潰锛?/strong>
Java浠g爜锛?/strong>
struts.xml閰嶇疆锛?/strong>
渚嬪瓙3锛?lt;a href="Login_add"></a>
锛堟绉嶉摼鎺ヨ娉ㄦ剰澶у皬鍐欙級
Jsp椤甸潰锛?/strong>
Java浠g爜锛?/strong>
struts.xml閰嶇疆锛?/strong>
DomainModel(鍩熸ā鍨?
浣跨敤DomainModel(鍩熸ā鍨?鎺ユ敹鍙傛暟
銆€銆€銆€銆€a.瀹氫箟锛氬畾涔塎odel绫伙紝鍦ˋction涓畾涔塎odel绫荤殑瀵硅薄锛堜笉闇€瑕乶ew锛夛紝鍒涘缓璇ュ璞$殑get鍜宻et鏂规硶锛?
銆€銆€銆€銆€b.鎺ユ敹锛氶€氳繃瀵硅薄鐨勫睘鎬ф帴鏀跺弬鏁帮紝濡傦細user.getUserName()锛?
銆€銆€銆€銆€c.鍙戦€?/strong>锛氫娇鐢ㄥ璞$殑灞炴€т紶閫掑弬鏁帮紝濡傦細user2!add?user.userName=MGC锛?/strong>
渚嬪瓙锛?/h2>
Jsp椤甸潰锛?/strong>
鍙戦€侊細浣跨敤瀵硅薄鐨勫睘鎬т紶閫掑弬鏁帮紝濡傦細user2!add?user.userName=MGC锛?/strong>
Java浠g爜锛?/strong>
鎺ユ敹锛氶€氳繃瀵硅薄鐨勫睘鎬ф帴鏀跺弬鏁帮紝濡傦細user.getUserName()锛涗笉闇€瑕乶ew锛?/strong>
struts.xm閰嶇疆
渚嬪浘锛?/strong>
DomainModel浣滅敤锛?/h2>
杩欑鏂规硶鏄垜浠紑鍙戦」鐩椂鏈€涓哄父鐢ㄧ殑锛屽浜庝竴涓睘鎬х浉瀵规瘮杈冨鐨勬椂鍊欙紝濡傛灉鎶婂睘鎬ч兘鍐欏埌Action涓紝閭d細鎶夾cton绫诲紕鐨勫緢涔憋紝鎵€浠ュ睘鎬ц緝澶氱殑鏃跺€欐渶濂介€夋嫨绗簩绉嶆柟寮忔潵鎺ユ敹鐢ㄦ埛杈撳叆鍙傛暟銆?/strong>
ModelDriven(妯″瀷椹卞姩)
ModelDriven瀹氫箟锛?/h2>
鍦ㄩ〉闈腑锛岃繖涓ā鍨嬪璞′腑鐨勫睘鎬у彲浠ョ洿鎺ラ€氳繃灞炴€у悕鏉ヨ闂紙濡倁sername锛?/strong>锛岃€屼笉闇€瑕佷娇鐢ㄥ舰濡?ldquo;user.username”杩欑鏍煎紡鐨勮〃杈惧紡銆?/strong>
ModelDriven浣滅敤锛?/h2>
杩欑鏂规硶鏄垜浠紑鍙戦」鐩椂鏈€涓哄父鐢ㄧ殑锛屽浜庝竴涓睘鎬х浉瀵规瘮杈冨鐨勬椂鍊欙紝濡傛灉鎶婂睘鎬ч兘鍐欏埌Action涓紝閭d細鎶夾cton绫诲紕鐨勫緢涔憋紝鎵€浠ュ睘鎬ц緝澶氱殑鏃跺€欐渶濂介€夋嫨姝ゆ柟寮忔潵鎺ユ敹鐢ㄦ埛杈撳叆鍙傛暟銆?/strong>
ModelDriven娉ㄦ剰瑕佺偣锛?/h2>
鍦╝ction涓篃涓嶉渶瑕佷负杩欎釜妯″瀷瀵硅薄鎻愪緵JavaBean椋庢牸鐨刧etter鍜宻etter鏂规硶銆備絾鏄繀椤昏鍦╝ction涓繘琛宯ew鎿嶄綔鍜屽疄鐜癵etModel鏂规硶骞惰繑鍥炵粰娉涘瀷瀵硅薄锛孧odelDriven瑕佷娇鐢ㄦ硾鍨嬪摝锛?/strong>
渚嬪瓙锛?/h2>
Jsp椤甸潰锛?/strong>
Java浠g爜锛?/strong>
struts.xml閰嶇疆
渚嬪浘锛?/h2>
Struts2涓庢暟鎹簱杩炴帴浜や簰
绫伙細
User.java ------灏佽User鐨勪俊鎭紙濡傦細private銆乻etXxx銆乬etXxx锛夈€?/h2>
UserDAO.java-----User绠$悊绫伙紝鍚湁璋冪敤杩炴帴鏁版嵁搴撹繘琛屽鍒犳敼鏌ョ瓑鍔熻兘鎿嶄綔绫汇€?/h2>
DBConn.java-----杩炴帴鏁版嵁搴撶殑绫汇€?/h2>
UserLoginAction.java-----灏佽鑾峰彇椤甸潰璇锋眰淇℃伅鐨凙ction绫汇€?/h2>
DBConn.java-----杩炴帴鏁版嵁搴撶殑绫汇€?/h2>
UserLoginAction.java-----灏佽鑾峰彇椤甸潰璇锋眰淇℃伅鐨凙ction绫汇€?/h2>
鏁翠釜璇锋眰鍒拌繑鍥炵殑娴佺▼锛?/h2>
棣栧厛UserLoginAction绫昏幏鍙栧鎴风鐢ㄦ埛鍙戣繃鏉ョ殑璇锋眰淇℃伅锛?/strong>name锛?/strong>password銆傚湪鏈被瀵逛笌瀹㈡埛绔氦浜掔殑鏌愪簺淇℃伅杩涜灏佽銆?/strong>setXxx鍜?/strong>geXtxx鏂规硶锛夛紝鎺ョ潃UserLoginAction绫讳細璋冪敤UserDAO绫荤殑checkUser(name,password)鏂规硶杩涜鏁版嵁搴撶殑鏌ユ壘锛?/strong>checkUser鏂规硶閲屼細璋冪敤DBConn绫荤殑getConn锛堬級鏂规硶杩涜鏁版嵁搴撶殑鍔犺浇鍜岃繛鎺ユ搷浣滐紝鍊樿嫢杩炴帴鎴愬姛锛?/strong>UserDAO.preparedStatement..executeQuery()鏂规硶杩涜璇彞鏌ユ壘锛夈€傛妸鎵惧埌鍖归厤鐨勪俊鎭祴鍊肩粰new User锛堣€?/strong>User閲屽彲浠ラ€氳繃User.setName(rs.getString(“name”))鏂规硶鎶婃嬁鍒扮殑淇℃伅鏀惧埌setName鏂规硶閲屻€傦級锛屾渶鍚庯紝checkUser鏂规硶杩斿洖涓€涓?/strong>User锛?/strong>UserLoginAction绫婚噷鐨?/strong>execute锛堬級鏂规硶瀹氫箟涓€涓?/strong>User鏉ヨ幏鍙?/strong>checkUser锛?/strong>name,password锛?/strong>鏂规硶杩斿洖鐨?/strong>User銆?/strong>
以上是关于Struts2 杩炴帴鏁版嵁搴撴€荤粨的主要内容,如果未能解决你的问题,请参考以下文章