Struts2 杩炴帴鏁版嵁搴撴€荤粨

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2 杩炴帴鏁版嵁搴撴€荤粨相关的知识,希望对你有一定的参考价值。

鏍囩锛?/p>

浜嗚ВMVC妯″紡

MVC 鏄竴绉嶄娇鐢?/strong>MVC                         Model View Controller妯″瀷-瑙嗗浘-鎺у埗鍣?/strong>璁捐鍒涘缓Web 搴旂敤绋嬪簭鐨勬ā寮?/strong>[1] 

澶т竴鏃剁殑绗旇锛氱幇鍦ㄥ啓鎴愬崥瀹紝鏈変簺鐭ヨ瘑鐐圭湅鍒版兂绗戝搱鍝堬紝缁欏垰瀛︾殑鐪嬬湅鍚э紒

  •  
  •  
  •  
  •  

Model 妯″瀷 琛ㄧず搴旂敤绋嬪簭鏍稿績 姣斿鏁版嵁搴撹褰曞垪琛?/h2>

View 瑙嗗浘 鏄剧ず鏁版嵁 鏁版嵁搴撹褰?/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>

褰撴煇涓鑹?/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鐨勪綔鐢?/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>

鎶€鏈垎浜? src=

Java浠g爜锛?/strong>

鎶€鏈垎浜? src=

Struts.xml閰嶇疆锛?/strong>

鎶€鏈垎浜? src=

 

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>

 鎶€鏈垎浜? src=

Java浠g爜锛?/strong>

 鎶€鏈垎浜? src=

Struts.xml閰嶇疆

 鎶€鏈垎浜? src=

 

 

渚嬪瓙2锛?lt;a href="loginadd"></a>

Jsp椤甸潰锛?/strong>

 鎶€鏈垎浜? src= 

Java浠g爜锛?/strong>

 鎶€鏈垎浜? src=

struts.xml閰嶇疆锛?/strong>

 鎶€鏈垎浜? src=

 

渚嬪瓙3锛?lt;a href="Login_add"></a>

锛堟绉嶉摼鎺ヨ娉ㄦ剰澶у皬鍐欙級

Jsp椤甸潰锛?/strong>

 鎶€鏈垎浜? src=

Java浠g爜锛?/strong>

 鎶€鏈垎浜? src=

struts.xml閰嶇疆锛?/strong>

 鎶€鏈垎浜? src=

 

 

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>

 鎶€鏈垎浜? src=

 

Java浠g爜锛?/strong>

 

 鎶€鏈垎浜? src=

 

鎺ユ敹锛氶€氳繃瀵硅薄鐨勫睘鎬ф帴鏀跺弬鏁帮紝濡傦細user.getUserName()锛涗笉闇€瑕乶ew锛?/strong>

 鎶€鏈垎浜? src=

 

struts.xm閰嶇疆

 鎶€鏈垎浜? src=

 

 

渚嬪浘锛?/strong>

鎶€鏈垎浜? src=

 

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>

 鎶€鏈垎浜? src=

 

Java浠g爜锛?/strong>

 鎶€鏈垎浜? src=

 鎶€鏈垎浜? src=

 

struts.xml閰嶇疆

 鎶€鏈垎浜? src=

 

渚嬪浘锛?/h2>

 鎶€鏈垎浜? src=

 

Struts2涓庢暟鎹簱杩炴帴浜や簰

绫伙細

User.java  ------灏佽User鐨勪俊鎭紙濡傦細private銆乻etXxx銆乬etXxx锛夈€?/h2>

UserDAO.java-----User绠$悊绫伙紝鍚湁璋冪敤杩炴帴鏁版嵁搴撹繘琛屽鍒犳敼鏌ョ瓑鍔熻兘鎿嶄綔绫汇€?/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 杩炴帴鏁版嵁搴撴€荤粨的主要内容,如果未能解决你的问题,请参考以下文章

JDBC杩炴帴MySQL鎿嶄綔姝ラ

闈㈣瘯瀹橈細鏁版嵁搴撹繛鎺ユ睜涓轰粈涔堥兘瑕佺敤Threadlocal鍛紵

鎬ц兘娴嬭瘯鐢ㄤ緥璁捐

MyCat瀹炵幇鏁版嵁搴撲笌鏁版嵁搴撲箣闂寸殑璇诲啓鍒嗙