struts2权威指南


㸡⫳њ Struts 2DŽ಴Ў Struts 2 ᰃ WebWork ⱘछ㑻ˈ㗠ϡᰃϔϾܼᮄⱘḚᶊˈ಴ℸ〇ᅮᗻǃ 㒳ˈ㗠ᰃ㒻ᡓњ WebWork ⱘ㸔㒳DŽ៪㗙䇈ˈWebWork 㸡⫳ߎњ Struts 2ˈ㗠ϡᰃ Struts 1 ⱘḚᶊ˖WebWork ෎⸔Ϟথሩ䍋ᴹⱘDŽҢᶤ⾡⿟ᑺϞᴹ䆆ˈStrut2 ≵᳝㒻ᡓ Struts 1 ⱘ㸔 1 Ⳍ↨ˈ⹂ᅲ᳝ᕜ໮䴽ੑᗻⱘᬍ䖯ˈԚᅗᑊϡᰃᮄথᏗⱘᮄḚᶊˈ㗠ᰃ೼঺ϔϾ䌿䌿᳝ৡ 㱑✊ Struts 2 ো⿄ᰃϔϾܼᮄⱘḚᶊˈԚ䖭ҙҙᰃⳌᇍ Struts 1 㗠㿔DŽStruts 2 Ϣ Struts 䖭гᰃヨ㗙ϡ᥼㤤ᓔথ㗙㞾Ꮕᅲ⦄Ḛᶊⱘॳ಴DŽ ߮߮䍋ℹⱘḚᶊˈৃ㛑ᴀ䑿ህᄬ೼ϔѯ䱤㮣ⱘ䯂乬ˈӮᇚ㞾䑿ⱘ BUG ᓩܹ㞾Ꮕⱘᑨ⫼DŽ ᇍѢӕϮᅲ䰙Փ⫼ MVC Ḛᶊ㗠㿔ˈḚᶊⱘ〇ᅮᗻ߭ᑨ䆹ᰃ᳔ؐᕫ㗗㰥ⱘ䯂乬DŽϔϾ 䡈ᛣНDŽ׳ϔѯ䖍㓬ಶ䯳ⱘ MVC Ḛᶊгᕜ᳝ JSFǃTapestry ੠ Spring MVC ㄝDŽ䰸њ䖭ѯ᳝ৡⱘ MVC Ḛᶊ໪ˈ䖬᳝ ڣḚᶊথᏗˈ՟བ Ⳃࠡˈ෎Ѣ Web ⱘ MVC Ḛᶊ䴲ᐌ໮ˈথሩгᕜᖿˈ↣䱨ϔ↉ᯊ䯈ህ᳝ϔϾᮄⱘ MVC ϞՓ⫼᳔ᑓ⊯ⱘ MVC ḚᶊDŽ 1 Ḛᶊ᳈ࡴ៤❳ǃ〇ᅮˈᗻ㛑г᳝њᕜདⱘֱ䆕DŽ಴ℸˈࠄⳂࠡЎℶˈStruts 1 ձ✊ᰃϪ⬠ 䆹Ḛᶊϔ㒣᥼ߎˈህᕫࠄњϪ⬠Ϟ Java Web ᓔথ㗙ⱘᢹᡸˈ㒣䖛䭓䖒 6 ᑈᯊ䯈ⱘ䫸⚐ˈStruts Struts 1 ᰃܼϪ⬠㄀ϔϾথᏗⱘ MVC Ḛᶊˈᅗ⬅ Craig McClanahan ೼ 2001 ᑈথᏗˈ  Struts 1 ੠ Struts 2 ⱘᇍ↨  Struts 2 ⱘ᥻ࠊ఼㒘ӊ  Struts 2 ⱘᷛㅒᑧ  Struts 2 ⱘḚᶊᶊᵘ  Struts 2 ⱘ䍋⑤  WebWork ⱘ෎ᴀ㒧ᵘ  Struts 1 ⱘ෎ᴀ㒧ᵘঞ݊ᄬ೼ⱘ䯂乬  ᐌ⫼ MVC Ḛᶊঞ݊⡍ᕕ  MVC ῵ᓣⱘӬ࢓  MVC ᗱᛇ  Model 1 ੠ Model 2  Web ᑨ⫼ⱘথሩ ᴀゴ㽕⚍ Struts 2 ὖ䗄 ㄀ゴ 2 ḍ᥂䆹ࡼᗕ⿟ᑣᴹ⫳៤ᇍᅶ᠋ッⱘડᑨDŽ ⱘ⿟ᑣˈ៪㗙ϔϾࡼᗕ义䴶ˈᔧᅶ᠋ッ৥䆹ࡼᗕ⿟ᑣ៪ࡼᗕ义䴶থ䗕䇋∖ᯊˈWeb ᳡ࡵ఼ ᅲ䰙Ϟˈ䖭ᰃৢᴹ᠔᳝ⱘࡼᗕ Web 㓪⿟ᡔᴃ᠔Փ⫼ⱘ῵ൟˈ䖭⾡῵ൟ䛑䳔㽕ϔϾࡼᗕ ḍ᥂ Servlet ⫳៤ᇍᅶ᠋ッⱘડᑨDŽ ℷབ೒ 1.1 ᠔ᰒ⼎ⱘˈ⌣㾜఼৥ Web ᳡ࡵ఼ݙᣛᅮⱘ Servlet থ䗕䇋∖ˈWeb ᳡ࡵ఼ ೒ 1.1 ᰒ⼎њ Servlet ⱘડᑨ⌕⿟DŽ ᠋ッⱘડᑨݙᆍDŽ ᅮњ᳡ࡵ఼ᇍᅶއ䆹 Web ᳡ࡵ఼䋳䋷໘⧚໮㒓⿟ǃ㔥㒰䗮ֵㄝࡳ㛑ˈ㗠 Servlet ⱘݙᆍ߭ 㾜఼䇋∖ˈᔧ⫼᠋৥ᣛᅮ URL ᇍᑨⱘ Servlet থ䗕䇋∖ᯊˈ䆹䇋∖㹿 Web ᳡ࡵ఼᥹ᬊࠄˈ Servlet ೼ Web ᑨ⫼Ё㹿᯴ᇘ៤ϔϾ URL˄㒳ϔ䌘⑤ᅮԡ˅ˈ䆹 URL ৃҹ㹿ᅶ᠋ッ⌣ ੠ᗻ㛑DŽ ࣪ˈᑊᇍᅶ᠋ッᦤկ໮㒓⿟ǃ㔥㒰䗮ֵㄝࡳ㛑ˈ䖭䛑ֱ䆕 Servlet ᳝᳈དⱘ〇ᅮᗻ  䰸ℸП໪ˈServlet 䖤㸠೼ Web ᳡ࡵ఼Ёˈ⬅ Web ᳡ࡵ఼এ䋳䋷ㅵ⧚ Servlet ⱘᅲ՟ ㄝDŽৠᯊˈJava 䇁㿔ᦤկњЄᆠⱘ㉏ᑧˈ䖭ѯ䛑ㅔ࣪њ Servlet ⱘᓔথDŽ ߚ߽⫼ Java 䇁㿔ⱘӬ࢓ˈ՟བ JDBC˄Java DataBase Connection˅ܙ Servlet ᑨ⫼ৃҹ 催њࡼᗕ Web ᑨ⫼ⱘᗻ㛑DŽ  Servlet ᰃ෎Ѣ Java 䇁㿔߯ᓎⱘˈ㗠 Java 䇁㿔߭ݙᓎњ໮㒓⿟ᬃᣕˈ䖭ϔ⚍໻໻ᦤ ⱘЏ㽕ᓔথᡔᴃDŽⳌ↨Ӵ㒳ⱘ CGI ᑨ⫼㗠㿔ˈServlet ݋᳝໻䞣ⱘӬ࢓˖ ᄬ೼䰤ࠊDŽࠄ 1997 ᑈᯊˈ䱣ⴔ Java 䇁㿔ⱘᑓ⊯Փ⫼ˈServlet ᡔᴃ䖙䗳៤Ўࡼᗕ Web ᑨ⫼ ೼ᕜ໮㔎⚍ˈ݊Ё᳔໻ⱘ㔎⚍ህᰃᓔথࡼᗕ Web ᑨ⫼䲒ᑺ䴲ᐌ໻ˈ㗠Ϩ೼ᗻ㛑ㄝ৘ᮍ䴶г CGI ᡔᴃᓔਃњࡼᗕ Web ᑨ⫼ⱘᯊҷˈ㒭њ䖭⾡ᡔᴃ᮴䰤ⱘৃ㛑ᗻDŽԚ CGI ᡔᴃᄬ ৃҹ㦋প⫼᠋ⱘ䇋∖খ᭄ˈᑊᇚ䖭ѯখ᭄ֱᄬࠄ᭄᥂ᑧ䞠DŽ 㽕Փ⫼䴭ᗕⱘ HTML 义䴶DŽCGI ᡔᴃৃҹҢ᭄᥂ᑧ䇏পֵᙃˈᇚ䖭ѯֵᙃਜ⦄㒭⫼᠋˗䖬 㔥݇᥹ষ˅ᡔᴃ⒵䎇њ䆹㽕∖ˈCGI ᡔᴃՓᕫ Web ᑨ⫼ৃҹϢᅶ᠋ッ⌣㾜఼ѸѦˈϡݡ䳔 ᕜ㞾✊ഄˈҎӀᏠᳯ Web ᑨ⫼䞠ᑨ䆹ࣙ৿ϔѯ㛑ࡼᗕᠻ㸠ⱘ义䴶ˈ᳔ᮽⱘ CGI˄䗮⫼ ೼ HTML 义䴶䞠ⱘˈ䆹义䴶ϡ݋໛Ϣ⫼᠋ѸѦⱘ㛑࡯ˈ≵᳝ࡼᗕᰒ⼎ⱘࡳ㛑DŽ ᑨ⫼ܼ䚼ᰃ䴭ᗕⱘ HTML 义䴶ˈ⫼Ѣᇚϔѯ᭛ᴀֵᙃਜ⦄㒭⌣㾜㗙ˈԚ䖭ѯֵᙃᰃ೎ᅮݭ 䱣ⴔ Internet ᡔᴃⱘᑓ⊯Փ⫼ˈWeb ᡔᴃᏆ㒣ᑓ⊯ᑨ⫼Ѣ Internet ϞˈԚᮽᳳⱘ Web 1.1.1 Web ᡔᴃⱘথሩ ⫼ⱘথሩग़৆੠ MVC ᗱᛇDŽ ҟ㒡 Web ᑨܜ᳝ᕜདⱘᅲ⫼Ӌؐⱘ Web MVC ḚᶊDŽҟ㒡 Struts MVC ḚᶊПࠡˈ៥Ӏ佪 ࡵ఼ህᰃ Web ᳡ࡵ఼DŽৃ㾕ˈWeb ᑨ⫼ᰃⳂࠡᑓ⊯Փ⫼ⱘᑨ⫼῵ᓣˈ㗠 Struts 2 ᰃϔϾ݋ Ҟ໽ˈ៥Ӏ㾕ࠄⱘ㒱໻䚼ߚᑨ⫼ˈ䛑ᰃ෎Ѣ B/S˄⌣㾜఼/᳡ࡵ఼˅ᶊᵘⱘˈ݊Ёⱘ᳡ 1.1 MVC ᗱᛇὖ䗄 Ͼ䴲ᐌؐᕫᳳᕙⱘḚᶊDŽ ᗻ㛑ㄝ৘ᮍ䴶䛑᳝ᕜདⱘֱ䆕˗㗠Ϩ਌ᬊњ Struts 1 ੠ WebWork ϸ㗙ⱘӬ࢓ˈ಴ℸˈᰃϔ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 3 ⱘ亢䰽ˈᅠܼ᳝ৃ㛑ᓩܹᮄⱘ䫭䇃DŽ 䴭ᗕⱘ᭛ᴀݙᆍˈгϡᕫϡᠧᓔ⏋ᴖⱘࡼᗕ㛮ᴀⱘ义䴶⑤᭛ӊ䖯㸠ׂᬍüü䖭ᰃϔ⾡ᕜ໻ ᗕ Web ᑨ⫼ⱘ㓈ᡸ៤ᴀ䍞ᴹ䍞໻ˈेՓা䳔㽕ׂᬍ䆹义䴶ⱘϔϾㅔऩᣝ䪂᭛ᴀˈ៪㗙ϔ↉ 䱣ⴔᅲ䰙 Web ᑨ⫼ⱘՓ⫼䍞ᴹ䍞ᑓ⊯ˈWeb ᑨ⫼ⱘ㾘῵г䍞ᴹ䍞໻ˈᓔথҎਬথ⦄ࡼ ⦄ⱘᛣНüü԰ЎϔϾ੠ ASP ᇍᡫⱘᡔᴃˈㅔऩህᰃ JSP ⱘ᳔໻Ӭ࢓DŽ Ӏⱘᅲ䋼ᅠܼϔḋDŽৃҹ䖭ḋ⧚㾷˖JSP ᰃϔ⾡᳈ㅔऩⱘ Servlet ᡔᴃˈ䖭гᰃ JSP ᡔᴃߎ ᇍ↨೒ 1.1 ੠೒ 1.2ˈথ⦄ϡ䆎ᰃ Servlet ࡼᗕ Web ᡔᴃˈ䖬ᰃ JSP ࡼᗕ Web ᡔᴃˈᅗ ೒ 1.2 JSP ⱘ䖤㸠ᴎ⧚ JSP ⱘ䖤㸠ᴎ⧚བ೒ 1.2 ᠔⼎DŽ 䖭ህᰃ䇈˖JSP ⱘᅲ䋼䖬ᰃ ServletDŽϡ䖛ˈкݭ JSP ↨кݭ Servlet ㅔऩᕫ໮DŽ ϡᰃ㾷䞞ൟⱘ㛮ᴀ䇁㿔ˈ಴ℸ JSP 义䴶ᑊϡ㛑ゟेᠻ㸠DŽ಴ℸˈJSP ᖙ乏㓪䆥៤ Servletˈ VBScript ᰃϔ⾡㛮ᴀ䇁㿔ˈ᮴䳔㓪䆥ˈ㗠 JSP Փ⫼ Java ԰Ў㛮ᴀ䇁হüüԚ Java Ңᴹህ ASP ੠ JSP Ңৡ⿄ϞབℸⳌԐˈԚᅗӀⱘ䖤㸠ᴎࠊᄬ೼ϔѯᏂ߿ˈ䖭Џ㽕ᰃ಴Ў 䰉㧹Ўњᇍᡫ ASP ᥼ߎⱘϔ⾡ࡼᗕ Web 㓪⿟ᡔᴃDŽ Ңᶤ⾡⿟ᑺϞᴹⳟˈJSP ᰃ Javaˈޚ䱣ৢˈ⬅ Sun ᏺ乚ⱘ Java 䰉㧹ˈゟेথᏗњ JSP ᷛ Ϟ៤Ўᑨ⫼᳔ᑓ⊯ⱘࡼᗕ Web ᓔথᡔᴃDŽ ໮ⱘ VB ⿟ᑣਬˈ䖭ѯ VB ⿟ᑣਬৃҹᕜ䕏ᯧഄ䖛⏵៤ ASP ⿟ᑣਬüü಴ℸˈASP ᡔᴃ偀 ⫼ VBScript ԰Ў㛮ᴀ䇁㿔ˈᅗⱘ䇁⊩ㅔऩǃᓔথᬜ⥛䴲ᐌ催DŽ㗠ϨˈϪ⬠ϞᏆ㒣᳝њ䴲ᐌ ᔧ ASP ᡔᴃ೼Ϫ⬠Ϟᑓ⊯⌕㸠ᯊˈҎӀᕜᖿᛳফࠄ䖭⾡ㅔऩⱘᡔᴃⱘ儙࡯˖ASP Փ ᡔᴃᓔྟⳳℷ԰Ўࡼᗕ Web 㓪⿟ᡔᴃDŽ ೼ ASP ⿟ᑣݙ䚼ⱘ᠔᳝㒘ӊ䛑᳝њ⣀ゟⱘݙᄬぎ䯈ˈᑊৃҹ䖯㸠џࡵ໘⧚DŽᷛᖫⴔ ASP IIS 4.0 ⱘ໪᥹ᓣ䰘ӊDŽᅗϢ ASP 1.0 ⱘЏ㽕ऎ߿೼Ѣᅗⱘ໪䚼㒘ӊᰃৃҹ߱ྟ࣪ⱘˈ䖭ḋˈ ࠄњ 1998 ᑈˈᖂ䕃থᏗњ ASP 2.0DŽᅗᰃ Windows NT 4 Option Pack ⱘϔ䚼ߚˈ԰Ў ೒ 1.1 Servlet ⱘડᑨ⌕⿟ ㄀ 1 ゴ Struts 2 ὖ䗄 4 ݊݋ԧⱘᅲ⦄ᮍᓣབ೒ 1.5 ᠔⼎DŽ 䇗⫼ৢッ JavaBean ᴹᅠ៤ᅲ䰙ⱘ䘏䕥໘⧚˗᳔ৢˈ䕀থࠄⳌᑨⱘ JSP 义䴶໘⧚ᰒ⼎䘏䕥DŽ ఼ˈ䋳䋷᥹ᬊᅶ᠋ッথ䗕ⱘ䇋∖ˈ೼ Servlet Ёাࣙ৿᥻ࠊ䘏䕥੠ㅔऩⱘࠡッ໘⧚˗✊ৢˈ Model 2 Ꮖ㒣ᰃ෎Ѣ MVC ᶊᵘⱘ䆒䅵῵ᓣDŽ೼ Model 2 ᶊᵘЁˈServlet ԰Ўࠡッ᥻ࠊ ᶊᵘDŽ ᮽᳳ᳝໻䞣 ASP ੠ JSP ᡔᴃᓔথߎᴹⱘ Web ᑨ⫼ˈ䖭ѯ Web ᑨ⫼䛑䞛⫼њ Model 1 ⏋ᴖ೼ϔ䍋ˈҢ㗠ᇐ㟈ҷⷕⱘ䞡⫼ᗻ䴲ᐌԢˈ๲ࡴњᑨ⫼ⱘᠽሩᗻ੠㓈ᡸⱘ䲒ᑺDŽ ᅗⱘሔ䰤ᗻ䴲ᐌᯢᰒ˖JSP 义䴶䑿ݐ View ੠ Controller ϸ⾡㾦㡆ˈᇚ᥻ࠊ䘏䕥੠㸼⦄䘏䕥 Model 1 ῵ᓣⱘᅲ⦄↨䕗ㅔऩˈ䗖⫼Ѣᖿ䗳ᓔথᇣ㾘῵乍ⳂDŽԚҢᎹ⿟࣪ⱘ㾦ᑺⳟˈ ೒ 1.4 ᰒ⼎њ Model 1 ⱘ⿟ᑣ⌕⿟DŽ JavaBean ᴹ໘⧚᭄᥂ᑧ䖲᥹ǃ᭄᥂ᑧ䆓䯂ㄝ᪡԰DŽ ߎડᑨDŽ⫼ᇥ䞣ⱘخ⬅ JSP 义䴶㒘៤ˈJSP 义䴶᥹ᬊ໘⧚ᅶ᠋ッ䇋∖ˈᇍ䇋∖໘⧚ৢⳈ᥹ ᠔䇧 Model 1 ህᰃ JSP ໻㸠݊䘧ⱘᯊҷˈ೼ Model 1 ῵ᓣϟˈᭈϾ Web ᑨ⫼޴Тܼ䚼 ೒ 1.3 ࡼᗕ Web 㓪⿟ᡔᴃⱘথሩग़৆ ᇍѢ Java 䰉㧹ⱘࡼᗕ Web 㓪⿟ᡔᴃ㗠㿔ˈ߭㒣ग़њ᠔䇧ⱘ Model 1 ੠ Model 2 ᯊҷDŽ 1.1.2 Model 1 ੠ Model 2 ࡼᗕ Web 㓪⿟ᡔᴃ໻㟈᳝བ೒ 1.3 ᠔⼎ⱘ䏃㒓DŽ ⫼㓈ᡸೄ䲒ⱘ䯂乬DŽ Web ᑨ އ㋻䎳ⴔᖂ䕃гথᏗњ ASP.NET ᡔᴃˈᅗӀ䛑䞛⫼ϔ⾡Ӭ⾔ⱘߚሖᗱᛇˈ࡯೒㾷 䱣ৢህᰃ Java 䰉㧹থᏗњϔ༫ᅠᭈⱘӕϮᓔথ㾘㣗˖J2EE˄⦄೼Ꮖ㒣᳈ৡЎ Java EE˅ˈ ῵ᓣDŽ ⱘ䗝ᢽˈ䖭ᇍѢৢᳳⱘ㓈ᡸⳌᔧϡ߽DŽ᜶᜶ഄᓔথҎਬᓔྟ೼ Web ᓔথЁՓ⫼ MVC ᔧ䖛໮㾦㡆ᰃⳌᔧ༅䋹ܙ䖭Ͼᯊ׭ˈҎӀᛣ䆚ࠄ˖Փ⫼ऩ㒃ⱘ ASPˈ៪ 㗙 JSP 义䴶 Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 5 䍋߱ˈMVC ῵ᓣᰃ䩜ᇍⳌৠⱘ᭄᥂䳔㽕ϡৠᰒ⼎ⱘᑨ⫼㗠䆒䅵ⱘˈ݊ᭈԧⱘᬜᵰབ ˄᥻ࠊ఼˅ˈ䖭ϝϾ䚼ߚҹ᳔ᇥⱘ㗺ড়णৠᎹ԰ˈҢ㗠ᦤ催ᑨ⫼ⱘৃᠽሩᗻঞৃ㓈ᡸᗻDŽ MVC ᗱᛇᇚϔϾᑨ⫼ߚ៤ϝϾ෎ᴀ䚼ߚ˖Model˄῵ൟ˅ǃView˄㾚೒˅੠ Controller ᳝䴶৥ᇍ䈵⿟ᑣ䆒䅵䇁㿔䛑ᑨ䆹䙉ᅜⱘ㾘㣗DŽ MVC ᑊϡᰃ Java 䇁㿔᠔⡍᳝ⱘ䆒䅵ᗱᛇˈгᑊϡᰃ Web ᑨ⫼᠔⡍᳝ⱘᗱᛇˈᅗᰃ᠔ 1.1.3 MVC ᗱᛇঞ݊Ӭ࢓ ᳈催໡ᴖᑺDŽ ⾡⿟ᑺϞ䆆ˈModel 2 Ўњ䰡Ԣ㋏㒳ৢᳳ㓈ᡸⱘ໡ᴖᑺˈैᇐ㟈ࠡᳳᓔথⱘ Model 2 ᦤկњ᳈དⱘৃᠽሩᗻঞৃ㓈ᡸᗻˈԚ๲ࡴњࠡᳳᓔথ៤ᴀDŽҢᶤ ৃҹՓ⫼ Model 1 ⱘ῵ᓣᴹᓔথᑨ⫼ˈ㗠ϡᰃՓ⫼ Model 2 ⱘ῵ᓣDŽ㱑✊ 㰩ぐ ᇍѢ䴲ᐌᇣൟⱘ Web キ⚍ˈབᵰৢᳳⱘ᳈ᮄǃ㓈ᡸᎹ԰ϡᰃ⡍߿໻ˈ Model 2 Ꮖ㒣ᰃ MVC 䆒䅵ᗱᛇϟⱘᶊᵘˈϟ䴶ㅔ㽕ҟ㒡 MVC 䆒䅵ᗱᛇⱘӬ࢓DŽ Ё㹿ߚ㾷៤໮ϾणৠᎹ԰ⱘ䚼ߚˈ䳔㢅᳈໮ᯊ䯈ᠡ㛑ⳳℷᥠᦵ݊䆒䅵੠ᅲ⦄䖛⿟DŽ г๲ࡴњᑨ⫼ᓔথⱘ໡ᴖ⿟ᑺDŽॳᴀ䳔㽕ϔϾㅔऩⱘ JSP 义䴶ህ㛑ᅲ⦄ⱘᑨ⫼ˈ೼ Model 2 ⬅Ѣᓩܹњ MVC ῵ᓣˈՓ Model 2 ݋᳝㒘ӊ࣪ⱘ⡍⚍ˈ᳈䗖⫼Ѣ໻㾘῵ᑨ⫼ⱘᓔথˈԚ ᔧDŽܙ ᔧˈ㗠᥻ࠊ఼˄Controller˅߭⬅ Servletܙ⬅ JSP 义䴶 ᔧˈ㾚೒˄View˅ܙ ৢৄⱘ JavaBean 䗮ֵDŽ೼ Model 2 ῵ᓣϟˈ῵ൟ˄Model˅⬅ JavaBean ҙҙ⫼Ѣᇚ㒧ᵰਜ⦄㒭⫼᠋ˈJSP 义䴶ⱘ䇋∖Ϣ Servlet˄᥻ࠊ఼˅ѸѦˈ㗠 Servlet 䋳䋷Ϣ ℷབ೒ 1.5 ЁⳟࠄⱘˈModel 2 ϟ JSP ϡݡᡓᢙ᥻ࠊ఼ⱘ䋷ӏˈᅗҙҙᰃ㸼⦄ሖ㾦㡆ˈ ೒ 1.5 Model 2 ⱘ⿟ᑣ⌕⿟ ⌣㾜 ఼ 4 ˄ ˅ Servlet ˄᥻ࠊ఼˅ JSP ˄㾚೒˅ ೒ 1.5 ᰒ⼎њ Model 2 ⱘ⿟ᑣ⌕⿟DŽ ೒ 1.4 Model 1 ⱘ⿟ᑣ⌕⿟ ⌣㾜 ఼ ㄀ 1 ゴ Struts 2 ὖ䗄 6 ᵰ⫼᠋ϡᇍᑨ⫼থߎ䇋∖ˈ㾚೒᮴⊩Џࡼ᳈ᮄ㞾ᏅDŽ ॳ಴ᰃ಴Ў Web ᑨ⫼ᰃϔ⾡䇋∖/ડᑨ῵ᓣϟᑨ⫼ˈᇍѢ䇋∖/ડᑨᑨ⫼ˈབ ⱘ MVC ᗱᛇϢ Web ᑨ⫼ⱘ MVC ᗱᛇгᄬ೼ϔᅮⱘᏂ߿ˈᓩ䍋Ꮒ߿ⱘЏ㽕 㰩ぐ MVC ᗱᛇϢ㾖ᆳ㗙῵ᓣ᳝ϔᅮⱘⳌԐП໘ˈԚᑊϡᅠܼⳌৠDŽ㒣݌ া᳝ᔧ⫼᠋䇋∖⌣㾜䆹义䴶ᯊˈ᥻ࠊ఼ᠡ䋳䋷䇗⫼῵ൟ᭄᥂ᴹ᳈ᮄ JSP 义䴶DŽ ব࣪ᯊˈ῵ൟ᮴⊩Џࡼথ䗕⍜ᙃ㒭 JSP 义䴶˄಴Ў Web ᑨ⫼䛑ᰃ෎Ѣ䇋∖/ડᑨ῵ᓣⱘ˅ˈ 䴶থ⫳ᬍব˗㗠ᇍѢ Web ᑨ⫼㗠㿔ˈेՓᇚ໮Ͼ JSP 义䴶⊼ݠ㒭ϔϾ῵ൟˈᔧ῵ൟথ⫳ Ͼᑨ⫼⿟ᑣ㗠㿔ˈ៥Ӏৃҹᇚ㾚೒⊼ݠ㒭῵ൟˈᔧ῵ൟ᭄᥂থ⫳ᬍবᯊˈेᯊ䗮ⶹ㾚೒义 ⳌᇍѢᮽᳳⱘ MVC ᗱᛇˈWeb ῵ᓣϟⱘ MVC ᗱᛇ߭জᄬ೼ϔѯব࣪ˈ಴ЎᇍѢϔ ⱘ⡍ᕕˈ᳝߽Ѣ䗮䖛Ꮉ⿟࣪੠Ꮉ݋࣪ѻ⫳ㅵ⧚⿟ᑣҷⷕDŽ  MVC ᳈ヺড়䕃ӊᎹ⿟࣪ㅵ⧚ⱘ㊒⼲DŽϡৠⱘሖ৘ৌ݊㘠ˈ↣ϔሖⱘ㒘ӊ݋᳝Ⳍৠ ⱘ䇋∖DŽ಴ℸˈ᥻ࠊሖৃҹ䇈ᰃࣙ৿њ⫼᠋䇋∖ᴗ䰤ⱘὖᗉDŽ  ᥻ࠊሖⱘὖᗉгᕜ᳝ᬜˈ⬅Ѣᅗᡞϡৠⱘ῵ൟ੠ϡৠⱘ㾚೒㒘ড়೼ϔ䍋ˈᅠ៤ϡৠ  ᑨ⫼㹿ߚ䱨Ўϝሖˈ䰡Ԣњ৘ሖП䯈ⱘ㗺ড়ˈᦤկњᑨ⫼ⱘৃᠽሩᗻDŽ JSP 义䴶ǃVelocity ῵ᵓ៪㗙Ⳉ᥹ѻ⫳ Excel ᭛ḷㄝDŽ  ῵ൟ䖨ಲⱘ᭄᥂Ϣᰒ⼎䘏䕥ߚ⾏DŽ῵ൟ᭄᥂ৃҹᑨ⫼ӏԩⱘᰒ⼎ᡔᴃˈ՟བˈՓ⫼ ᇥҷⷕⱘ໡ࠊঞҷⷕⱘ㓈ᡸ䞣ˈϔᮺ῵ൟথ⫳ᬍবˈгᯧѢ㓈ᡸDŽ ޣ ໮Ͼ㾚೒ৃҹᇍᑨϔϾ῵ൟDŽᣝ MVC 䆒䅵῵ᓣˈϔϾ῵ൟᇍᑨ໮Ͼ㾚೒ˈৃҹ ὖᣀ䍋ᴹˈMVC ᳝བϟ⡍⚍DŽ ᗱᛇ㗠㿔ˈ㹿㾖ᆳ㗙ᕔᕔাᰃऩ㒃ⱘ᭄᥂ԧˈ㗠㾖ᆳ㗙߭ᰃऩ㒃ⱘ㾚೒义䴶DŽ ೼ᇥ䆌Ꮒ߿˖㾖ᆳ㗙῵ᓣϟ㾖ᆳ㗙੠㹿㾖ᆳ㗙ৃҹᰃϸϾѦⳌᇍㄝⱘᇍ䈵ˈԚᇍѢ MVC Ң䆒䅵῵ᓣⱘ㾦ᑺᴹⳟˈMVC ᗱᛇ䴲ᐌ㉏ԐѢϔϾ㾖ᆳ㗙῵ᓣˈԚϢ㾖ᆳ㗙῵ᓣᄬ 䗕䗮ⶹˈ᥹ϟᴹˈ㾚೒Ңᇍᑨⱘ῵ൟЁ㦋ᕫֵᙃˈ✊ৢᅠ៤㾚೒ᰒ⼎ⱘ᳈ᮄDŽ ៤ˈे˖ᡞ໮Ͼ㾚೒⊼ݠࠄৠϔϾ῵ൟˈᔧ῵ൟথ⫳ᬍবᯊˈ῵ൟ৥᠔᳝⊼ݠ䖛ⱘ㾚೒থ ডПѺ✊DŽ݋ԧഄ䇈ˈ↣Ͼ῵ൟᇍᑨϔ㋏߫ⱘ㾚೒߫㸼ˈ䖭⾡ᇍᑨ݇㋏䗮ᐌ䞛⫼⊼ݠᴹᅠ ೼㒣݌ⱘ MVC ῵ᓣЁˈџӊ⬅᥻ࠊ఼໘⧚ˈ᥻ࠊ఼ḍ᥂џӊⱘ㉏ൟᬍব῵ൟ៪㾚೒ˈ ೒ 1.6 MVC 㒧ᵘ ೒ 1.6 ᠔⼎DŽ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 7 Ԛᰃˈ೼ᅲ䰙ᓔথ䖛⿟Ёˈ䞛⫼ Tapestry г䴶Јⴔϔѯ䯂乬ᖙ乏㗗㰥˖ ᳈ࡴӬ䲙ⱘᶊᵘˈ᳈དⱘᓔথᬜ⥛DŽ 䆌ˈՓ⫼ Tapestry Ӯᏺ㒭ᭈϾᑨ⫼ܕ಴ℸˈヨ㗙ϔⳈᇍ Tapestry ᚙ᳝⣀䩳˖བᵰᡔᴃ 䙷⾡㓪䆥ৢⱘᦤ⼎DŽ Tapestry ᰃ㄀ϔ䗝ᢽDŽ㊒⹂ഄ䫭䇃᡹ਞˈৃҹᇚ䫭䇃ᅮԡࠄ⑤⿟ᑣЁⱘ㸠ˈপҷњ JSP Ё JSP ᓔথ㗙ᰃⳳℷ䴶৥ᇍ䈵ˈ㗠ϡᰃ URL 㾷ᵤDŽᇍѢᇍ义䴶㽕∖♉⌏ᑺⳌᔧ催ⱘ㋏㒳ˈ Tapestry ݋᳝ᕜ催ⱘҷⷕ໡⫼ᗻˈ೼ Tapestry Ёˈӏԩᇍ䈵䛑ৃⳟ԰ৃ໡⫼ⱘ㒘ӊDŽ ৡ⿄੠ path ৡ⿄ড়ѠЎϔDŽ 䕀Ⳃᷛ㽕Мᰃ㒘ӊˈ㽕Мᰃ义䴶ˈ≵᳝໮ԭⱘ path ὖᗉDŽ㒘ӊৡˈгህᰃᇍ䈵ৡ⿄ˈ㒘ӊ ⱘ䯂乬DŽTapsetry ᰃᅠܼ㒘ӊ࣪ⱘḚᶊDŽTapestr া᳝㒘ӊ៪义䴶ϸϾὖᗉˈ಴ℸˈ䫒᥹䏇 њᷛㅒᑧ੠㒘ӊ㒧ড়ܡTapestry Փ⫼㒘ӊᑧ᳓ҷњᷛㅒᑧˈ≵᳝ᷛㅒᑧὖᗉˈҢ㗠䙓 ⱘ῵ᵓᡔᴃेৃˈTapestry ᅲ⦄њ㾚೒䘏䕥੠Ϯࡵ䘏䕥ⱘᕏᑩߚ⾏DŽ 䗮䖛Փ⫼ Tapestryˈᓔথ㗙ᅠܼϡ䳔㽕Փ⫼ JSP ᡔᴃˈ⫼᠋া䳔㽕Փ⫼ Tapestry ᦤկ Servlet/JSP API ߚ⾏ˈᰃϔ⾡䴲ᐌӬ⾔ⱘ䆒䅵DŽ ࣙ৿њࠡッⱘ MVC Ḛᶊˈ䖬ࣙ৿њϔ⾡㾚೒ሖⱘ῵ᵓᡔᴃˈՓ⫼ Tapestry ᅠܼৃҹϢ MVC Ḛᶊ੠῵ᵓᡔᴃⱘ㒧ড়ˈᅗϡҙ ڣTapestry ᑊϡᰃϔ⾡ऩ㒃ⱘ MVC Ḛᶊˈᅗ᳈ 2ˊTapestry  JSF ⱘ៤❳ᑺ䖬᳝ᕙ䖯ϔℹᦤ催DŽ  JSF ᑊϡᰃϔϾᅠܼ㒘ӊ࣪ⱘḚᶊˈᅗձ✊ᰃ෎Ѣ JSP/Servlet ᶊᵘⱘDŽ  ԰Ўᮄ݈ⱘ MVC Ḛᶊˈ⫼᠋Ⳍᇍ䕗ᇥˈⳌ݇䌘⑤гϡᰃ䴲ᐌЄᆠDŽ JSP/Servlet ᶊᵘˈ಴㗠ᄺд᳆㒓ⳌᇍㅔऩDŽ೼ᅲ䰙Փ⫼䖛⿟ЁˈJSF гӮᄬ೼ϔѯϡ䎇˖ JSF ⱘџӊḚᶊৃҹ㒚࣪ࠄ㸼ऩЁ↣Ͼᄫ↉DŽJSF ձ✊ᰃ෎Ѣ JSP/Servlet ⱘˈҡ✊ᰃ ϔϾᇍ䈵Ёˈᦤկ᳈໮♉⌏ⱘ䗝ᢽDŽ Bean ࣙ৿Ϯࡵ䘏䕥ˈѠ㗙᮴⊩㵡ড়೼ϔ䍋DŽ೼ JSF Ёˈ᮶ৃҹᇚѠ㗙ߚᓔˈгৃҹড়ᑊ೼ 䆌⫳៤⣀ゟⱘ᥻ࠊ఼ᇍ䈵DŽ೼ Struts 1 ЁˈForm Bean ࣙ৿᭄᥂ˈActionܕᔧ✊ˈJSF г 䖛῵ൟᇍ䈵ᅲ⦄DŽ ᮴䳔೼㸼ऩ੠῵ൟᇍ䈵П䯈ᅲ⦄໮ԭⱘ᥻ࠊ఼ሖDŽJSF Ё≵᳝᥻ࠊ఼ᇍ䈵ˈ᥻ࠊ఼㸠Ў䗮 JSF ⱘ㸠Ўᮍ⊩೼ POJO Ёᅲ⦄ˈJSF ⱘ Managed Bean ᮴䳔㒻ᡓӏԩ⡍߿ⱘ㉏DŽ಴ℸˈ ᣛ Sun ⱘখ㗗ᅲ⦄DŽⳂࠡˈJSF ᰃ԰Ў JEE 5.0 ⱘϔϾ㒘៤䚼ߚˈϢ JEE 5.0 ϔ䍋থᏗDŽ 䗝ᢽˈࣙ৿ Sun ⱘখ㗗ᅲ⦄੠ Apache ⱘ MyFacesDŽ໻䚼ߚⱘᯊ׭ˈ៥Ӏ᠔䇈ⱘ JSF 䛑ᰃ 㗠ϡᰃϔϾѻકDŽⳂࠡˈJSF Ꮖ㒣᳝ϸϾᅲ⦄ѻકৃկˈޚ⹂ഄ䇈ˈJSF ᰃϔϾᷛޚ 1ˊJSF Ⳃࠡᐌ⫼ⱘ MVC Ḛᶊ䖬᳝བϟϔѯDŽ 䕙ࡽ㉏ᑧˈᐂࡽᑨ⫼ⱘᓔথDŽ 䖭ѯḚᶊ䛑ᦤկњ䕗དⱘሖ⃵ߚ䱨㛑࡯DŽ೼ᅲ⦄㡃དⱘ MVC ߚ䱨ⱘ෎⸔Ϟˈ䖬ᦤկϔѯ Ⳃࠡᐌ⫼ⱘ MVC Ḛᶊˈ䰸њ Struts 2 ⱘϸϾࠡ䑿໪ˈ䖬᳝ϔѯ䴲ᐌ⌕㸠ⱘ MVC Ḛᶊˈ 1.1.4 ᐌ⫼ⱘ MVC Ḛᶊ ㄀ 1 ゴ Struts 2 ὖ䗄 8 Ḛᶊ˖Struts 1.0 ೼ 2001 ᑈ 6 ᳜থᏗˈ䖭ϔ⚍ৃ㛑ᰃՓᅗᕫࠄབℸᑓ⊯ᢹ᠈ⱘЏ㽕ॳ಴DŽ ៤ࡳᕫⲞѢᅗЄᆠⱘ᭛ḷǃ⌏䎗ⱘᓔথ㕸ԧDŽᔧ✊ˈStruts 1 ᰃϪ⬠Ϟ㄀ϔϾথᏗⱘ MVC ⥛ˈ䖬ᰃ᠔ᢹ᳝ⱘᓔথҎ㕸ˈStruts 1 䛑ᢹ᳝݊Ҫ MVC Ḛᶊϡৃ↨ᢳⱘӬ࢓DŽStruts 1 ⱘ Ң䖛এⱘቕ᳜ᴹⳟˈStruts 1 ᰃ᠔᳝ MVC ḚᶊЁϡᆍ䕽假ⱘ㚰߽㗙ˈϡㅵᰃᏖഎऴ᳝ 1.2.1 Struts 1 ㅔҟঞᄬ೼ⱘ䯂乬 WebWork ⱘᓔথҎਬˈ䛑ৃҹᑇ〇䖛⏵ࠄՓ⫼ Struts 2 ḚᶊDŽ Ͼݐᆍ WebWork ੠ Struts 1 ⱘ MVC ḚᶊˈStruts 2 ⱘⳂᷛᰃᏠᳯৃҹ䅽ॳᴹՓ⫼ Struts 1ǃ Struts 2 ҹ WebWork Ӭ⾔ⱘ䆒䅵ᗱᛇЎḌᖗˈ਌ᬊњ Struts 1 ⱘ䚼ߚӬ⚍ˈᓎゟњϔ 1.2 Struts 2 ⱘ䍋⑤੠㚠᱃  䖛ߚ䗑∖ᶊᵘⱘᅠ㕢ˈ᳝䖛ᑺ䆒䅵ⱘॅ䰽DŽ  ໾䖛㒚࣪ⱘ㾦㡆ߦߚˈ໾䖛⚺⧤ˈ䰡Ԣњᑨ⫼ⱘᓔথᬜ⥛DŽ MVC ḚᶊⱘৃᠽሩᗻDŽ  Spring ⱘ MVC Ϣ Servlet API 㗺ড়ˈ䲒ҹ㜅⾏ Servlet ᆍ఼⣀ゟ䖤㸠ˈ䰡Ԣњ Spring ೼ϔ䍋DŽ䖭䛑ᇐ㟈њ Spring MVC Ḛᶊⱘϔѯ㔎⚍˖ ᮍḜˈᑊϢ Web ᑨ⫼㋻㋻㗺ড়އᘏԧϞᴹⳟˈSpring MVC Ḛᶊ㟈࡯Ѣϔ⾡ᅠ㕢ⱘ㾷 ԚⳌᇍѢ Tapestry Ḛᶊ㗠㿔ˈSpring MVC ձ✊ᰃ෎Ѣ JSP/Servlet API ⱘDŽ ӏԩ㸼⦄ሖ㗺ড়˖JSPǃVelocity ៪㗙݊Ҫⱘᡔᴃüü䛑ৃҹ੠ Spring ᭈড়DŽ ⱘᇍᑨ݇㋏DŽ㾚೒ৡ㾷ᵤⱘ䜡㕂䴲ᐌ♉⌏ˈᢑ䈵ⱘ Model ᅠܼ⣀ゟѢ㸼⦄ሖᡔᴃˈϡӮϢ ModelAndView ᇍ䈵ˈ䆹ᇍ䈵ࣙ৿㾚೒ৡᄫ੠ ModelˈModel ᦤկњ Bean ⱘৡᄫঞ݊ᇍ䈵 Spring MVC Ḛᶊ᳝ϔϾᵕདⱘӬ࢓ˈህᰃᅗⱘ㾚೒㾷ᵤㄪ⬹˖ᅗⱘ᥻ࠊ఼䖨ಲϔϾ ˄requestˈresponse˅ᮍ⊩DŽ䗮䖛ᅲ⦄䆹᥹ষᴹᅲ⦄⫼᠋ⱘϮࡵ䘏䕥᥻ࠊ఼DŽ ᠔᳝᥻ࠊ఼䛑ᖙ乏ᅲ⦄ Controller ᥹ষˈ䆹᥹ষҙᅮН ModelAndView handleRequest Spring MVC 䖬ࣙᣀ໘⧚఼᯴ᇘǃ㾚೒㾷ᵤǃֵᙃ೑䰙࣪ǃЏ乬㾷ᵤǃ᭛ӊϞӴㄝDŽ ∖ˈᇚ䇋∖ߚথࠄᇍᑨⱘϮࡵ᥻ࠊ఼DŽ Spring MVC Ḛᶊҹ DispatcherServlet ЎḌᖗ᥻ࠊ఼ˈ䆹᥻ࠊ఼䋳䋷ᢺ៾⫼᠋ⱘ᠔᳝䇋 䆩᳈ࡴᮍ֓DŽ⌟ܗᇍᑨ㾚೒ᡔᴃ䲚៤䖯ᴹDŽSpring ⱘ Controllers ⬅ IoC ᆍ఼ㅵ⧚DŽ಴ℸˈऩ Ҫ㾚೒ᡔᴃDŽ⫮㟇ৃҹՓ⫼㞾ᅮНⱘ㾚೒ᴎࠊüüা䳔㽕ㅔऩഄᅲ⦄ View ᥹ষˈᑊϨᡞ 㓪⿟ˈⳳℷᅲ⦄њ㾚೒᮴݇DŽ㾚೒ϡݡᔎࠊ㽕∖Փ⫼ JSPˈৃҹՓ⫼ VelocityǃXSLT ៪݊ ϔϾ䴲ᐌ⏙᱄ⱘߦߚˈ৘䚼ߚ㗺ড়ᵕԢDŽSpring ⱘ MVC ᰃ䴲ᐌ♉⌏ⱘˈᅗᅠܼ෎Ѣ᥹ষ Spring ᦤկњϔϾ㒚㟈ᅠᭈⱘ MVC ḚᶊDŽ䆹ḚᶊЎ῵ൟǃ㾚೒ǃ᥻ࠊ఼П䯈ᦤկњ 3ˊSpring MVC ࡴ⊼䞞DŽ  Tapestry ⱘ㒘ӊ䘏䕥↨䕗໡ᴖˈݡࡴϞ OGNL 㸼䖒ᓣ੠ሲᗻᣛᅮᴎࠊˈ಴㗠䲒ҹ⏏ ᅬᮍⱘ᭛ḷ໾䖛ᄺ䰶⌒ˈ㔎Уᅲ䰙ⱘ⼎՟⿟ᑣDŽ  Tapestry ⱘᄺд᳆㒓Ⳍᇍ䰵዁ˈ೑ݙᓔথ㕸ԧϡᰃ䴲ᐌ⌏䎗ˈ᭛ḷϡᰃकߚЄᆠDŽ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ ㄀ 1 ゴ Struts 2 ὖ䗄 ЎњՓ䇏㗙ৃҹᯢⱑ Struts 1 ⱘ䖤㸠ᴎࠊˈϟ䴶ᇚㅔ㽕ҟ㒡 Struts 1 ⱘ෎ᴀḚᶊDŽ Struts 1 Ḛᶊҹ ActionServlet ԰ЎḌᖗ᥻ࠊ఼ˈᭈϾᑨ⫼⬅ᅶ᠋ッ䇋∖偅ࡼDŽᔧᅶ᠋ッ ৥ Web ᑨ⫼থ䗕䇋∖ᯊˈ䇋∖ᇚ㹿 Struts 1 ⱘḌᖗ᥻ࠊ఼ ActionServlet ᢺ៾ˈActionServlet ḍ᥂䇋∖އᅮᰃ৺䳔㽕䇗⫼Ϯࡵ䘏䕥᥻ࠊ఼໘⧚⫼᠋䇋∖˄ᅲ䰙ϞˈϮࡵ䘏䕥᥻ࠊ఼䖬ᰃ ᥻ࠊ఼ˈᅗাᰃ䋳䋷䇗⫼῵ൟᴹ໘⧚⫼᠋䇋∖˅ˈᔧ⫼᠋䇋∖໘⧚ᅠ៤ৢˈ݊໘⧚㒧ᵰ䗮 䖛 JSP ਜ⦄㒭⫼᠋DŽ ᇍѢᭈϾ Struts 1 Ḛᶊ㗠㿔ˈ᥻ࠊ఼ህᰃᅗⱘḌᖗˈStruts 1 ⱘ᥻ࠊ఼⬅ϸϾ䚼ߚ㒘៤˖ Ḍᖗ᥻ࠊ఼੠Ϯࡵ䘏䕥᥻ࠊ఼DŽ݊ЁḌᖗ᥻ࠊ఼ህᰃ ActionServletˈ⬅ Struts 1 Ḛᶊᦤկ˗ Ϯࡵ䘏䕥᥻ࠊህᰃ⫼᠋㞾ᅮНⱘ Actionˈ⬅ᑨ⫼ᓔথ㗙ᦤկDŽ ᇍѢ໻䚼ߚ⫼᠋䇋∖㗠㿔ˈ䛑䳔㽕ᕫࠄ᳡ࡵ఼ⱘ໘⧚DŽᔧ⫼᠋থ䗕ϔϾ䳔㽕ᕫࠄ᳡ࡵ ఼໘⧚ⱘ䇋∖ᯊˈ䆹䇋∖㹿 ActionServlet ᢺ៾ࠄˈActionServlet ᇚ䆹䇋∖䕀থ㒭ᇍᑨⱘϮ ࡵ䘏䕥᥻ࠊ఼ˈϮࡵ䘏䕥᥻ࠊ఼䇗⫼῵ൟᴹ໘⧚⫼᠋䇋∖˗བᵰ⫼᠋䇋∖াᰃᏠᳯᕫࠄᶤ Ͼ URL 䌘⑤ˈ߭⬅ ActionServlet ᇚ㹿䇋∖ⱘ䌘⑤䕀থ㒭⫼᠋DŽ Struts 1 ⱘ⿟ᑣ䖤㸠⌕⿟བ೒ 1.7 ᠔⼎DŽ ೒ 1.7 Struts 1 ⱘ⿟ᑣ䖤㸠⌕⿟ ϟ䴶ህ Struts 1 ⿟ᑣ⌕⿟݋ԧߚᵤ MVC ЁⱘϝϾ㾦㡆DŽ ˄1˅Model 䚼ߚ Struts 1 ⱘ Model 䚼ߚЏ㽕⬅ᑩሖⱘϮࡵ䘏䕥㒘ӊܙᔧˈ䖭ѯϮࡵ䘏䕥㒘ӊᇕ㺙њᑩ ሖ᭄᥂ᑧ䆓䯂ǃϮࡵ䘏䕥ᮍ⊩ᅲ⦄DŽᅲ䰙ϞˈᇍѢϔϾ៤❳ⱘӕϮᑨ⫼㗠㿔ˈModel 䚼ߚ гϡᰃϔϾㅔऩⱘ JavaBean ᠔㛑ᅠ៤ⱘˈᅗৃ㛑ᰃϔϾ៪໮Ͼ EJB 㒘ӊˈৃ㛑ᰃϔϾ WebService ᳡ࡵDŽᘏПˈModel 䚼ߚᇕ㺙њᭈϾᑨ⫼ⱘ᠔᳝Ϯࡵ䘏䕥ˈԚᭈϾ䚼ߚᑊϡᰃ ⬅ Struts 1 ᦤկⱘˈStruts 1 г≵᳝Ўᅲ⦄ Model 㒘ӊᦤկӏԩᬃᣕDŽ ˄2˅View 䚼ߚ Struts 1 ⱘ View 䚼ߚ䞛⫼ JSP ᅲ⦄DŽStruts 1 ᦤկњЄᆠⱘᷛㅒᑧˈ䗮䖛䖭ѯᷛㅒᑧৃ ҹ᳔໻䰤ᑺഄޣᇥ㛮ᴀⱘՓ⫼DŽ䖭ѯ㞾ᅮНⱘᷛㅒᑧৃҹ䕧ߎ᥻ࠊ఼ⱘ໘⧚㒧ᵰDŽ 㱑✊ Struts 1 ᦤկњϢ Ties Ḛᶊⱘᭈড়ˈԚ Struts 1 ᠔ᬃᣕⱘ㸼⦄ሖᡔᴃ䴲ᐌऩϔ˖ ᮶ϡᬃᣕ FreeMarkerǃVelocity ㄝ῵ᵓᡔᴃˈгϡᬃᣕ JasperReports ㄝ᡹㸼ᡔᴃDŽ ˄3˅Controller 䚼ߚ Struts 1 ⱘ Controller ⬅ϸϾ䚼ߚ㒘៤DŽ  ㋏㒳Ḍᖗ᥻ࠊ఼˖⬅ Struts 1 Ḛᶊᦤկˈህᰃ㋏㒳Ёⱘ ActionServletDŽ  Ϯࡵ䘏䕥᥻ࠊ఼˖⬅ Struts 1 Ḛᶊᦤկˈህᰃ⫼᠋㞾Ꮕᅲ⦄ⱘ Action ᅲ՟DŽ 9 10 ⳟϟ䴶ⱘ Action ҷⷕ⠛↉˖ ⒵њ໻䞣ⱘ Servlet APIDŽܙˈⱘˈ᠔ҹ೼ Struts 1 ⱘϮࡵ䘏䕥᥻ࠊ఼ݙ ಴Ў Struts 1 Ḛᶊᰃ೼ Model 2 ⱘ෎⸔Ϟথሩ䍋ᴹⱘˈ಴ℸᅗᅠܼᰃ෎Ѣ Servlet API ˄2˅Ϣ Servlet API Ϲ䞡㗺ড়ˈ䲒Ѣ⌟䆩 ড়ˈ䖭Ϲ䞡䰤ࠊњ Struts 1 ⱘՓ⫼DŽ 㱑✊ Struts 1 ᳝䴲ᐌӬ⾔ⱘ䆒䅵ˈԚ⬅Ѣग़৆ॳ಴ˈᅗ≵᳝ᦤկϢ᳈໮㾚೒ᡔᴃⱘᭈ ࠄ䅽䘏䕥㾚೒ৡৃҹᬃᣕ᳈໮ⱘ㾚೒ᡔᴃDŽخ㋏ˈাᰃ≵᳝ ㌑䡻 Struts 1 Ꮖ㒣䗮䖛䜡㕂᭛ӊㅵ⧚䘏䕥㾚೒ৡ੠ᅲ䰙㾚೒П䯈ⱘᇍᑨ݇ ≵᳝㗗㰥Ϣ䖭ѯ FreeMarkerǃVelocity ㄝ㾚೒ᡔᴃⱘᭈড়DŽ Ԛ಴Ў Struts 1 Ḛᶊߎ⦄ⱘᑈҷ໾ᮽњˈ䙷ᯊ׭䖬≵᳝ FreeMarkerǃVelocity ㄝᡔᴃˈ಴㗠 ᥹ᠻ㸠䕀থ䇋∖ˈ㗠ҙҙ䖨ಲϔϾ䘏䕥㾚೒ৡüüᅲ䰙ⱘ䕀থᬒ೼䜡㕂᭛ӊЁ䖯㸠ㅵ⧚DŽ ҢϞ䴶ⱘ䆒䅵ᴹⳟˈϡᕫϡԽ᳡ Struts 1 ⱘ䆒䅵㗙催ᑺ㾷㗺ⱘ䆒䅵˖᥻ࠊ఼ᑊ≵᳝Ⳉ ⦄㒭⫼᠋DŽ ActionForword ᇍ䈵ৢˈৃҹḍ᥂䘏䕥㾚೒ৡ੠㾚೒䌘⑤П䯈ⱘᇍᑨ݇㋏ˈᇚ㾚೒䌘⑤ਜ ӊЁᅮНњ䘏䕥㾚೒ৡ੠㾚೒䌘⑤П䯈ⱘᇍᑨ݇㋏ˈᔧ ActionServlet ᕫࠄ໘⧚఼䖨ಲⱘ ActionForward ᇍ䈵˄ৃҹ⧚㾷 ActionForward ᰃϔϾ䘏䕥㾚೒ৡ˅ˈ೼ struts-config.xml ᭛ 㱑✊ Struts 1 ໘⧚ᅠ⫼᠋䇋∖ৢˈᑊ≵᳝Ⳉ᥹䕀ࠄ⡍ᅮⱘ㾚೒䌘⑤ˈ㗠ᰃ䖨ಲϔϾ ᑨ⫼㗠㿔ˈᑊϡϔᅮՓ⫼ JSP ԰Ў㸼⦄ሖᡔᴃDŽ FreeMarker ㄝᡔᴃⱘᭈড়DŽ䖭ϔ⚍Ϲ䞡ࠊ㑺њ Struts 1 ḚᶊⱘՓ⫼ˈᇍѢⳂࠡⱘᕜ໮ Java EE Struts 1 াᬃᣕ JSP ԰Ў㸼⦄ሖᡔᴃˈϡᦤկϢ݊Ҫ㸼⦄ሖᡔᴃˈ՟བ Velocityǃ ˄1˅ᬃᣕⱘ㸼⦄ሖᡔᴃऩϔ üü䖭гᰃ Struts 2 ߎ⦄ⱘॳ಴DŽϟ䴶݋ԧߚᵤ Struts 1 Ёᄬ೼ⱘ⾡⾡㔎䱋DŽ ⱘ㔎䱋ˈ䱣ⴔ Web ᑨ⫼ⱘ䗤⏤ᠽ໻ˈ䖭ѯ㔎䱋䗤⏤ব៤ࠊ㑺 Struts 1 থሩⱘ䞡㽕಴㋴ܡ ᇍѢ Struts 1 Ḛᶊ㗠㿔ˈ಴ЎᅗϢ JSP/Servlet 㗺ড়䴲ᐌ㋻ᆚˈ಴㗠ᇐ㟈њ䆌໮ϡৃ䙓 FreeMarker ੠ Velocity ㄝ˅ⱘᭈড়DŽ 䋳䋷⫼᥻ࠊ఼䇗⫼Ϯࡵ䘏䕥㒘ӊˈᑊ䋳䋷᥻ࠊ఼Ϣ㾚೒ᡔᴃ˄JSPǃ ㌑䡻 ᇍѢӏԩⱘ MVC Ḛᶊ㗠㿔ˈ݊ᅲাᅲ⦄њ C˄᥻ࠊ఼˅䚼ߚˈԚᅗ ࠊ఼ሖDŽ ᣕDŽ಴ℸˈ᥻ࠊ఼䚼ߚህᰃ Struts 1 ḚᶊⱘḌᖗDŽ᳝ᯊ׭ˈ៥ӀⳈ᥹ᇚ MVC ሖ⿄Ў᥻ Struts 1 ᦤկњ㋏㒳᠔䳔㽕ⱘḌᖗ᥻ࠊ఼ˈгЎᅲ⦄Ϯࡵ䘏䕥᥻ࠊ఼ᦤկњ䆌໮ᬃ 䇗⫼ Model ᴹᅠ៤໘⧚DŽ Ϯࡵ䘏䕥᥻ࠊ఼䋳䋷໘⧚⫼᠋䇋∖ˈԚϮࡵ䘏䕥᥻ࠊ఼ᴀ䑿ᑊϡ݋᳝໘⧚㛑࡯ˈ㗠ᰃ ࡵ䘏䕥᥻ࠊ఼ˈ߭ᇚ䇋∖䕀থ㒭 Action ໘⧚ˈ৺߭Ⳉ᥹䕀৥䇋∖ⱘ JSP 义䴶DŽ ᅮᰃ৺䳔㽕䇗⫼Ϯࡵ䘏䕥᥻ࠊ఼ˈབᵰ䳔㽕䇗⫼Ϯއ∖᠔᳝ HTTP 䇋∖ˈ✊ৢḍ᥂⫼᠋䇋 ⱘ Servletˈ䆹᥻ࠊ఼䋳䋷ᢺ៾ޚ1 Ḛᶊᦤկˈ㒻ᡓ HttpServlet ㉏ˈ಴ℸৃҹ䜡㕂៤ϔϾᷛ Struts 1 ⱘḌᖗ᥻ࠊ఼ᇍᑨ೒ 1.7 ЁⱘḌᖗ᥻ࠊ఼˄ActionServlet˅DŽ䆹᥻ࠊ఼⬅ Struts Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 11 䆌ܕˈ䆩᳈ࡴᮍ֓⌟ܗࡴᵒ㗺ড়ⱘ䆒䅵ˈ䅽㋏㒳ⱘ Action ϡݡϢ Servlet API 㗺ড়DŽՓऩ ໽ᗻϡ䎇㗠㿔ˈWebWork ߭᳈ࡴӬ⾔ˈᅗ䞛⫼њϔ⾡᳈ܜⳌᇍѢ Struts 1 ᄬ೼ⱘ䙷ѯ Rickard Oberg˄Ҫህᰃ JBoss ੠ XDoclet ⱘ԰㗙˅DŽ བ QutarzǃOSWorkFlow ㄝDŽᅲ䰙ϞˈWebWork ⱘ߯ྟҎ߭ᰃ঺ϔϾ Java 乚ඳⱘৡҎ˖ ϾӬ⾔ⱘᓔ⑤㒘㒛˖opensymphonyˈ䖭ϾӬ⾔ⱘᓔ⑤㒘㒛ৠḋᓔথњ໻䞣Ӭ⾔ⱘᓔ⑤乍Ⳃˈ WebWork 㱑✊≵᳝ Struts 1 䙷ḋ䌿䌿᳝ৡˈԚгᰃߎ䑿ৡ䮼ˈWebWork ᴹ㞾঺໪ϔ 1.2.2 WebWork ㅔҟ ৃ㾕ˈStruts 1 ⱘ Action ㉏䖭⾡։ܹᓣ䆒䅵ᇐ㟈њ䕗Ԣⱘҷⷕ໡⫼DŽ ㉏ᇚᅠܼ≵᳝߽⫼Ӌؐˈ៤ЎϔේᑳકDŽ ActionForward ㉏DŽ䖭⾡։ܹᓣ䆒䅵ⱘ᳔໻ᔅ⚍೼Ѣˈϔᮺ㋏㒳䳔㽕䞡ᵘᯊˈ䖭ѯ Action ᅲ⦄໘⧚ᮍ⊩ᯊˈজࣙ৿њ໻䞣 Struts 1 API˖བ ActionMappingǃActionForm ੠ ℷབҢϞ䴶ҷⷕ⠛↉Ё᠔ⳟࠄⱘˈStruts 1 ⱘ Action ㉏ᖙ乏㒻ᡓ Struts 1 ⱘ Action ෎㉏ˈ ˄3˅ҷⷕϹ䞡ձ䌪Ѣ Struts 1 APIˈሲѢ։ܹᓣ䆒䅵 Web ᳡ࡵ఼DŽ಴ℸˈϔᮺ㜅⾏њ Web ᳡ࡵ఼ˈAction ⱘ⌟䆩䴲ᐌೄ䲒DŽ ಴Ў HttpServletRequest ੠ HttpServletResponse ϸϾখ᭄ᰃ Servlet APIˈϹ䞡ձ䌪Ѣ ݊ᰃ HttpServletRequest ੠ HttpServletResponse ϸϾখ᭄ˈ䗮ᐌ⬅ Web ᆍ఼䋳䋷ᅲ՟࣪DŽ ActionFormǃHttpServletRequest ੠ HttpServletResponseˈ߱ྟ࣪䖭 4 Ͼখ᭄↨䕗ೄ䲒ˈᇸ ᔧ៥Ӏ䳔㽕⌟䆩Ϟ䴶 Action ㉏ⱘ execute ᮍ⊩ᯊˈ䆹ᮍ⊩᳝ 4 Ͼখ᭄˖ActionMappingǃ } } } return mapping.findForward("success"); //໘⧚༅䋹ˈ䖨ಲϔϾ ActionForward ᇍ䈵 { else } return mapping.findForward("success"); //໘⧚៤ࡳˈ䖨ಲϔϾ ActionForward ᇍ䈵 { && "tiger".equals(loginForm.getPassword()) if ("scott".equals(loginForm.getUsername() //ᔧ⫼᠋ৡЎ scott,ᆚⷕЎ tiger ᯊ䖨ಲ៤ࡳ LoginForm loginForm = (LoginForm)form; //ᇚ݊ᔎࠊ㉏ൟ䕀ᤶЎⱏᔩ⫼ⱘ ActionForm //㦋পᇕ㺙⫼᠋䇋∖খ᭄ⱘ ActionForm ᇍ䈵 { AuctionException HttpServletRequest request, HttpServletResponse response)throws public ActionForward execute(ActionMapping mapping, ActionForm form, //໘⧚⫼᠋䇋∖ⱘ execute ᮍ⊩ { public class LoginAction extends Action //Ϯࡵ䘏䕥᥻ࠊ఼ᖙ乏㒻ᡓ Struts 1 ᦤկⱘ Action ㉏ ㄀ 1 ゴ Struts 2 ὖ䗄 12 ⱘ⾡⾡㔎⚍㗠㿔ˈWebWork ᄬ೼བϟӬ⚍˖ Ϣࠡ䴶ⱘ Struts 1 Ḛᶊᇍ↨ˈϡ䲒থ⦄ WebWork ೼ᕜ໮ഄᮍ⹂ᅲ᳈Ӭ⾔DŽⳌᇍ Struts 1 Ϟ䴶ᭈϾ䖛⿟ⱘ᭄᥂⌕೒བ೒ 1.8 ᠔⼎DŽ ᣛᅮⱘ䆩೒䌘⑤㒣䖛ᢺ៾఼䫒ⱘ໘⧚ৢˈ⫳៤ᇍᅶ᠋ッⱘડᑨ䕧ߎDŽ ᅠ HTTP 䇋∖ৢˈ䆹㒧ᵰাᰃϔϾ᱂䗮ᄫヺІˈ䆹ᄫヺІᇚᇍᑨࠄᣛᅮⱘ㾚೒䌘⑤DŽ Мᓔথ㗙㞾Ꮕᅲ⦄ⱘ Action ৃҹϢ Servlet API ߚ⾏ⱘॳ಴DŽᔧᓔথ㗙㞾Ꮕⱘ Action ໘⧚ ᓔথ㗙㞾Ꮕᅲ⦄ⱘϮࡵ䘏䕥᥻ࠊ఼াᰃ WebWork Ϯࡵ᥻ࠊ఼ⱘⳂᷛüü䖭ህᰃЎҔ ᠔㨫ⱘljSpring 2.0 ᅱ݌NJϔкⱘ㄀ 6 ゴDŽ ᵰ䇏㗙䳔㽕㦋প᳈໮݇Ѣ AOP 㓪⿟ⱘⶹ䆚ˈ䇋খ䯙 AOP Ⳍ݇䌘᭭ˈ៪ヨ㗙 㓪⿟ᮍᓣˈাᰃ䖭⾡ AOP Ёⱘᢺ៾఼⬅㋏㒳ᦤկˈ಴ℸ᮴䳔⫼᠋খϢDŽབ ㌑䡻 WebWork Ё߯ᓎ᥻ࠊ఼ҷ⧚ⱘᮍᓣˈህᰃϔ⾡ AOP˄䴶৥ߛ䴶㓪⿟˅ ⧚ҹ⫼᠋ᅲ⦄ⱘ᥻ࠊ఼԰ЎⳂᷛˈҹᢺ៾఼䫒Ёⱘᢺ៾఼԰Ў໘⧚˄Advice˅DŽ ߯ᓎ᥻ࠊ఼ҷ⧚ᯊˈWebWork 䳔㽕ᕫࠄᓔথ㗙ᅮНⱘ xwork.xml 䜡㕂᭛ӊˈ᥻ࠊ఼ҷ ⱘ᥻ࠊ఼ҷ⧚DŽ ؐᕫ⊼ᛣⱘᰃˈℸᯊⱘϮࡵ䘏䕥᥻ࠊ఼ᑊϡᰃᓔথ㗙ᅲ⦄ⱘ᥻ࠊ఼ˈ㗠ᰃ WebWork ߯ᓎ ໘⧚ˈ䆹᥻ࠊ఼߭䇗⫼ Action ᯴ᇘ఼ˈ䆹᯴ᇘ఼ᇚ⫼᠋䇋∖䕀থࠄᇍᑨⱘϮࡵ䘏䕥᥻ࠊ఼DŽ ఼䖛Ⓒˈ⬅ WebWork ⱘḌᖗ᥻ࠊ఼ᢺ៾ˈབᵰ⫼᠋䇋∖䳔㽕 WebWork ⱘϮࡵ䘏䕥᥻ࠊ఼ ᔧ⫼᠋৥ Web ᑨ⫼থ䗕䇋∖ᯊˈ䆹䇋∖㒣䖛 ActionContextCleanUpǃSiteMesh ㄝ䖛Ⓒ 䫒ˈ䋳䋷ᇚ⫼᠋䇋∖᭄᥂䕀থࠄ Actionˈᑊ䋳䋷ᇚ Action ⱘ໘⧚㒧ᵰ䕀ᤶ៤ᇍ⫼᠋ⱘડᑨDŽ WebWork ⱘ Action ৃҹϢ Servlet API ߚ⾏ˈᕫⲞѢᅗ♉Ꮋⱘ䆒䅵ˈᅗՓ⫼ϔϾᢺ៾఼ ܼϢ Servlet API ߚ⾏ˈ಴㗠䆹 Action ᳈ᆍᯧ⌟䆩DŽ Ⳍᇍ Struts 1 ⱘ Action Ϣ Servlet API ㋻㋻㗺ড়ⱘᔅ⚍ᴹ䇈ˈWebWork ⱘ Action ߭ᅠ  Ϯࡵ䘏䕥᥻ࠊ఼ Actionˈ䆹᥻ࠊ఼⬅⿟ᑣਬᦤկDŽ  Ḍᖗ᥻ࠊ఼ ServletDispatcherˈ䆹᥻ࠊ఼ḚᶊᦤկDŽ Ё᥻ࠊ఼䛑⬅ϸϾ䚼ߚ㒘៤˖ Ң໘⧚⌕⿟ϞᴹⳟˈWebWork Ϣ Struts 1 䴲ᐌ㉏ԐˈᅗӀⱘḌᖗ䛑⬅᥻ࠊ఼㒘៤ˈ݊ ⧚ HTTP ⱘડᑨ੠䇋∖DŽ WebWork ᓎゟ೼ XWork ПϞˈՓ⫼ ServletDispatcher ԰Ў䆹ḚᶊⱘḌᖗ᥻ࠊ఼ˈ໘ ᓩሲᗻⱘᬃᣕ䴲ᐌᔎ໻DŽ WebWork 2 Փ⫼ OGNL 䖭Ͼᔎ໻ⱘ㸼䖒ᓣ䇁㿔ˈৃҹ䆓䯂ؐᷜDŽOGNL ᇍ䲚ড়੠㋶ ៤⌟䆩ˈ㗠ϡ䳔㽕䆒㕂᳡ࡵ⊼ݠDŽ ᆍ఼ˈ䗮䖛᥻ࠊড䕀ˈৃҹ䅽⌟䆩বᕫ᳈ㅔऩˈ⌟䆩Ё䆒㕂ᅲ⦄᳡ࡵ᥹ষⱘ Mock ᇍ䈵ᅠ ᐌᓔྟᏆ㒣⹂ᅮ೼ᗢḋⱘ⦃๗ϟՓ⫼DŽWebWork ᳝㞾Ꮕⱘ᥻ࠊড䕀˄Inversion of Control˅ WebWork ৃҹ㜅⾏ Web ᑨ⫼Փ⫼ˈ䖭ϔ⚍ԐТᑊ≵᳝໾໮Ӭ࢓ˈ಴ЎˈϔϾᑨ⫼䗮 བ VelocityǃFreeMarker ੠ XSLT ㄝDŽ ⳌᇍѢ Struts 1 ҙᬃᣕ JSP 㸼⦄ሖᡔᴃⱘ㔎䱋㗠㿔ˈWebWork ᬃᣕ᳈໮ⱘ㸼⦄ሖᡔᴃˈ ㋏㒳Ң B/S 㒧ᵘ৥ C/S 㒧ᵘ䕀ᤶDŽ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 13 //username 䇋∖খ᭄ᇍᑨⱘ getter ᮍ⊩ } this.password = password; { public void setPassword(String password) //password 䇋∖খ᭄ᇍᑨⱘ setter ᮍ⊩ } return password; { public String getPassword() //password 䇋∖খ᭄ᇍᑨⱘ getter ᮍ⊩ private String username; private String password; //䆹 Action ᇕ㺙ⱘϸϾ䇋∖খ᭄ private final static String LOGINFAIL="loginfail"; //䆹ᄫヺІᐌ䞣ᇚ԰Ў Action ⱘ䖨ಲؐ { public class LoginAction implements Action Action ⼎՟˖ ϔϾ᱂䗮 Java ᇍ䈵ˈ䆹᥻ࠊ఼ҷⷕЁ≵᳝㗺ড়ӏԩ Servlet APIDŽⳟϟ䴶ⱘ WebWork ⱘڣ ⳌᇍѢ Struts 1 ḚᶊЁⱘ Action ߎ⦄њ໻䞣 Servlet API 㗠㿔ˈWebWork ⱘ Action ᳈ ˄1˅Action ᮴䳔Ϣ Servlet API 㗺ড়ˈ᳈ᆍᯧ⌟䆩 ೒ 1.8 WebWork ⱘ᭄᥂⌕೒ 㾚೒῵ᵓ Action ҷ⧚ 䜡㕂ㅵ⧚఼ ㄀ 1 ゴ Struts 2 ὖ䗄 14 ൟߚ߿ᰃ ActionMappingǃActionFormǃHttpServletRequest ੠ HttpServletResponseˈϔϾࣙ 䰸ℸП໪ˈStruts 1 Ё Action гࣙ৿њϔϾ execute ᮍ⊩ˈԚ䆹ᮍ⊩䳔㽕 4 Ͼখ᭄ˈ㉏ ᥹ষˈ䖬ৃҹ㒻ᡓϔϾ⠊㉏˗ԚϔᮺᏆ㒣㒻ᡓϔϾ⠊㉏ˈ߭ᛣੇⴔ䆹㉏ϡ㛑ݡ㒻ᡓ݊Ҫ⠊㉏DŽ ϔϾ㉏ᰃᅠܼϡৠⱘὖᗉ˖ᅲ⦄ϔϾ᥹ষᇍ㉏ⱘ∵ᶧ㽕ᇣᕫ໮ˈ䆹㉏гৃҹᅲ⦄݊Ҫӏᛣ Struts 1 Ёⱘ Action ㉏䳔㽕㒻ᡓ Struts 1 ⱘ Action ㉏DŽ៥Ӏⶹ䘧ˈᅲ⦄ϔϾ᥹ষ੠㒻ᡓ ҙҙᅲ⦄њ WebWork ⱘ Action ᥹ষˈࣙ৿њϔϾ execute ᮍ⊩DŽ ೼Ϟ䴶ⱘActionҷⷕЁˈϡ䲒থ⦄WebWorkЁⱘAction݊ᅲህᰃϔϾPOJOˈ䆹Action ˄2˅Action ᮴䳔Ϣ WebWork 㗺ড়ˈҷⷕ䞡⫼⥛催 ≵᳝ࣙ৿ӏԩ Servlet APIˈ⫮㟇≵᳝ WebWork ⱘ APIDŽ བᵰ䳔㽕⌟䆩Ϟ䴶ⱘ Action ҷⷕˈ⌟䆩⫼՟ⱘкݭᇚ䴲ᐌᆍᯧˈ಴Ў execute ᮍ⊩Ё Action ⹂ᅲ໾㞗㚓њˈ⹂ᅲϡབ WebWork ⱘ Action 䙷МӬ䲙DŽ ᔧ៥Ӏᇚ WebWork ⱘ Action ੠ Struts 1 ⱘ Action 䖯㸠ᇍ↨ᯊˈϡ䲒থ⦄ Struts 1 ⱘ APIˈ㗠ᰃҹϔϾ Map ᇍ䈵ҷ㸼њ HTTP Session ᇍ䈵DŽ ेՓ Action Ё䳔㽕䆓䯂 HTTP Session ᇍ䈵ˈձ✊≵᳝೼ҷⷕЁⳈ᥹ߎ⦄ HttpSession ៾఼䋳䋷߱ྟ࣪ˈҹ⫼᠋䇋∖খ᭄Ў݊䌟ؐDŽ 㗠ᰃⳈ᥹䇗⫼䆓䯂䆹 Action ⱘ username ੠ password ៤ਬሲᗻüü䖭ϸϾሲᗻ⬅ Action ᢺ ᭄˖username ੠ password ᯊˈAction ᑊ᳾䗮䖛 HttpServletRequest ᇍ䈵ᴹ㦋ᕫ䇋∖খ᭄ˈ ೼Ϟ䴶ⱘ Action ҷⷕЁˈ៥Ӏⳟϡࠄӏԩⱘ Servlet APIˈᔧ㋏㒳䳔㽕໘⧚ϸϾ䇋∖খ } } } return LOGINFAIL; { else } return SUCCESS; session.put("username",getUsername()); Map session = ctx.getSession(); //ᇚᔧࠡⱏᔩⱘ⫼᠋ৡֱᄬࠄ Session ActionContext ctx = ActionContext.getContext(); { && "password".equals(getPassword())) if ("yeeku".equalsIgnoreCase(getUsername()) { public String execute() throws Exception //໘⧚⫼᠋䇋∖ⱘ execute ᮍ⊩ } this.username = username; { public void setUsername(String username) //username 䇋∖খ᭄ᇍᑨⱘ setter ᮍ⊩ } return username; { public String getUsername() Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 15 ᥻ࠊ఼ҷ⧚䋳䋷໘⧚⫼᠋䇋∖ˈ໘⧚⫼᠋䇋∖ᯊಲ䇗Ϯࡵ᥻ࠊ఼ⱘ execute ᮍ⊩ˈ䆹 ˄Advice˅ˈҹ⫼᠋ⱘϮࡵ䘏䕥᥻ࠊ఼ЎⳂᷛˈ߯ᓎϔϾ᥻ࠊ఼ҷ⧚DŽ Ң᭄᥂⌕೒ϞᴹⳟˈStruts 2 Ϣ WebWork ⳌᏂϡ໻ˈStruts 2 ৠḋՓ⫼ᢺ៾఼԰Ў໘⧚ 1.3.1 Struts 2 Ḛᶊᶊᵘ ⫼᠋ⱘϮࡵ䘏䕥᥻ࠊ఼Ϣ Servlet API ߚ⾏DŽ 䆌ܕᖗˈ㗠ϡᰃՓ⫼ Struts 1 ⱘ䆒䅵ḌᖗDŽStruts 2 ໻䞣Փ⫼ᢺ៾఼ᴹ໘⧚⫼᠋䇋∖ˈҢ㗠 Struts 2 ⱘԧ㋏Ϣ Struts 1 ԧ㋏ⱘᏂ߿䴲ᐌ໻ˈ಴Ў Struts 2 Փ⫼њ WebWork ⱘ䆒䅵Ḍ 1.3 Struts 2 ԧ㋏ҟ㒡 Struts 1 ᳝ᴎᭈড়ˈҢ㗠ֱ䆕 Struts 1 ᓔথ㗙㛑ᑇ〇䖛⏵ࠄ Struts 2DŽ ᔧ✊ˈᇍѢӴ㒳ⱘ Struts 1 ᓔথ㗙ˈStruts 2 гᦤկњᕜདⱘ৥ৢݐᆍᗻˈStruts 2 ৃϢ WebWork ᓔথ㗙Ӯথ⦄ˈҢ WebWork 䖛⏵ࠄ Struts 2 ᰃϔӊ䴲ᐌㅔऩⱘџᚙDŽ Ḛᶊˈ಴Ў Struts 2 ᰃҹ WebWork ЎḌᖗˈ㗠ϡᰃҹ Struts 1 ЎḌᖗⱘDŽℷ಴Ўℸˈ䆌໮ Struts 1 ڣ೼ৢ䴶ⱘҟ㒡Ёˈ៥ӀӮথ⦄ˈStruts 2 䴲ᐌ㉏ԐѢ WebWork Ḛᶊˈ㗠ϡ 㸠㒘ড়ⱘ᳡ࡵˈㅔऩഄ䇈ˈShale ᰃϔ⾡ SOA˄䴶৥᳡ࡵᶊᵘ˅ᶊᵘDŽ  Struts ᅲ䋼ϞᰃϔϾᎼ໻ⱘǃ໡ᴖⱘ䇋∖໘⧚఼˗㗠 Shale ߭ᰃϔ㒘㛑ҹӏԩᮍᓣ䖯  Struts Ϣ JSF 䲚៤ˈ㗠 Shale ߭ᰃᓎゟ೼ JSF ПϞDŽ 㗠ϡᰃ Struts ⱘछ㑻DŽShale ೼ᕜ໮ᮍ䴶Ϣ Struts ᄬ೼ϡৠП໘ˈ݊Ё᳝ϸ⚍᳔Ўさߎ˖ ϔϾᮄⱘḚᶊˈڣᛇˈᅗϢॳ᳝ⱘ Struts 1 ⱘ݇㘨ᕜᇥˈᅗՓ⫼ܼᮄⱘ䆒䅵ᗱᛇDŽShale ᳈ Struts ߚ࣪ߎᴹⱘ঺໪ϔϾḚᶊᰃ Shaleˈ䖭ϾḚᶊ䖰䖰䍙ߎњ Struts 1 ॳ᳝ⱘ䆒䅵ᗱ 䆌 Struts 1 ੠ WebWork ᓔথ㗙ৠᯊՓ⫼ Struts 2 ḚᶊDŽܕˈϸϾḚᶊ ЎḌᖗˈStruts 2 ЎӴ㒳 Struts 1 ⊼ܹњ WebWork ⱘ䆒䅵⧚ᗉˈ㒳ϔњ Struts 1 ੠ WebWork ⱘ Struts 2 ḚᶊDŽStruts 2 㱑✊ᰃ೼ Struts 1 ⱘ෎⸔Ϟথሩ䍋ᴹⱘˈԚᅲ䋼Ϟᰃҹ WebWork ⳂࠡˈStruts Ꮖ㒣ߚ࣪៤ϸϾḚᶊ˖㄀ϔϾḚᶊህᰃӴ㒳 Struts 1 ੠ WebWork 㒧ড়ৢ ᮄⱘ MVC Ḛᶊ㫀݈ࢗ䍋ˈStruts 1 гᓔྟњ㸔⎆ⱘ᳈ᮄDŽ 䛑ᕫࠄњᑓ⊯ⱘ䆕ᯢDŽԚ⬅Ѣᅗ໾Ā㗕āњˈϔѯ䆒䅵Ϟⱘ㔎䱋៤Ўᅗⱘ⹀ӸDŽ䴶ᇍ໻䞣 㒣䖛Ѩᑈ໮ⱘথሩˈStruts 1 Ꮖ㒣៤ЎϔϾ催ᑺ៤❳ⱘḚᶊˈϡㅵᰃ〇ᅮᗻ䖬ᰃৃ䴴ᗻˈ 1.2.3 Struts 2 䍋⑤ ㄝ䛑᳝ᕜདⱘᬃᣕˈҢ㗠㒭ᓔথ᳈໮ⱘ䗝ᢽˈᦤկњ᳈དⱘ䗖ᑨᗻDŽ ℷབҢ೒ 1.8 ᠔㾕ࠄⱘˈWebWork ᇍ໮⾡㸼⦄ሖᡔᴃ˖JSPǃVelocity ੠ FreeMarker ˄3˅ᬃᣕ᳈໮ⱘ㸼⦄ሖᡔᴃˈ᳝᳈དⱘ䗖ᑨᗻ API 㗺ড়ˈҢ㗠݋᳝᳈དⱘҷⷕ䞡⫼⥛DŽ ᕫⲞѢ WebWork ♉Ꮋⱘ䆒䅵ˈWebWork Ёⱘ Action ᮴䳔Ϣӏԩ Servlet APIǃWebWork г≵᳝ߎ⦄ӏԩ WebWork APIˈ䖭Ͼᮍ⊩೼ӏԩ⦃๗ϟ䛑᳝䞡⫼ⱘӋؐDŽ ໡⫼ӋؐDŽԚ WebWork ⱘ execute ᮍ⊩߭ᅠܼϡৠˈ䆹ᮍ⊩Ё≵᳝ߎ⦄ӏԩ Servlet APIˈ ৿њ䖭 4 Ͼখ᭄ⱘᮍ⊩ˈ䰸њ೼ Struts 1 Ḛᶊϟ᳝⫼໪ˈヨ㗙䲒ҹᛇ䈵ߎ䆹ҷⷕ䖬᳝ӏԩ ㄀ 1 ゴ Struts 2 ὖ䗄 16 ϟ䴶ᰃ struts.xml 䜡㕂᭛ӊⱘ⼎՟˖ ㉏ˈᑊᅮН䆹 Action ໘⧚㒧ᵰϢ㾚೒䌘⑤П䯈ⱘ᯴ᇘ݇㋏DŽ struts.xml ᭛ӊݙᅮНњ Struts 2 ⱘ㋏߫ Actionˈᅮ Н Action ᯊˈᣛᅮ䆹 Action ⱘᅲ⦄  䜡㕂 Struts 2 ܼሔሲᗻⱘ struts.properties ᭛ӊDŽ  䜡㕂 Action ⱘ struts.xml ᭛ӊDŽ Struts 2 ⱘ䜡㕂᭛ӊ᳝ϸӑ˖ ᔧ Struts 2 ߯ᓎ㋏㒳ⱘ Action ҷ⧚ᯊˈ䳔㽕Փ⫼ Struts 2 ⱘ䜡㕂᭛ӊDŽ 1.3.2 Struts 2 ⱘ䜡㕂᭛ӊ ᣕ VelocityǃFreeMarker ㄝ῵ᵓᡔᴃDŽ гৃҹᰃ PDF ᭛ḷ៪㗙݊Ҫ᭛ḷDŽℸᯊᬃᣕⱘ㾚೒ᡔᴃ䴲ᐌ໮ˈ᮶ᬃᣕ JSPˈгᬃˈڣ೒ Action ⱘ execute ᮍ⊩໘⧚㒧ᵰֵᙃᇚ㹿䕧ߎࠄ⌣㾜఼Ёˈৃҹᰃ HTML 义䴶ǃ Action াᰃϔϾ᥻ࠊ఼ˈᅗӮ䇗⫼Ϯࡵ䘏䕥㒘ӊᴹ໘⧚⫼᠋ⱘ䇋∖DŽ ᭄᥂ᑧ᪡԰ˈ᮶ৃҹᰃᇚ᭄᥂ֱᄬࠄ᭄᥂ᑧˈгৃҹҢ᭄᥂ᑧЁẔ㋶ֵᙃDŽᅲ䰙Ϟˈ಴Ў 㦋প⫼᠋䇋∖খ᭄ˈ✊ৢᠻ㸠ᶤ⾡ܜ⫣ಲ䇗 Action ⱘ execute ᮍ⊩ˈ䆹 execute ᮍ ϞӴㄝࡳ㛑DŽ WebWork ⱘᢺ៾఼䫒㞾ࡼᇍ䇋∖ᑨ⫼䗮⫼ࡳ㛑ˈ՟བ workflowǃvalidation ៪᭛ӊ ᅮ䇗⫼ড়䗖ⱘ ActionDŽއ∖Ḍᖗ᥻ࠊ఼ FilterDispatcher ḍ᥂䇋 ⌣㾜఼থ䗕䇋∖ˈ՟བ䇋∖/mypage.actionǃ/reports/myreport.pdf ㄝDŽ Struts 2 Ḛᶊⱘ໻㟈໘⧚⌕⿟བϟ˖ ೒ 1.9 Struts 2 ⱘԧ㋏ὖ೒ ೒ 1.9 ᰒ⼎њ Struts 2 ⱘԧ㋏ὖ೒DŽ ᅮњ Struts 2 ᇚᗢḋⱘ㾚೒䌘⑤ਜ⦄㒭⫼᠋DŽއᮍ⊩ⱘ䖨ಲؐᇚ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 17 ⳟϟ䴶ⱘ JSP 义䴶ⱘ㸼ऩᅮН⠛↉˖ ᇥ义䴶ҷⷕⱘкݭDŽޣ䗮䖛Փ⫼ Struts 2 ⱘᷛㅒˈᓔথ㗙ৃҹ᳔໻䰤ᑺഄ ೑䰙࣪ǃAjax ᬃᣕㄝࡳ㛑DŽ 㛑ˈ䖭ѯᷛㅒᑧϡҙᦤկњ㸼⦄ሖ᭄᥂໘⧚ˈ㗠Ϩᦤկњ෎ᴀⱘ⌕⿟᥻ࠊࡳ㛑ˈ䖬ᦤկњ Struts 2 ⱘᷛㅒᑧгᰃ Struts 2 ⱘ䞡㽕㒘៤䚼ߚˈStruts 2 ⱘᷛㅒᑧᦤկњ䴲ᐌЄᆠⱘࡳ 1.3.3 Strut 2 ⱘᷛㅒᑧ ᑨ⫼ⱘܼሔሲᗻDŽ ℷབϞ䴶㾕ࠄⱘˈstruts.properties ᭛ӊⱘᔶᓣᰃ㋏߫ⱘ keyǃvalue ᇍˈᅗᣛᅮњ Struts 2 struts.configuration.xml.reload=true //ᣛᅮᔧ Struts 2 䜡㕂᭛ӊᬍবৢˈWeb Ḛᶊᰃ৺䞡ᮄࡴ䕑 Struts 2 䜡㕂᭛ӊ struts.devMode = false #ᣛᅮ Struts 2 ໘Ѣᓔথ⢊ᗕ 䆹᭛ӊⱘ⼎՟བϟ˖ 䰸ℸП໪ˈStruts 2 䖬᳝ϔϾ䜡㕂 Struts 2 ܼሔሲᗻⱘ Properties ᭛ӊ˖struts.propertiesDŽ ᰃ঺ϔϾ Actionüü䖭гᰃ Struts 2 ৃҹᬃᣕ໮⾡㾚೒ᡔᴃⱘॳ಴DŽ 㗠 type ᣛᅮ䕀৥ⱘ䌘⑤㉏ൟˈℸ໘䕀৥ⱘ䌘⑤ৃҹᰃ JSPˈгৃҹᰃ FreeMarker ㄝˈ⫮㟇 ㋴ᯊˈৃҹᣛᅮϸϾሲᗻ˖type ੠ nameDŽ݊Ё name ᣛᅮњ execute ᮍ⊩䖨ಲⱘᄫヺІˈܗ 㸼⼎ᔧ execute ᮍ⊩䖨ಲ cancel ⱘᄫヺІᯊˈ䏇䕀ࠄ Welcome ⱘ ActionDŽᅮН result Welcome ᮍ⊩䖨ಲؐ੠㾚೒䌘⑤П䯈ⱘ᯴ᇘ݇㋏DŽᇍѢབϟ䜡㕂⠛↉˖ ㋴ᣛᅮ executeܗ ⱘᅲ⦄㉏ˈ㗠ϨⱘᅮН Action ⱘ໘⧚㒧ᵰᯊˈᣛᅮњ໮Ͼ resultˈresult ೼Ϟ䴶ⱘ struts.xml ᭛ӊЁˈᅮНњϸϾ ActionDŽᅮН Action ᯊˈϡҙᅮНњ Action Welcome ChangePassword MainMenu Welcome /pages/Logon.jsp ㄀ 1 ゴ Struts 2 ὖ䗄 18 ࡵ᥻ࠊ఼ ActionDŽ 㒘ӊЎḌᖗⱘDŽℷབࠡ䴶ᦤࠄⱘˈStruts 2 ⱘ᥻ࠊ఼⬅ϸϾ䚼ߚ㒘៤˖FilterDispatcher ੠Ϯ Struts 2 ⱘ᥻ࠊ఼㒘ӊᰃ Struts 2 ḚᶊⱘḌᖗˈџᅲϞˈ᠔᳝ MVC Ḛᶊ䛑ᰃҹ᥻ࠊ఼ 1.3.4 Struts 2 ⱘ᥻ࠊ఼㒘ӊ ࡳ㛑䴲ᐌᔎ໻DŽ ᔎ໻੠♉⌏ⱘ㸼䖒ᓣ䇁㿔˖OGNL˄Object Graph Notation Language˅ˈ಴ℸ ⱘᷛㅒᑧDŽ㗠Ϩ Struts 2 ⱘᷛㅒᬃᣕ㸼䖒ᓣ䇁㿔ˈ䖭⾡㸼䖒ᓣ䇁㿔ᬃᣕϔϾ ㌑䡻 Struts 2 ⱘᷛㅒᑧⱘࡳ㛑䴲ᐌ໡ᴖˈ䆹ᷛㅒᑧ޴Тৃҹᅠܼ᳓ҷ JSTL ೒ 1.10 Փ⫼ Struts 2 ᷛㅒⱘᬜᵰ 㸠᭄᥂᷵偠ৢⱘ䕧ߎDŽ ߭义䴶ҷⷕ᳈ࡴㅔ⋕ˈ㗠Ϩ᳝᳈ㅔऩⱘ䫭䇃䕧ߎDŽ೒ 1.10 ᰃϞ䴶Փ⫼ Struts 2 ᷛㅒᠻ བᵰᤶ៤བϟՓ⫼ Struts 2 ᷛㅒⱘᅮНᮍᓣ˖ ㋴ˈ䖬ϡ݋໛䕧ߎ᷵偠ֵᙃⱘࡳ㛑ˈԚܗϞ䴶义䴶Փ⫼њӴ㒳ⱘ HTML ᷛㅒᅮН㸼ऩ ୰⃶ⱘ买㡆˖
ᑈ㑾˖
ৡᄫ˖
Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ ㄀ 1 ゴ Struts 2 ὖ䗄 ᅲ䰙ϞˈStruts 2 ᑨ⫼Ё䍋԰⫼ⱘϮࡵ᥻ࠊ఼ϡᰃ⫼᠋ᅮНⱘ Actionˈ㗠ᰃ㋏㒳⫳៤ⱘ Action ҷ⧚ˈԚ䆹 Action ҷ⧚ҹ⫼᠋ᅮНⱘ Action ЎⳂᷛDŽ ϟ䴶ᰃ Struts 2 ⱘ Action ҷⷕ⼎՟˖ public class LoginAction { //ᇕ㺙⫼᠋䇋∖খ᭄ⱘ username ሲᗻ private String username; //ᇕ㺙⫼᠋䇋∖খ᭄ⱘ password ሲᗻ private String password; //username ሲᗻⱘ getter ᮍ⊩ public String getUsername() { return username; } //username ሲᗻⱘ setter ᮍ⊩ public void setUsername(String username) { this.username = username; } //password ሲᗻⱘ getter ᮍ⊩ public String getPassword() { return password; } //password ሲᗻⱘ setter ᮍ⊩ public void setPassword(String password) { this.password = password; } //໘⧚⫼᠋䇋∖ⱘ execute ᮍ⊩ public String execute() throws Exception { //བᵰ⫼᠋ৡЎ scottˈᆚⷕЎ tigerˈ߭ⱏᔩ៤ࡳ if (getUsername().equals("scott") && getPassword().equals("tiger") ) { return "success"; } else { return "error"; } } } 䗮䖛ᶹⳟϞ䴶ⱘ Action ҷⷕˈথ⦄䆹 Action ↨ WebWork Ёⱘ Action ᳈ᕏᑩˈ䆹 Action ᮴䳔ᅲ⦄ӏԩ⠊᥹ষˈ᮴䳔㒻ᡓӏԩ Struts 2 ෎㉏ˈ䆹 Action ㉏ᅠܼᰃϔϾ POJO˄᱂䗮ǃ Ӵ㒳ⱘ Java ᇍ䈵˅ˈ಴ℸ݋᳝ᕜདⱘ໡⫼ᗻDŽ ᔦ㒇䍋ᴹˈ䆹 Action ㉏᳝བϟӬ࢓˖ 19 20  㸼䖒ᓣ䇁㿔ᮍ䴶ⱘᇍ↨˖Struts 1 ᭈড়њ JSTLˈ಴ℸৃҹՓ⫼ JSTL 㸼䖒ᓣ䇁㿔DŽ 䆹 Model ᇍ䈵᮴䳔㒻ᡓӏԩ Struts 2 ෎㉏ˈᰃϔϾ POJOˈҢ㗠䰡Ԣњҷⷕ∵ᶧDŽ ModelDriven ῵ᓣˈৃҹ䅽ᓔথ㗙Փ⫼ऩ⣀ⱘ Model ᇍ䈵ᴹᇕ㺙⫼᠋䇋∖খ᭄ˈԚ ᇍ䈵㉏ൟDŽབᵰᓔথ㗙ձ✊ᗔᗉ Struts 1 ActionForm ⱘ῵ᓣˈStruts 2 ᦤկњ 㽕໻䞣ᓔথ ActionForm ㉏ⱘ⚺⧤ˈᅲ䰙Ϟˈ䖭ѯሲᗻ䖬ৃҹᰃࣙ৿ᄤሲᗻⱘ Rich њᓔথ㗙䳔ܡActionForm˗Struts 2 Ⳉ᥹Փ⫼ Action ሲᗻᴹᇕ㺙⫼᠋䇋∖ሲᗻˈ䙓 њࡼᗕ ActionForm ᴹㅔ࣪ ActionForm ⱘᓔথˈԚձ✊䳔㽕೼䜡㕂᭛ӊЁᅮН ಴ℸˈᓔথ㗙ᖙ乏߯ᓎ໻䞣ⱘ ActionForm ㉏ᇕ㺙⫼᠋䇋∖খ᭄DŽ㱑✊ Struts 1 ᦤկ ActionForm ᖙ乏㒻ᡓϔϾ෎㉏˖ActionFormDŽ᱂䗮ⱘ JavaBean ϡ㛑⫼԰ ActionFormˈ  ᇕ㺙䇋∖খ᭄ⱘᇍ↨˖Struts 1 Փ⫼ ActionForm ᇍ䈵ᇕ㺙⫼᠋ⱘ䇋∖খ᭄ˈ᠔᳝ⱘ 䆩 Struts 1 ⱘ Action ㉏DŽStruts 2 Action ৃҹ䗮䖛߱ྟ࣪ǃ䆒㕂ሲᗻǃ䇗⫼ᮍ⊩ᴹ⌟䆩DŽ ˄῵ᢳњ HttpServetRequest ੠ HttpServletResponse ᇍ䈵˅ˈҢ㗠ৃҹ㜅⾏ Web ᆍ఼⌟ ࡽѢ㄀ϝᮍᠽሩ˖Struts TestCaseˈ䆹ᠽሩϟࣙ৿њ㋏߫ⱘ Mock ᇍ䈵׳Actionˈᖙ乏 APIˈ䖭Փᕫ Action ⱘ⌟䆩㽕ձ䌪Ѣ Web ᆍ఼DŽЎњ㜅⾏ Web ᆍ఼⌟䆩 Struts 1 ⱘ  ৃ⌟ᗻᮍ䴶ⱘᇍ↨˖⌟䆩 Struts 1 Action ⱘϔϾЏ㽕䯂乬ᰃ execute ᮍ⊩ձ䌪Ѣ Servlet 䯂 HttpServetRequest ੠ HttpServletResponseˈҢ㗠㒭ᓔথ㗙᳈໮♉⌏ⱘ䗝ᢽDŽ খ᭄ˈStruts 2 Action ҡ✊ৃҹ䆓䯂ᅗӀDŽԚᰃˈ໻䚼ߚᯊ׭ˈAction 䛑᮴䳔Ⳉ᥹䆓 ⱘ䲒ᑺDŽᔧ✊ˈབᵰ Action 䳔㽕Ⳉ᥹䆓䯂 HttpServletRequest ੠ HttpServletResponse 䆌 Action 㜅⾏ Web ᆍ఼䖤㸠ˈҢ㗠䰡Ԣњ⌟䆩 Actionܕݡձ䌪Ѣ Servlet APIˈҢ㗠 execute ᮍ⊩Ё᳝ HttpServletRequest ੠ HttpServletResponse ᮍ⊩DŽStruts 2 Action ϡ  Servlet ձ䌪ᮍ䴶ⱘᇍ↨˖Struts 1 Action ձ䌪Ѣ Servlet APIˈ಴Ў Struts 1 Action ⱘ ᇍ䈵Ў↣ϔϾ䇋∖ѻ⫳ϔϾᅲ՟ˈ಴ℸ≵᳝㒓⿟ᅝܼ䯂乬DŽ ᑊϨ㽕೼ᓔথᯊ⡍߿ᇣᖗDŽAction 䌘⑤ᖙ乏ᰃ㒓⿟ᅝܼⱘ៪ৠℹⱘ˗Struts 2 Action ⱘџˈخAction ⱘϔϾᅲ՟ᴹ໘⧚᠔᳝ⱘ䇋∖DŽऩ՟ㄪ⬹䰤ࠊњ Struts 1 Action 㛑  㒓⿟῵ᓣᮍ䴶ⱘᇍ↨˖Struts 1 Action ᰃऩ՟῵ᓣᑊϨᖙ乏ᰃ㒓⿟ᅝܼⱘˈ಴Ўҙ᳝ Ͼࣙ৿ execute ᮍ⊩ⱘ POJO ㉏䛑ৃҹ⫼԰ Struts 2 ⱘ ActionDŽ ActionSupport ෎㉏এᅲ⦄ᐌ⫼ⱘ᥹ষDŽेՓ Action ᥹ষϡᰃᖙ乏ᅲ⦄ⱘˈা᳝ϔ ᥹ষˈгৃҹᅲ⦄݊Ҫ᥹ষˈՓৃ䗝੠ᅮࠊⱘ᳡ࡵ៤Ўৃ㛑DŽStruts 2 ᦤկϔϾ ϔϾ݋ԧ䯂乬ᰃՓ⫼ᢑ䈵㉏㓪⿟㗠ϡᰃ᥹ষDŽStruts 2 Action ㉏ৃҹᅲ⦄ϔϾ Action  ೼ Action ᅲ⦄㉏ᮍ䴶ⱘᇍ↨˖Struts 1 㽕∖ Action ㉏㒻ᡓϔϾᢑ䈵෎㉏˗Struts 1 ⱘ ϔϾ䴲ᐌ݋᳝ᅲ⫼Ӌؐⱘ MVC ḚᶊDŽϟ䴶ᰃ Struts 1 ੠ Struts 2 ೼৘ᮍ䴶ⱘㅔ㽕ᇍ↨DŽ ߎњᎼ໻ⱘᬍ䖯ˈⱘ⹂ᰃخ㒣䖛Ϟ䴶ㅔ㽕ҟ㒡ˈϡ䲒থ⦄ˈStruts 2 ⹂ᅲ೼ Struts 1 Ϟ 1.4 Struts 2 Ϣ Struts 1 ⱘᇍ↨ ԩⱘ㾚೒ˈ⫮㟇ᰃ঺ϔϾ ActionDŽ  Action ㉏ⱘ execute ᮍ⊩ҙ䖨ಲϔϾᄫヺІ԰Ў໘⧚㒧ᵰˈ䆹໘⧚㒧ᵰৃ᯴ᇘࠄӏ 䆩䴲ᐌㅔऩDŽ⌟ܗ Action ㉏᮴䳔Ϣ Servlet API 㗺ড়ˈ಴ℸ䖯㸠ऩ  Action ㉏ᅠܼᰃϔϾ POJOˈ಴ℸ݋᳝ᕜདⱘҷⷕ໡⫼ᗻDŽ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 21  ᆠ᭛ᴀ㓪䕥఼ᷛㅒ˖Struts 2 ϡݡᬃᣕ WebWork ⱘᆠ᭛ᴀ㓪䕥఼ˈབᵰᑨ⫼Ё䳔㽕 䳔㽕Փ⫼ AroundInterceptorˈ߭ᑨ䆹㞾Ꮕ᠟ࡼᇐܹ WebWork Ёⱘ AroundInterceptor ㉏DŽ  AroundInterceptor˖Struts 2 ϡݡᬃᣕ WebWork Ёⱘ AroundInterceptorDŽབᵰᑨ⫼⿟ᑣЁ 䰸ℸП໪ˈStruts 2 гߴ䰸њ WebWork Ёᇥ䞣⡍ᗻ˖ org.apache.Struts2.config.Settings com.opensymphony.webwork.config.Configuration Dispatcher DispatcherUtil struts.properties webwork.properties struts.xml xwork.xml org.apache.Struts2.* com.opensymphony.webwork.* com.opensymphony.xwork2.* com.opensymphony.xwork.* Struts 2 ៤ਬ WebWork ៤ਬ 㸼 1.1 Struts 2 ੠ WebWork ៤ਬৡ⿄ⱘᇍᑨ ϟ䴶ᰃ Struts 2 Ϣ WebWork ੑৡϞᄬ೼ᬍব˄㾕㸼 1.1˅˖ WebWork ⱘᓔথ㒣偠ˈᇚৃҹ᳈ࡴ䖙䗳ഄ䖯ܹ Struts 2 ⱘᓔথ乚ඳDŽ ೼ᕜ໮ᮍ䴶ˈStruts 2 ҙҙᰃᬍবњ WebWork ϟⱘৡ⿄ˈ಴ℸˈབᵰᓔথ㗙݋᳝ ᳈咏⚺DŽ ݊ᅲᰃ WebWork 2.3 㗠ᏆˈҢ WebWork 2.2 䖕⿏ࠄ Struts 2.0 ϡӮ↨Ң WebWork 2.1 ࠄ 2.2 ⱘ Struts 2 ⱘᅬᮍ᭛ḷ䛑ᦤࠄ˖WebWork ࠄ Struts 2 ᰃϔ⃵ᑇ⒥ⱘ䖛⏵DŽᅲ䰙ϞˈStruts 2.0 Ңᶤ⾡⿟ᑺϞᴹⳟˈStruts 2 ᰃ WebWork ⱘछ㑻ˈ㗠ϡᰃ Struts 1 ⱘछ㑻ˈ⫮㟇೼ Apache 1.5 WebWork ੠ Struts 2 ᇍ↨ ᥂䳔㽕߯ᓎⳌᑨේᷜˈҢ㗠੠ϡৠⱘ Action ϔ䍋Փ⫼DŽ ఼ේᷜ˄Interceptor Stacks˅Ў↣ϔϾ Action ߯ᓎϡৠⱘ⫳ੑ਼ᳳDŽᓔথ㗙ৃҹḍ ὖᗉ˅ˈԚᰃ῵ഫЁⱘ᠔᳝ Action ᖙ乏݅ѿⳌৠⱘ⫳ੑ਼ᳳDŽStruts 2 ᬃᣕ䗮䖛ᢺ៾  Action ᠻ㸠᥻ࠊⱘᇍ↨˖Struts 1 ᬃᣕ↣ϔϾ῵ഫᇍᑨϔϾ䇋∖໘⧚˄े⫳ੑ਼ᳳⱘ ⊩䖯㸠᷵偠ˈгᬃᣕᭈড় XWork ᷵偠Ḛᶊ䖯㸠᷵偠DŽ 䗮䖛ᭈড় Commons alidator Ḛᶊᴹᅠ៤᭄᥂᷵偠DŽStruts 2 ᬃᣕ䗮䖛䞡ݭ validate ᮍ  ᭄᥂᷵偠ⱘᇍ↨˖Struts 1 ᬃᣕ೼ ActionForm 䞡ݭ validate ᮍ⊩Ё᠟ࡼ᷵偠ˈ៪㗙 Փ⫼ OGNL 䖯㸠㉏ൟ䕀ᤶˈᬃᣕ෎ᴀ᭄᥂㉏ൟ੠ᐌ⫼ᇍ䈵П䯈ⱘ䕀ᤶDŽ Commons-Beanutils 䖯㸠㉏ൟ䕀ᤶˈ↣Ͼ㉏ϔϾ䕀ᤶ఼ˈ䕀ᤶ఼ᰃϡৃ䜡㕂ⱘ˗Struts 2  ㉏ൟ䕀ᤶⱘᇍ↨˖Struts 1 ActionForm ሲᗻ䗮ᐌ䛑ᰃ String ㉏ൟDŽStruts 1 Փ⫼ ೼ϔ䍋DŽ Փ⫼ĀValueStackāᡔᴃˈՓᷛㅒᑧ㛑໳䆓䯂ؐˈ㗠ϡ䳔㽕ᡞᇍ䈵੠㾚೒义䴶㒥ᅮ JSP ᴎࠊᡞᇍ䈵㒥ᅮࠄ㾚೒义䴶˗Struts 2 ޚ 㒥ᅮؐࠄ㾚೒ⱘᇍ↨˖Struts 1 Փ⫼ᷛ Graph Notation Language˅ˈ಴ℸˈStruts 2 ϟⱘ㸼䖒ᓣ䇁㿔ࡳ㛑᳈ࡴᔎ໻DŽ Struts 2 ৃҹՓ⫼ JSTLˈԚᅗᭈড়њϔ⾡᳈ᔎ໻੠♉⌏ⱘ㸼䖒ᓣ䇁㿔˖OGNL˄Object 䖭⾡㸼䖒ᓣ䇁㿔᳝෎ᴀᇍ䈵೒䘡ग़ˈԚ೼ᇍ䲚ড়੠㋶ᓩሲᗻⱘᬃᣕϞ߭ࡳ㛑ϡᔎ˗ ㄀ 1 ゴ Struts 2 ὖ䗄 22 ᛳফ෎Ѣ Struts 2 ⱘ MVC ᑨ⫼DŽ ϟϔゴᇚҹϔϾㅔऩⱘ HelloWorld ᑨ⫼Ў⼎՟ˈᴹҟ㒡෎Ѣ Struts 2 ⱘᑨ⫼ˈ䅽䇏㗙 ᶊⱘᶊᵘǃᷛㅒᑧǃ᥻ࠊ఼㒘ӊㄝˈᑊህ Struts 1 ੠ Struts 2 ⱘⳌ݇ᮍ䴶䖯㸠њ↨䕗DŽ Ң㗠ᓩ⬇ߎᇍ Struts 2 䍋⑤ⱘҟ㒡DŽ᳔ৢ໻㟈ҟ㒡њ Struts 2 Ḛᶊⱘԧ㋏ˈࣙᣀ Struts 2 Ḛ ⚍ҟ㒡њ Struts 2 ⱘϸϾࠡ䑿˖Struts 1 ੠ WebWorkˈҹঞ䖭ϸϾḚᶊⱘᶊᵘ੠Џ㽕⡍ᕕˈ Ḛᶊˈࣙᣀ JSFǃTapestry ੠ Spring MVCˈҹঞ䖭ѯḚᶊⱘ෎ᴀⶹ䆚੠Ⳍ݇⡍ᕕDŽᴀゴ䞡 㽕῵ൟ੠⡍ᕕˈ䖯㗠ҟ㒡њ MVC ῵ᓣⱘЏ㽕ㄪ⬹੠Џ㽕Ӭ࢓DŽ᥹ⴔҟ㒡њᐌ⫼ⱘ MVC ᴀゴ໻㟈ҟ㒡њ Web ᑨ⫼ⱘᓔথग़৆ˈҢग़৆ⱘ㾦ᑺҟ㒡њ Model 1 ੠ Model 2 ⱘㅔ 1.6 ᴀゴᇣ㒧 ఼ˈҹ Spring ⱘ IoC ᆍ఼԰Ў咬䅸ⱘ Object ᎹॖDŽ  IoC ᆍ఼ᬃᣕ˖Struts 2 ϡݡᬃᣕݙᓎⱘ IoC ᆍ఼ˈ㗠ᬍЎܼ䴶ᬃᣕ Spring ⱘ IoC ᆍ Փ⫼ᆠ᭛ᴀ㓪䕥఼ˈ߭ᑨ䆹Փ⫼ Dojo ⱘᆠ᭛ᴀ㓪䕥఼DŽ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ ᳔ᮄⱘѻક࣪ GA ⠜ᰃ Struts 2.06ˈᬙᴀкⱘ᠔᳝ᑨ⫼䛑ᰃ෎Ѣ䆹⠜ᴀⱘ Struts 2DŽᓎ䆂䇏 ヨ㗙ݭᴀкⱘᯊ׭ˈStruts 2 Ꮖ㒣থᏗњ݊ѻક࣪ GA˄General Availability˅⠜ˈ݊ᅲ ϟ䴶៥ӀҢϟ䕑ǃᅝ㺙 Struts 2 ᓔྟˈ᜶᜶ᓔྟԧ偠 Struts 2 MVC Ḛᶊⱘ儙࡯DŽ 2.1 ϟ䕑੠ᅝ㺙 Struts 2 Ḛᶊ ߭䖯ܹϔϾᦤ⼎义䴶DŽᔧ⫼᠋ᦤѸ㸼ऩᯊˈᴀᑨ⫼Ӯ᳝෎ᴀⱘ᭄᥂᷵偠DŽ ᆚⷕˈབᵰ⫼᠋䕧ܹⱘ⫼᠋ৡ੠ᆚⷕヺড়㽕∖ˈ߭䖯ܹϔϾ⃶䖢义䴶˗བᵰ⫼᠋䕧ܹ䫭䇃ˈ 䆌⫼᠋䕧ܹ⫼᠋ৡǃܕˈᴀ HelloWorld ᑨ⫼ᰃϔϾㅔऩⱘᑨ⫼˖⫼᠋䖯ܹϔϾⱏᔩ义䴶 䇋∖ˈབԩ䇗⫼Ϯࡵ᥻ࠊ఼໘⧚⫼᠋䇋∖ˈᑊҟ㒡 Action ໘⧚㒧ᵰ੠䌘⑤П䯈ⱘ᯴ᇘ݇㋏DŽ ϟ䴶ᇚҹϔϾ Struts 2 ⱘ HelloWorld ᑨ⫼Ў՟ˈҟ㒡 Strust 2 MVC Ḛᶊབԩᢺ៾⫼᠋ 䡈ᛣНDŽ׳ⱘ Ꮖ㒣᳝ WebWork ᓔথ㒣偠ⱘᓔথ㗙㗠㿔ˈWebWork ⱘᓔথ㒣偠ᇍ Struts 2 ⱘᓔথᇚ᳝ᕜད ᓔথ㒣偠ⱘᓔথ㗙㗠㿔ˈStruts 1 ⱘᓔথ㒣偠ᇍѢ Struts 2 ᑊ≵᳝໾໻ⱘᐂࡽ˗ⳌডˈᇍѢ 㱑✊ Struts 2 ᦤկњϢ Struts 1 ⱘݐᆍˈԚᏆ㒣ϡᰃ Struts 1 ⱘछ㑻DŽᇍѢᏆ᳝ Struts 1 Struts 2 ᰃ WebWork ⱘछ㑻ˈ㗠ϡᰃ Struts 1 ⱘछ㑻DŽ ᑊ䆺㒚ᇍ↨њ Struts 2 ੠ Struts 1 ⱘᏂᓖˈᇍ↨њ Struts 2 ੠ WebWork ⱘᏂᓖˈ㗠Ϩᣛߎ˖ ࠡ䴶Ꮖ㒣ㅔ㽕ҟ㒡њ Struts 2 ⱘ䍋⑤ˈҹঞ Struts 2 ⱘϸϾࠡ䑿˖Struts 1 ੠ WebWorkˈ  ᭄᥂᷵偠߱ℹ  ⿟ᑣ೑䰙࣪߱ℹ  Փ⫼ Struts 2 ⱘ㸼ऩᷛㅒ  ೼ JSP Ё䕧ߎ Action ⱘ䖨ಲؐ  ೼ Action Ё䆓䯂 HttpSession  䜡㕂 Struts 2 ⱘ Action  ᅲ⦄ Struts 2 ⱘ Action  㒃᠟Ꮉ߯ᓎϔϾ Struts 2 ᑨ⫼  㒃᠟Ꮉ߯ᓎϔϾ Web ᑨ⫼  Struts 2 ⱘϟ䕑੠ᅝ㺙 ᴀゴ㽕⚍ Struts 2 ϟⱘ HelloWorld ㄀ゴ 24 ⱘḌᖗ Filter 䜡㕂ⱘ web.xml 䜡㕂᭛ӊⱘҷⷕ˖ 㓪䕥 Web ᑨ⫼ⱘ web.xml 䜡㕂᭛ӊˈ䜡㕂 Struts 2 ⱘḌᖗ FilterDŽϟ䴶ᰃ๲ࡴњ Struts 2 WEB-INF/lib 䏃ᕘϟDŽ 䚼⡍ᗻˈ಴ℸ≵᳝ᖙ㽕ϔ⃵ᇚ䆹 lib 䏃ᕘϟ JAR ᭛ӊܼ䚼໡ࠊࠄ Web ᑨ⫼ⱘ ㌑䡻 ໻䚼ߚᯊ׭ˈՓ⫼ Struts 2 ⱘ Web ᑨ⫼ᑊϡ䳔㽕߽⫼ࠄ Struts 2 ⱘܼ ੠ xwork-2.0.1.jar ⏏ࡴࠄ㋏㒳ⱘ CLASSPATH ⦃๗ব䞣䞠DŽ ೼ DOS ៪㗙 Shell にষϟ᠟ࡼ㓪䆥 Struts 2 Ⳍ݇ⱘ⿟ᑣˈ߭䖬ᑨ䆹ᇚ Struts2-core-2.0.6.jar ᳈໮⡍ᗻˈ߭䳔㽕ᇚ᳈໮ⱘ JAR ᭛ӊ໡ࠊࠄ Web ᑨ⫼ⱘ WEB-INF/lib 䏃ᕘϟDŽབᵰ䳔㽕 ᑧ໡ࠊࠄ Web ᑨ⫼ⱘ WEB-INF/lib 䏃ᕘϟDŽᔧ✊ˈབᵰԴⱘ Web ᑨ⫼䳔㽕Փ⫼ Struts 2 ⱘ ᇚ lib ᭛ӊ།ϟⱘ Struts2-core-2.0.6.jarǃxwork-2.0.1.jar ੠ ognl-2.6.11.jar ㄝᖙ䳔㉏  src˖䆹᭛ӊ།ϟࣙ৿њ Struts 2 Ḛᶊⱘܼ䚼⑤ҷⷕDŽ  lib˖䆹᭛ӊ།ϟࣙ৿њ Struts 2 ḚᶊⱘḌᖗ㉏ᑧˈҹঞ Struts 2 ⱘ㄀ϝᮍᦦӊ㉏ᑧDŽ  j4˖䆹᭛ӊ།ϟࣙ৿њ䅽 Struts 2 ᬃᣕ JDK 1.4 ⱘ JAR ᭛ӊDŽ ⱘ᭛ḷˈҹঞ API ᭛ḷㄝݙᆍDŽ  docs˖䆹᭛ӊ།ϟࣙ৿њ Struts 2 ⱘⳌ݇᭛ḷˈࣙᣀ Struts 2 ⱘᖿ䗳ܹ䮼ǃStruts 2 ᐌ᳝⫼ⱘ䌘᭭DŽ  apps˖䆹᭛ӊ།ϟࣙ৿њ෎Ѣ Struts 2 ⱘ⼎՟ᑨ⫼ˈ䖭ѯ⼎՟ᑨ⫼ᇍѢᄺд㗙ᰃ䴲 䆹᭛ӊህᰃϔϾ݌ൟⱘ Web 㒧ᵘˈ䆹᭛ӊ།ࣙ৿བϟ᭛ӊ㒧ᵘ˖ 䗮ᐌᓎ䆂䇏㗙ϟ䕑㄀ϔϾ䗝乍˖ϟ䕑 Struts 2 ⱘᅠᭈ⠜ˈᇚϟ䕑ࠄⱘ Zip ᭛ӊ㾷य़㓽ˈ ᇚࣙᣀ䆹䗝乍ϟⱘܼ䚼ݙᆍDŽ  Alternative Java 4 JARs˖ϟ䕑ৃ䗝ⱘ JDK 1.4 ⱘᬃᣕ JARDŽϟ 䕑 Struts 2 ⱘᅠᭈ⠜ᯊ 䚼ݙᆍDŽ  Source˖ϟ䕑 Struts 2 ⱘܼ䚼⑤ҷⷕˈϟ䕑 Struts 2 ⱘᅠᭈ⠜ᯊᇚࣙᣀ䆹䗝乍ϟⱘܼ ੠ API ᭛ḷㄝDŽϟ䕑 Struts 2 ⱘᅠᭈ⠜ᯊᇚࣙᣀ䆹䗝乍ϟⱘܼ䚼ݙᆍDŽ  Documentation˖ҙҙϟ䕑 Struts 2 ⱘⳌ݇᭛ḷˈࣙ৿ Struts 2 ⱘՓ⫼᭛ḷǃখ㗗᠟ݠ ᣀ䆹䗝乍ϟⱘܼ䚼ݙᆍDŽ  Essential Dependencies˖ҙҙϟ䕑 Struts 2 ⱘḌᖗᑧˈϟ䕑 Struts 2 ⱘᅠᭈ⠜ᯊᇚࣙ Example Applications 䗝乍ϟDŽ  Blank Application only˖ҙϟ䕑 Struts 2 ⱘぎ⼎՟ᑨ⫼ˈ䖭Ͼぎᑨ⫼Ꮖ㒣ࣙ৿೼ ᕜ໻ⱘᐂࡽˈϟ䕑 Struts 2 ⱘᅠᭈ⠜ᯊᏆ㒣ࣙ৿њ䆹䗝乍ϟܼ䚼ᑨ⫼DŽ  Example Applications˖ϟ䕑 Struts 2 ⱘ⼎՟ᑨ⫼ˈ䖭ѯ⼎՟ᑨ⫼ᇍѢᄺд Struts 2 ᳝  Full Distribution˖ϟ䕑 Struts 2 ⱘᅠᭈ⠜DŽ䗮ᐌᓎ䆂ϟ䕑䆹䗝乍DŽ ⠜DŽ೼ Struts 2.06 ϟ᳝བϟ޴Ͼ䗝乍˖ ⱏᔩ http://struts.apache.org/download.cgi#Struts206 キ⚍ˈϟ䕑 Struts 2 ⱘ᳔ᮄ GA ϟ䕑੠ᅝ㺙 DWR 䇋ᣝབϟℹ偸䖯㸠DŽ ৃҹϟ䕑᳈ᮄⱘ GA ⠜ˈⳌֵϡӮ᳝໾໻ᏂᓖDŽ 㗙ϟ䕑 Struts 2.06 ⠜ˈ㗠ϡᰃϟ䕑᳔ᮄⱘ Beta ⠜ˈབᵰ Struts 2 ᳔᳝ᮄⱘ GA ⠜ˈ䇏㗙г Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 25 ⫼᠋ৡ˖

⫼᠋ⱏᔩ

ⱏᔩ义䴶 <%@ page language="java" contentType="text/html; charset=GBK"%> 䴶ҷⷕ˖ ৢ䴶๲ࡴࡼᗕݙᆍˈ಴ℸձ✊ᇚ䆹义䴶ҹ jsp Ўৢ㓔ֱᄬDŽϟ䴶ᰃ⫼᠋䇋∖ⱏᔩⱘ JSP 义 ԩࡼᗕݙᆍˈᅲ䰙ϞˈᭈϾ义䴶ᅠܼৃҹᰃϔϾ䴭ᗕ HTML 义䴶DŽԚ㗗㰥ࠄ䳔㽕೼䆹义䴶 ϟ䴶ᰃϔϾ᳔ㅔऩⱘ㸼ऩᦤѸ义䴶ˈ䆹义䴶ⱘ㸼ऩݙҙࣙ৿ϸϾ㸼ऩඳˈ⫮㟇≵᳝ӏ 䖭Ͼ㸼ऩᦤѸ义䴶ࣙ৿њϸϾ㸼ऩඳ˖⫼᠋ৡ੠ᆚⷕDŽ ᴀⱘ㾚೒ᡔᴃ˖JSP ᡔᴃDŽᔧ⫼᠋䳔㽕ⱏᔩᴀ㋏㒳ᯊˈ⫼᠋䳔㽕ϔϾㅔऩⱘ㸼ऩᦤѸ义䴶ˈ Struts 2 ᬃᣕ໻䚼ߚ㾚೒ᡔᴃˈᔧ✊гᬃᣕ᳔Ӵ㒳ⱘ JSP 㾚೒ᡔᴃˈᴀᑨ⫼ᇚՓ⫼᳔෎ 2.2 Ң⫼᠋䇋∖ᓔྟ 䴶ᇚᏺ乚䇏㗙䖯ܹ Struts 2 MVC ḚᶊⱘϪ⬠DŽ 㒣䖛Ϟ䴶 3 Ͼℹ偸ˈ៥ӀᏆ㒣ৃҹ೼ϔϾ Web ᑨ⫼ЁՓ⫼ Struts 2 ⱘ෎ᴀࡳ㛑њˈϟ /*Struts2 org.apache.Struts2.dispatcher.FilterDispatcher struts2 com/xml/ns/j2ee/web-app_2_4.xsd"> xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ܗ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 27 ೼ WEB-INF/classes ϟ๲ࡴ struts.xml 䜡㕂᭛ӊDŽ ᇚ Struts 2 Ḛᶊⱘ㉏ᑧ໡ࠊࠄ Web ᑨ⫼ⱘ WEB-INF/lib 䏃ᕘϟDŽ ׂᬍ web.xml ᭛ӊˈ೼ web.xml ᭛ӊЁ䜡㕂 Struts 2 ⱘḌᖗ FilterDŽ ᑨ⫼Ёᅝ㺙 Struts 2 ḚᶊḌᖗা䳔㽕㒣䖛བϟϝϾℹ偸DŽ Ўњ㒭 Web ᑨ⫼๲ࡴ Struts 2 ࡳ㛑ˈা䳔㽕ᇚ Struts 2 ᅝ㺙ࠄ Web ᑨ⫼ЁेৃDŽ೼ Web 2.3.2 ๲ࡴ Struts 2 ࡳ㛑 ਃࡼ Tomcatˈ೼⌣㾜఼Ё⌣㾜 2.2 㡖ᅮНⱘ JSP 义䴶ˈᇚⳟࠄབ೒ 2.1 ᠔⼎ⱘ义䴶DŽ ⫼ϟ JSP 义䴶ⱘৡᄫˈгৃҹׂᬍDŽ݊Ҫ᭛ӊ།ǃ䜡㕂᭛ӊ䛑ϡৃҹׂᬍDŽ Ϟ䴶ⱘ Struts2qs ᰃ Web ᑨ⫼᠔ᇍᑨ᭛ӊ།ⱘৡᄫˈৃҹ᳈ᬍ˗login.jsp ᰃ䆹 Web ᑨ |ˉlogin.jsp | |ˉweb.xml | |ˉlib | |ˉclasses |ˉWEB-INF Struts2qs Web ᑨ⫼ⱘϔϾ义䴶DŽ䆹 Web ᇚ᳝བϟ᭛ӊ㒧ᵘ˖ ᇚ 2.2 㡖᠔ᅮНⱘ JSP 义䴶᭛ӊ໡ࠊࠄ㄀ 1 ℹ᠔ᓎⱘ᭛ӊ།ϟˈ䆹 JSP 义䴶ᇚ៤Ў䆹 䏃ᕘϟˈ䆹 Web ᑨ⫼ᇚৃҹ㞾ࡼ䚼㕆೼ Tomcat ЁDŽ 㒣䖛Ϟ䴶ℹ偸ˈᏆ㒣ᓎゟњϔϾぎ Web ᑨ⫼DŽᇚ䆹 Web ᑨ⫼໡ࠊࠄ Tomcat ⱘ webapps ѢֱᄬऩϾ*.class ᭛ӊ੠ JAR ᭛ӊDŽ ೼㄀ 2 ℹ᠔ᓎⱘ WEB-INF 䏃ᕘϟˈᮄᓎϸϾ᭛ӊ།˖classes ੠ libˈᅗӀߚ߿⫼ com/xml/ns/j2ee/web-app_2_4.xsd"> xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ܗ ㋴ -->ܗ Action ໘⧚㒧ᵰ੠䌘⑤П䯈ⱘ᯴ᇘ݇㋏DŽϟ䴶ᰃ䆹ᑨ⫼ⱘ struts.xml ᭛ӊⱘҷⷕ˖ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ ㄀ 2 ゴ Struts 2 ϟⱘ HelloWorld ᇍѢϞ䴶ⱘ໘⧚⌕⿟ˈৃҹㅔ࣪Ўབϟⱘ⌕⿟˖⫼᠋䕧ܹ ϸϾখ᭄ˈे username ੠ passwordˈ✊ৢ৥ login.action থ䗕 䇋∖ˈ䆹䇋∖㹿 FilterDispatcher 䕀থ㒭 LoginAction ໘⧚ˈབ ᵰ LoginAction ໘⧚⫼᠋䇋∖䖨ಲ success ᄫヺІˈ߭䖨ಲ㒭 ⫼᠋ welcome.jsp 义䴶˗བᵰ䖨ಲ error ᄫヺІˈ߭䖨ಲ㒭⫼ ᠋ error.jsp 义䴶DŽ ೒ 2.2 ⱏᔩ៤ࡳ义䴶 ೒ 2.3 ᰒ⼎њϞ䴶ᑨ⫼ⱘ໘⧚⌕⿟DŽ FilterDispatcher ೒ 2.3 HelloWorld ᑨ⫼ⱘ໘⧚⌕⿟ 2.5 ᬍ䖯᥻ࠊ఼ 䗮䖛ࠡ䴶ҟ㒡ˈ䇏㗙Ꮖ㒣ৃҹᅠ៤ㅔऩⱘ Struts 2 ⱘ෎ᴀᑨ⫼њˈԚ䖬ৃҹ䖯ϔℹᬍ 䖯ᑨ⫼ⱘ Action ㉏ˈ՟བ䆹 Action ㉏ৃҹ䗮䖛ᅲ⦄ Action ᥹ষˈ߽⫼䆹᥹ষⱘӬ࢓DŽࠡ 䴶ᑨ⫼ⱘ Action ㉏≵᳝Ϣ JavaBean ѸѦˈ≵᳝ᇚϮࡵ䘏䕥᪡԰ⱘ㒧ᵰᰒ⼎㒭ᅶ᠋ッDŽ 2.5.1 ᅲ⦄ Action ᥹ষ 㸼䴶Ϟⳟ䍋ᴹˈᅲ⦄ Struts 2 ⱘ Action ᥹ষ≵᳝໾໻ⱘད໘ˈҙӮ∵ᶧ䆹 Action ⱘᅲ ⦄㉏DŽџᅲϞˈᅲ⦄ Action ᥹ষৃҹᐂࡽᓔথ㗙᳈དഄᅲ⦄ Action ㉏DŽϟ䴶佪ܜⳟ Action ᥹ষⱘᅮН˖ public interface Action { //ϟ䴶ᅮНњ 5 ϾᄫヺІᐌ䞣 public static final String SUCCESS = "success"; public static final String NONE = "none"; public static final String ERROR = "error"; public static final String INPUT = "input"; public static final String LOGIN = "login"; //ᅮН໘⧚⫼᠋䇋∖ⱘ execute ᢑ䈵ᮍ⊩ public String execute() throws Exception; } 31 32 } } } return ERROR; { else } return SUCCESS; { && getPassword().equals("tiger") ) if (getUsername().equals("scott") //৺߭䖨ಲ error ⱘᄫヺІ ᄫヺІ //ᔧ⫼᠋䇋∖খ᭄ⱘ username ㄝѢ scottˈᆚⷕ䇋∖খ᭄Ў tiger ᯊˈ䖨ಲ success { public String execute() throws Exception //໘⧚⫼᠋䇋∖ⱘ execute ᮍ⊩ } this.password = password; { public void setPassword(String password) //password ሲᗻᇍᑨⱘ setter ᮍ⊩ } return password; { public String getPassword() //password ሲᗻᇍᑨⱘ getter ᮍ⊩ } this.username = username; { public void setUsername(String username) //username ሲᗻᇍᑨⱘ setter ᮍ⊩ } return username; { public String getUsername() //username ሲᗻᇍᑨⱘ getter ᮍ⊩ private String password; private String username; //ϟ䴶ᰃ Action ݙ⫼Ѣᇕ㺙⫼᠋䇋∖খ᭄ⱘϸϾሲᗻ { public class LoginAction implements Action //ᅲ⦄ Action ᥹ষᴹᅲ⦄ Struts 2 ⱘ Action ㉏ ࡽѢϞ䴶ⱘ Action ᥹ষˈ៥Ӏৃҹᇚॳᴹⱘ Action ㉏ҷⷕׂᬍЎབϟ˖׳ˈ಴ℸ ㄝ˅DŽ Ⳉ᥹䖨ಲ᭄ᄫᐌ䞣ǃᄫヺІᐌ䞣ܡⳈ᥹䖨ಲϔϾ success ᄫヺІ˄⿟ᑣЁᑨ䆹ሑ䞣䙓ܡ䙓 ࣪DŽ՟བᇍѢ໘⧚៤ࡳˈ߭䖨ಲ SUCCESS ᐌ䞣ˈޚᑊৃҹՓ⫼ execute ᮍ⊩ⱘ䖨ಲؐᷛ SUCCESSǃNONEǃERRORǃINPUT ੠ LOGINˈᅗӀৃҹㅔ࣪ execute ᮍ⊩ⱘ䖨ಲؐˈ ᄫヺІᐌ䞣˖ޚ೼Ϟ䴶ⱘ Action ҷⷕЁˈ៥Ӏথ⦄䆹 Action ᥹ষ䞠Ꮖ㒣ᅮНњ 5 Ͼᷛ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 33 ៤ࡳ义䴶 <%@ page language="java" contentType="text/html; charset=GBK"%> 义䴶ҷⷕ˖ 义䴶ЁՓ⫼ JSP 2.0 㸼䖒ᓣ䇁⊩䕧ߎ Session Ёⱘ user ሲᗻDŽϟ䴶ᰃׂᬍৢⱘ welcome.jsp ЎњẔ偠៥Ӏ䆒㕂ⱘ Session ሲᗻᰃ৺៤ࡳˈ៥Ӏׂᬍ welcome.jsp 义䴶ˈ೼ welcome.jsp ㉏ҷⷕᅠܼϔḋDŽ೼Ϟ䴶ⱘ Action ㉏䗮䖛 ActionContext 䆒㕂њϔϾ Session ሲᗻ˖userDŽ Ϟ䴶ⱘҷⷕҙᦤկњ Action ㉏ⱘ execute ᮍ⊩ˈ䆹 Action ㉏ⱘ݊Ҫ䚼ߚϢࠡ䴶ⱘ Action } } return ERROR; { else } return SUCCESS; ActionContext.getContext().getSession().put("user" , getUsername()); //䗮䖛 ActionContext ᇍ䈵䆓䯂 Web ᑨ⫼ⱘ Session { && getPassword().equals("tiger") ) if (getUsername().equals("scott") //৺߭䖨ಲ error ⱘᄫヺІ ヺІ //ᔧ⫼᠋䇋∖খ᭄ⱘ username ㄝѢ scottˈᆚⷕ䇋∖খ᭄Ў tiger ᯊˈ䖨ಲ success ᄫ { public String execute() throws Exception //໘⧚⫼᠋䇋∖ⱘ execute ᮍ⊩ ActionContext 䆓䯂 Web ᑨ⫼ⱘ SessionDŽׂᬍৢⱘ execute ᮍ⊩ҷⷕབϟ˖ Ўњৃҹ䎳䏾⫼᠋ֵᙃˈ៥Ӏׂᬍ Action ㉏ⱘ execute ᮍ⊩ˈ೼ execute ᮍ⊩Ё䗮䖛 ఼Ӯ䋳䋷䆹 Session ੠ HttpSession П䯈ⱘ䕀ᤶDŽ 㱑✊ ActionContext ⱘ getSession 䖨ಲⱘϡᰃ HttpSession ᇍ䈵ˈԚ Struts 2 ⱘ㋏߫ᢺ៾ HttpSessionDŽ Action ㉏Ϣ Servlet API ᅠܼߚ⾏ˈ಴ℸ getSession ᮍ⊩ⱘ䖨ಲؐ㉏ൟᰃ Mapˈ㗠ϡᰃ ಲџਸ਼˛ᅲ䰙Ϟˈ䖭Ϣ Struts 2 ⱘ䆒䅵૆ᄺ᳝݇ˈStruts 2 Ўњㅔ࣪ Action ㉏ⱘ⌟䆩ˈᇚ getSession ⱘᮍ⊩ˈԚ䆹ᮍ⊩ⱘ䖨ಲؐ㉏ൟᑊϡᰃ HttpSessionˈ㗠ᰃ MapDŽ䖭জᰃᗢМ Ўњ䆓䯂 HttpSession ᅲ՟ˈStruts 2 ᦤկњϔϾ ActionContext ㉏ˈ䆹㉏ᦤկњϔϾ ᙃüü䗮ᐌˈᔧϔϾ⫼᠋ⱏᔩ៤ࡳৢˈ䳔㽕ᇚ⫼᠋ⱘ⫼᠋ৡ⏏ࡴЎ Session ⢊ᗕֵᙃDŽ ࠡ䴶ⱘ Action ໘⧚ᅠ⫼᠋ⱏᔩৢˈҙҙᠻ㸠њㅔऩⱘ义䴶䕀থˈᑊ᳾䎳䏾⫼᠋⢊ᗕֵ 2.5.2 䎳䏾⫼᠋⢊ᗕ ᮍ⊩ৃҹ䖨ಲ Action ᥹ষ䞠ⱘᄫヺІᐌ䞣DŽ 䰸њৢ䴶ⱘ Action ㉏ᅲ⦄њ Action ᥹ষDŽ಴Ўᅲ⦄њ Action ᥹ষˈᬙ Action ㉏ⱘ execute ᇍ↨ࠡ䴶 Action ੠ℸ໘ⱘ Action ᅲ⦄㉏ˈ៥Ӏথ⦄ϸϾ Action ㉏ⱘҷⷕ෎ᴀⳌԐˈ ㄀ 2 ゴ Struts 2 ϟⱘ HelloWorld 34 ⱘϮࡵ䘏䕥㒘ӊ߭াᰃ䖨ಲϔϾ䴭ᗕⱘᄫヺІ᭄㒘üü಴Ў䖭াᰃϔ⾡῵ᢳDŽ ⊩DŽ⧚䆎ϞˈϮࡵ䘏䕥㒘ӊᅲ⦄Ϯࡵ䘏䕥ᮍ⊩ᯊˈᖙ乏ձ䌪ѢᑩሖⱘᣕЙሖ㒘ӊˈԚℸ໘ Ϟ䴶ⱘϮࡵ䘏䕥㒘ӊᅲ䰙Ϟህᰃ MVC ῵ᓣЁⱘ Modelˈᅗ䋳䋷ᅲ⦄㋏㒳Ϯࡵ䘏䕥ᮍ } } return books; { public String[] getLeeBooks() //Ϯࡵ䘏䕥ᮍ⊩ˈ䆹ᮍ⊩䖨ಲܼ䚼೒к }; "Struts,Spring,Hibernate ᭈড়ᓔথ" "෎Ѣ J2EE ⱘ Ajax ᅱ݌", "䕏䞣㑻 J2EE ӕϮᑨ⫼ᅲ៬", "Spring2.0 ᅱ݌" , new String[]{ private String[] books = 䆒໛˄᭄᥂ᑧ˅Ёপߎⱘ᭄᥂ټ //ҹϔϾᐌ䞣᭄㒘῵ᢳњҢᣕЙᄬ { public class BookService ϟ䴶ᰃ㋏㒳᠔⫼ⱘϮࡵ䘏䕥㒘ӊⱘҷⷕ˖ 䆹ᮍ⊩ৃҹ㦋পᶤϾ㋏߫ⱘܼ䚼к㈡DŽ Action ৃҹ㦋প䖭㋏߫ⱘк㈡ˈ៥Ӏ๲ࡴϔϾϮࡵ䘏䕥㒘ӊˈᅗࣙ৿ϔϾϮࡵ䘏䕥ᮍ⊩ˈ ϟ䴶ᇚЎᑨ⫼๲ࡴϔϾ Actionˈ䆹 Action 䋳䋷㦋পᶤϾ㋏߫ⱘܼ䚼к㈡DŽЎњ䅽䆹 ܼ䚼ଚકˈ㗠 JSP 义䴶߭㦋প䆹 Action ⱘ໘⧚㒧ᵰˈᑊᇚܼ䚼㒧ᵰ䗁ҷ䕧ߎDŽ 䳔㽕ᶹ䆶ᶤϾ⾡㉏ϟⱘଚકˈ߭ Action 䇗⫼Ϯࡵ䘏䕥㒘ӊⱘϮࡵ䘏䕥ᮍ⊩ᕫࠄ䆹⾡㉏ϟⱘ ᇍѢ໻䚼ߚ Web ᑨ⫼㗠㿔ˈ⫼᠋䳔㽕㦋ᕫ䇋∖ Action ⱘ໘⧚㒧ᵰˈ՟བˈ೼㒓䌁⠽㋏㒳 ࠄⳂࠡЎℶˈAction ҙҙ᥻ࠊ䕀থ⫼᠋䇋∖ˈJSP 义䴶ᑊ᳾㦋ᕫ Action ⱘ໘⧚㒧ᵰDŽ 2.5.3 ⏏ࡴ໘⧚ֵᙃ scottˈৃ㾕೼ Action 䗮䖛 ActionContext 䆒㕂 Session ᰃ៤ࡳⱘDŽ೒ 2.4 ⱏᔩ៤ࡳ义䴶 ೼Ϟ䴶ⱏᔩ៤ࡳⱘ义䴶ЁˈᏆ㒣䕧ߎⱏᔩ᠔⫼ⱘ⫼᠋ৡ˖ བ೒ 2.4 ᠔⼎ⱘ义䴶DŽ Āᆚⷕā䕧ܹḚЁ䕧ܹ tigerˈ✊ৢऩߏĀⱏᔩāᣝ䪂ˈᇚⳟࠄ ೼བ೒ 2.1 ᠔⼎义䴶ⱘĀ⫼᠋ৡā䕧ܹḚЁ䕧ܹ scottˈ೼ Ϯᑨ⫼ᅲ៬NJϔкⱘ㄀ 2 ゴDŽ Session Ёⱘ user ሲᗻDŽ݇Ѣ JSP 2.0 㸼䖒ᓣⱘⶹ䆚ˈ䇋খⳟヨ㗙᠔㨫ⱘlj䕏䞣㑻 J2EE ӕ Ϟ䴶ⱘ JSP 义䴶Ϣࠡ䴶ⱘ JSP 义䴶≵᳝໾໻ᬍবˈ䰸њՓ⫼њ JSP 2.0 䇁⊩ᴹ䕧ߎ ⃶䖢ˈ${sessionScope.user},ᙼᏆ㒣ⱏᔩ! Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 35 ValueStack ᇍ䈵ЁDŽ 㺙њ Action 䳔㽕ӴܹϟϔϾ义䴶ᰒ⼎ⱘؐDŽᅲ䰙Ϟˈ䖭ѯؐᇚ㹿ᇕ㺙೼ ㌑䡻 Action Ёⱘ៤ਬሲᗻˈᑊϔᅮ⫼Ѣᇕ㺙⫼᠋ⱘ䇋∖খ᭄ˈгৃ㛑ᰃᇕ 䇋∖খ᭄ˈгৃ㛑ᰃᇕ㺙њ Action 䳔㽕ӴܹϟϔϾ JSP 义䴶Ёᰒ⼎ⱘሲᗻDŽ 䗮䖛Ϟ䴶ⱘ Action ㉏ˈ៥Ӏথ⦄ Action ㉏Ёⱘ៤ਬሲᗻˈᑊϡϔᅮ⫼Ѣᇕ㺙⫼᠋ⱘ } } } return LOGIN; { else } return SUCCESS; setBooks(bs.getLeeBooks()); //ᇚϮࡵ䘏䕥㒘ӊⱘ䖨ಲؐ䆒㕂៤䆹 Action ⱘሲᗻ BookService bs = new BookService(); //߯ᓎ BookService ᅲ՟ { if (user != null && user.equals("scott")) //བᵰ user ሲᗻϡЎぎˈϨ䆹ሲᗻؐЎ scott get("user"); String user = (String)ActionContext.getContext().getSession(). //㦋প Session Ёⱘ user ሲᗻ { public String execute() throws Exception //໘⧚⫼᠋䇋∖ⱘ execute ᮍ⊩ } return books; { public String[] getBooks() //books ሲᗻⱘ getter ᮍ⊩ } this.books = books; { public void setBooks(String[] books) //books ሲᗻⱘ setter ᮍ⊩ private String[] books; //䆹ሲᗻᑊϡ⫼Ѣᇕ㺙⫼᠋䇋∖খ᭄ˈ㗠⫼Ѣᇕ㺙 Action 䳔㽕䕧ߎࠄ JSP 义䴶ֵᙃ { public class GetBooksAction implements Action ᮄ๲ⱘ Action ㉏ⱘҷⷕབϟ˖ ᴀ㋏㒳ˈ߭㦋প㋏㒳Ёܼ䚼к㈡ˈ৺߭䖨ಲⱏᔩ义䴶DŽ ㄝѢ scott ᄫヺІüü䖭㽕∖ᶹⳟ೒кПࠡˈ⫼᠋ᖙ乏Ꮖ㒣ⱏᔩᴀ㋏㒳DŽབᵰ⫼᠋Ꮖ㒣ⱏᔩ ߸ᮁ Session Ё user ሲᗻᰃ৺ᄬ೼ˈᑊϨܜ೼㋏㒳Ё๲ࡴབϟ Action ㉏ˈ䆹 Action ㉏ ሖ㒘ӊᴹ㦋প᭄᥂ᑧֵᙃDŽ 㰩ぐ ℸ໘ⱘϮࡵ䘏䕥㒘ӊাᰃ῵ᢳᅲ⦄Ϯࡵ䘏䕥ᮍ⊩ˈᑊ᳾ⳳℷ䇗⫼ᣕЙ ㄀ 2 ゴ Struts 2 ϟⱘ HelloWorld 36 request.getAttribute("struts.valueStack"); //㦋পᇕ㺙䕧ߎֵᙃⱘ ValueStack ᇍ䈵 䕧ߎֵᙃⱘ ValueStack ᇍ䈵DŽ Ўњ೼ JSP 义䴶Ё䕧ߎ䳔㽕䕧ߎⱘ೒кֵᙃˈ៥Ӏৃҹ䗮䖛བϟҷⷕᴹ㦋পࣙ৿ܼ䚼 䖛 getAttributeNames ᮍ⊩ߚᵤ䇋∖Ёⱘܼ䚼ሲᗻDŽ 䖛ᶹⳟ Struts 2 ⱘ৘⾡᭛ḷˈ᳔䞡㽕ⱘϔ⚍ᰃৃҹ೼ showBook.jsp 义䴶Ё䗮 struts.valueStack 䇋∖ሲᗻ䞠ⱘ?䖭ϔᮍ䴶Ϣ㓪⿟㒣偠᳝݇ˈ঺ϔᮍ䴶ৃҹ䗮 ㌑䡻 䇏㗙ৃ㛑ᛳࠄ༛ᗾ˖ヨ㗙ᰃབԩⶹ䘧 Struts 2 ᇚ䖭ѯሲᗻؐᇕ㺙೼ struts.valueStack ⱘ䇋∖ሲᗻ䞠DŽ خᔧ Action 䆒㕂њᶤϾሲᗻؐৢˈStruts 2 ᇚ䖭ѯሲᗻܼؐ䚼ᇕ㺙೼ϔϾি ⱘࡼ԰ˈᬙℸ໘Փ⫼ JSP 㛮ᴀᴹ䕧ߎܼ䚼೒кֵᙃDŽ 㒭 JSP 䕧ߎⱘֵᙃˈԚヨ㗙Ўњ䅽䇏㗙᳈⏙Ἦ Struts 2 ᷛㅒᑧ೼ᑩሖ᠔ᅠ៤ 㰩ぐ ೼ᅲ䰙ᑨ⫼Ёˈ޴Т㒱ᇍϡӮՓ⫼ヨ㗙䖭⾡ᮍᓣᴹ䕧ߎ Action 䕀থ ϟ䴶ヨ㗙ᇚҹ᳔ॳྟⱘᮍᓣ˖JSP 㛮ᴀᴹ䕧ߎܼ䚼೒кDŽ 䕀থ㒭 showBook.jsp 义䴶ˈ಴ℸ showBook.jsp 义䴶ᖙ乏䋳䋷䕧ߎܼ䚼೒кDŽ 䴶DŽབᵰ⫼᠋Ꮖ㒣ⱏᔩˈgetBooks.action ᇚҢ㋏㒳Ёࡴ䕑ࠄ㋏㒳Ёⱘ᠔᳝೒кˈᑊᇚ䇋∖ བᵰ⫼᠋≵᳝ⱏᔩˈⳈ᥹৥ getBooks.action থ䗕䇋∖ˈ䆹䇋∖ᇚ㹿䕀থࠄ login.jsp 义 2.5.4 䕧ߎ໘⧚ֵᙃ ᔧ⫼᠋৥ getBooks.action থ䗕䇋∖ᯊˈ䆹䇋∖ᇚ㹿䕀থ㒭 lee.GetBooksAction ໘⧚DŽ /showBook.jsp /login.jsp struts.xml ᭛ӊЁˈ䜡㕂䆹 Action ⱘ䜡㕂⠛↉བϟ˖ 䆹 Action ໘⧚⫼᠋䇋∖ᯊˈ᮴䳔㦋ᕫ⫼᠋ⱘӏԩ䇋∖খ᭄DŽᇚ䆹 Action 䜡㕂೼ ܹ᥻ࠊ఼㒘ӊDŽ Ꮉॖ῵ᓣㅵ⧚Ϯࡵ䘏䕥㒘ӊᅲ՟ˈ៪㗙䗮䖛ձ䌪⊼ܹᇚϮࡵ䘏䕥㒘ӊᅲ՟⊼ 㰩ぐ ᅲ䰙乍ⳂЁϡӮ೼᥻ࠊ఼ЁⳈ᥹߯ᓎϮࡵ䘏䕥㒘ӊⱘᅲ՟ˈ㗠ᰃ䗮䖛 ܹüü䖭ᇚ೼ৢ䴶ゴ㡖䞠ҟ㒡DŽ ҹ߽⫼Ꮉॖ῵ᓣᴹㅵ⧚Ϯࡵ䘏䕥㒘ӊ˗ᔧ✊ˈⳂ᳔ࠡ⌕㸠ⱘᮍᓣᰃ߽⫼ձ䌪⊼ৃ⫣خ❳ⱘ ಴Ў᥻ࠊ఼ϡᑨ䆹݇ᖗϮࡵ䘏䕥㒘ӊⱘᅲ՟࣪䖛⿟DŽ↨䕗៤ˈ⫣خ՟ˈ䖭ᑊϡᰃϔ⾡དⱘ ᔧ៥Ӏⱘ᥻ࠊ఼䳔㽕䇗⫼Ϯࡵ䘏䕥ᮍ⊩ᯊˈ៥ӀⳈ᥹߯ᓎњϔϾϮࡵ䘏䕥㒘ӊⱘᅲ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 37 䖛⿟ˈᏆ㒣ᅠܼࣙᣀњ Struts 2 Ḛᶊⱘ 3 Ͼ䚼ߚ˖㾚೒ǃ᥻ࠊ఼੠῵ൟDŽ 䗮䖛Ϟ䴶义䴶ˈ៥Ӏⳟࠄ JSP 义䴶Ꮖ㒣䕧ߎњ Struts 2 ᥻ࠊ఼ⱘ䖨ಲֵᙃDŽϞ䴶ᭈϾ ೼⌣㾜఼Ё৥ getBooks.action থ䗕䇋∖ˈᇚⳟࠄབ೒ 2.5 ᠔⼎ⱘ义䴶DŽ 㛮ᴀDŽԚᅗᇍѢ䇏㗙⧚㾷 Struts 2 བԩ໘⧚ᇕ㺙೼ Action ⱘ ValueStack ैᕜ᳝ᐂࡽDŽ ϡৃ৺䅸ˈϞ䴶 JSP 义䴶ⱘҷⷕᰃϥ䰟ⱘˈ㗠Ϩ䲒ҹ㓈ᡸˈ಴Ў䞠䴶䭊ጠњ໻䞣ⱘ Java
<%}%> <%=book%> кৡ˖ %> { for (String book : books) //䗁ҷ䕧ߎܼ䚼೒кֵᙃ String[] books = (String[])vs.findValue("books"); //䇗⫼ ValueStack ⱘ fineValue ᮍ⊩㦋প Action Ёⱘ books ሲᗻؐ ValueStack vs = (ValueStack)request.getAttribute("struts.valueStack"); //㦋পᇕ㺙䕧ߎֵᙃⱘ ValueStack ᇍ䈵 <% ԰㗙ᴢ߮ⱘ೒к ԰㗙ᴢ߮ⱘ೒к <% @page import="java.util.*,com.opensymphony.xwork2.util.*"%> <%@ page language="java" contentType="text/html; charset=GBK"> 义䴶ⱘҷⷕ˖ ⧚㾷њϞ䴶݇䬂ⱘϸℹˈᭈϾ JSP 义䴶ⱘҷⷕህ↨䕗ᆍᯧњ㾷њDŽϟ䴶ᰃ showBook.jsp vs.findValue("books"); //䇗⫼ ValueStack ⱘ fineValue ᮍ⊩ᶹⳟᶤϾ㸼䖒ᓣⱘؐ ܼ䚼೒кֵᙃDŽ ໻㟈⧚㾷њ ValueStack ᇍ䈵ⱘ㒧ᵘৢˈ៥Ӏৃҹ䗮䖛བϟҷⷕᴹ㦋প Action Ё䆒㕂ⱘ Ёⱘሲᗻৡৃҹ⧚㾷Ў ValueStack Ё value ⱘৡᄫDŽ Ўᅗৃҹḍ᥂㸼䖒ᓣᴹᶹ䆶ؐ˅DŽAction ᠔᳝ⱘሲᗻ䛑㹿ᇕ㺙ࠄњ ValueStack ᇍ䈵ЁˈAction Ң᭄᥂㒧ᵘϞᴹⳟˈValueStack ᳝⚍㉏ԐѢ Map 㒧ᵘˈԚᅗ↨ Map 㒧ᵘ᳈ࡴᔎ໻˄಴ 㺙ⱘֵᙃDŽ Struts 2 Փ⫼ⱘϔϾ ValueStack ᇍ䈵ˈৃҹ䗮䖛 OGNL 㸼䖒ᓣ䴲ᐌᮍ֓ഄ䆓䯂䆹ᇍ䈵ᇕ Ϟ䴶ҷⷕ䖨ಲϔϾ ValueStack ᇍ䈵ˈ䆹ᇍ䈵ᇕ㺙њܼ䚼ⱘ䕧ߎֵᙃDŽ䆹ᇍ䈵ᰃ ㄀ 2 ゴ Struts 2 ϟⱘ HelloWorld 38
԰㗙ᴢ߮ⱘ೒к
԰㗙ᴢ߮ⱘ೒к <%@taglib prefix="s" uri="/struts-tags"%> <%@ page language="java" contentType="text/html; charset=GBK" %> 义䴶ҷⷕབϟ˖ 䖛Փ⫼Ϟ䴶ⱘ޴Ͼᷛㅒˈ᳓ᤶ showBook.jsp 义䴶Ёⱘ Java 㛮ᴀˈׂᬍৢⱘ showBook.jsp ݇Ѣ Struts 2 ᷛㅒᑧ᳈⏅ܹⱘՓ⫼ˈ㄀ 10 ゴ䖬Ӯ⏅ܹҟ㒡ˈᬙℸ໘ϡݡ䆺㒚䆆㾷DŽ䗮  property˖䆹ᷛㅒ⫼Ѣ䕧ߎᣛᅮሲᗻؐDŽ ㋴DŽܗ iterator˖Џ㽕⫼Ѣ䗁ҷ䕧ߎᶤϾ䲚ড়ሲᗻⱘ৘Ͼ䲚ড় ߭䕧ߎ䆹ᷛㅒ䞠ⱘᷛㅒԧDŽˈ؛ᓣ䖨ಲ  else˖䆹ᷛㅒϡ㛑⣀ゟՓ⫼ˈᅗ䳔㽕Ϣ if ᷛㅒ㒧ড়Փ⫼ˈབᵰ if ᷛㅒݙ߸ᮁⱘ㸼䖒  if˖䆹ᷛㅒᬃᣕᷛㅒԧˈབᵰ if ᷛㅒ䞠߸ᮁⱘ㸼䖒ᓣ䖨ಲⳳˈ߭䕧ߎᷛㅒԧݙᆍDŽ 䕗ᐌ⫼ⱘᷛㅒ᳝˖ Ўњ᥻ࠊ䕧ߎ Struts 2 ⱘ ValueStack Ёᇕ㺙ⱘؐˈStruts 2 ᦤկњ໻䞣ⱘᷛㅒDŽ݊Ё↨ 2.6.1 ᬍ୘䕧ߎ义䴶 ϟ䴶ᇚӮՓ⫼ Struts 2 ⱘᷛㅒᴹᬍ୘ᭈϾᑨ⫼㾚೒㒘ӊDŽ ᐌϥ䰟ˈ⡍߿ᰃ䕧ߎ Action 䖨ಲֵᙃⱘ JSP 义䴶ˈՓ⫼њ໻䞣ⱘ Java 㛮ᴀᴹ᥻ࠊ䕧ߎˈ њ Struts 2 ᑨ⫼Ё῵ൟǃ᥻ࠊ఼ǃ㾚೒ 3 Ͼ㒘ӊⱘᓔথDŽԚᑨ⫼Ёⱘ㾚೒㒘ӊ˖JSP 义䴶䴲 䗮䖛ࠡ䴶ⱘ޴㡖ⱘҟ㒡ˈ៥ӀᏆ㒣ᯢⱑњ Struts 2 MVC Ḛᶊⱘ෎ᴀ᭄᥂⌕ˈᏆ㒣ᅠ៤ 2.6 ᬍ䖯㾚೒㒘ӊ ೒ 2.5 ೼ JSP 义䴶Ё䕧ߎ Action ⱘ䖨ಲֵᙃ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 39 ࠡ䴶᠔⼎ⱘ෎ᴀ᥻ࠊǃ᭄᥂䕧ߎㄝࡳ㛑ˈ䖬ᦤկњ䴲ᐌЄᆠⱘ UI 㒘ӊˈ䰸њᦤկ㋏߫ⱘ ࠡ䴶Ꮖ㒣ᦤࠄ䖛ˈStruts 2 ⱘϔϾ䞡㽕㒘ӊህᰃᷛㅒᑧDŽStruts 2 ᷛㅒᑧЁϡҙᦤկњ 2.6.2 Փ⫼ UI ᷛㅒㅔ࣪㸼ऩ义䴶 㓈ᡸ៤ᴀDŽ 䴶ЁҙՓ⫼ Struts 2 㛮ᴀ᥻ࠊ䕧ߎˈᅠܼ⍜䰸њ义䴶Ёⱘ Java 㛮ᴀˈ䰡Ԣњ䆹义䴶ⱘৢᳳ хˈ䰡Ԣњৃ䯙䇏ᗻǃৃ㓈ᡸᗻDŽԚᴀ义ޠ䞣ⱘ Java 㛮ᴀˈ䅽ᭈϾ义䴶ⱘҷⷕⳟ䍋ᴹ䴲ᐌ ݇䬂೼Ѣ 2.5.4 㡖Ёⱘ JSP 义䴶ҷⷕϢᴀ㡖义䴶ҷⷕⱘᏂᓖ˖ࠡ䴶 JSP 义䴶Փ⫼њ໻ 䖭гᕫⲞѢ Struts 2 ᷛㅒᑧⱘㅔ⋕DŽ Ϟ䴶义䴶ⱘ䕧ߎᬜᵰϢ೒ 2.5 ᑊ≵᳝໾໻ⱘϡৠˈাᰃՓ⫼ϡৠ买㡆ᴹߚ䱨њ䆄ᔩ㸠DŽ ೒ 2.6 Փ⫼ Struts 2 ᷛㅒᬍ୘ৢⱘ䕧ߎ⬠䴶 ᇚⳟࠄབ೒ 2.6 ᠔⼎ⱘ⬠䴶DŽ ⱏᔩᴀ㋏㒳˅ˈܜ೼⌣㾜఼Ёݡ⃵৥ getBooks.action থ䗕䇋∖˄থ䗕䇋∖Пࠡˈᖙ乏 Ͼ᳝㚠᱃㡆ⱘ㸼Ḑ㸠˗৺߭䕧ߎϔϾ᮴㚠᱃㡆ⱘ㸼Ḑ㸠DŽ ㋴ᅮНњϔϾᑣো˖indexDŽ䗮䖛߸ᮁᑣোᰃ৺Ў༛᭄ˈབᵰ㸠ᑣোЎ༛᭄ˈ߭䕧ߎϔܗ㒘 义䴶ЁՓ⫼ Struts 2 ⱘ iterator ᷛㅒ䗁ҷ䕧ߎ ValueStack Ёⱘ books ᭄㒘ˈᑊЎ↣Ͼ᭄ ㅒᑧDŽ ㌑䡻 བᵰ䳔㽕Փ⫼ᶤϾᷛㅒᑧЁⱘᷛㅒˈ߭ᖙ乏೼义䴶ⱘᓔྟᇐܹ䆹ᷛ 䆹 taglib ᣛҸ⫼ѢᇐܹᷛㅒᑧDŽ Ϟ䴶ⱘ JSP 义䴶Փ⫼њ Struts 2 ⱘᷛㅒᑧˈ಴ℸᖙ乏೼ JSP 义䴶ⱘ佪䚼⏏ࡴ taglib ᣛҸˈ
кৡ˖ ᭄ -->ي㋴ⱘᑣোᰃ৺ϡЎܗ ㋴ -->ܗ <s:text name="bookPageTitle"/> <%@taglib prefix="s" uri="/struts-tags"%> <%@ page language="java" contentType="text/html; charset=GBK"%> showBook.jsp 义䴶ҷⷕབϟ˖ ಴ℸˈ៥Ӏݡ⃵ׂᬍ㸼⦄ሖⱘ JSP 义䴶ˈՓ⫼೑䰙࣪ᷛㅒ䕧ߎ೑䰙ֵ࣪ᙃDŽׂᬍৢⱘ  ˖Փ⫼㸼䖒ᓣᮍᓣ䕧ߎ೑䰙ֵ࣪ᙃDŽ  ˖Փ⫼ s:text ᷛㅒᴹ䕧ߎ೑䰙ֵ࣪ᙃDŽ Struts 2 ᦤկњབϟϸ⾡ᮍᓣᴹ䕧ߎ೑䰙ֵ࣪ᙃ˖ ヺІᐌ䞣DŽ Ўњ䅽⿟ᑣৃҹᰒ⼎೑䰙ֵ࣪ᙃˈ߭䳔㽕೼ JSP 义䴶Ё䕧ߎ keyˈ㗠ϡᰃⳈ᥹䕧ߎᄫ 2.7.3 䕧ߎ೑䰙ֵ࣪ᙃ struts.custom.i18n.resources=lee.messageResource classes/lee 䏃ᕘϟ //ᅮН Struts 2 ⱘ䌘⑤᭛ӊⱘ baseName ᰃ messageResourceˈϨ᭛ӊᬒ೼ WEB-INF/ WEB-INF/classes/lee 䏃ᕘϟˈ߭䳔㽕ׂᬍ struts.properties ЁⱘᅮНབϟ˖ ֱᄬ೼䆹䏃ᕘϟⱘDŽབᵰᇚ䆹䌘⑤᭛ӊֱᄬ೼ WEB-INF/classes ⱘᄤⳂᔩϟˈ՟བֱᄬ೼ Struts 2 咬䅸ࡴ䕑 WEB-INF/classes ϟⱘ䌘⑤᭛ӊˈ೼Ϟϔ㡖Ёˈ៥Ӏህᰃᇚ䌘⑤᭛ӊ baseName ЎĀmessageResouceāüü䖭Ϣ៥Ӏࠡ䴶ֱᄬ䌘⑤᭛ӊⱘ baseName ᰃϔ㟈ⱘDŽ ㄀ 2 ゴ Struts 2 ϟⱘ HelloWorld 44 ೒ 2.11 䆒㕂 FireFox ⱘ䇁㿔⦃๗ ೒ 2.12 䆒㕂 FireFox ⱘ䇁㿔⦃๗Ў㣅䇁/㕢೑ Փ⫼㣅䇁/㕢೑ⱘ⦃๗DŽܜϾ义䴶Ӭ ߫㸼ḚЁ䗝ᢽĀ㣅䇁/㕢೑ [en-us]āˈ✊ৢऩߏĀϞ⿏āᣝ䪂ˈᇚ䆹䗝乍⿏㟇᳔Ϟ䴶ˈ䅽ᭈ ߏϟᢝḚে䖍ⱘĀ⏏ࡴāᣝ䪂ˈᇚ⏏ࡴњĀ㣅䇁/㕢೑āⱘ䇁㿔⦃๗DŽᑊ೼䆹ᇍ䆱ḚϞ䴶ⱘ ೼䆹ᇍ䆱Ḛϟ䴶ⱘϟᢝ߫㸼ḚЁ䗝ᢽĀ㣅䇁/㕢೑ [en-us]ā䗝乍ˈབ೒ 2.12 ᠔⼎ˈ✊ৢऩ ೼བ೒ 2.11 ᠔⼎ⱘᇍ䆱ḚЁऩߏĀ䗝ᢽāᣝ䪂ˈᇚߎ⦄Ā䇁㿔੠ᄫヺ㓪ⷕāᇍ䆱Ḛˈ ḚˈऩߏĀ催㑻āᣝ䪂ˈᇚⳟࠄབ೒ 2.11 ᠔⼎ⱘ⬠䴶DŽ ߏ FireFox ⌣㾜఼㦰ऩᷣЁⱘĀᎹ݋ā㦰ऩˈ䗝ᢽĀ䗝乍ā㦰ऩ乍ˈᇚߎ⦄Ā䗝乍āᇍ䆱 FireFox ⱘ䇁㿔⦃๗ᑊϡফ Windows ㋏㒳ⱘ᥻ࠊDŽЎњ䅽 FireFox гՓ⫼㕢೑㣅䇁⦃๗ˈऩ བᵰ៥ӀՓ⫼ FireFox ⌣㾜఼ᴹ⌣㾜䆹义䴶ᯊˈথ⦄ձ✊ᰒ⼎Ё᭛⬠䴶üü䖭ᰃ಴Ў ೒ 2.9 䆒㕂䇁㿔/ऎඳ䗝乍 ೒ 2.10 ⿟ᑣ೑䰙࣪ⱘᬜᵰ ݡ⃵৥᳡ࡵ఼䇋∖ login.jsp 义䴶ˈᇚⳟࠄབ೒ 2.10 ᠔⼎ⱘ义䴶DŽ ๗Ў㕢೑㣅䇁DŽ བᵰ៥Ӏ䗝ᢽĀ㣅䇁˄㕢೑˅ā䗝乍ˈ✊ৢऩߏĀ⹂ᅮāᣝ䪂ˈᇚ䆒㕂ᴀഄⱘ䇁㿔⦃ ᥻ࠊ䴶ᵓЁऩߏĀऎඳ੠䇁㿔䗝乍āˈ䖯ܹབ೒ 2.9 ᠔⼎ⱘᇍ䆱ḚDŽ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 45 this.password = password; { public void setPassword(String password) //password ሲᗻᇍᑨⱘ setter ᮍ⊩ } return password; { public String getPassword() //password ሲᗻᇍᑨⱘ getter ᮍ⊩ } this.username = username; { public void setUsername(String username) //username ሲᗻᇍᑨⱘ setter ᮍ⊩ } return username; { public String getUsername() //username ሲᗻᇍᑨⱘ getter ᮍ⊩ private String password; private String username; //ϟ䴶ᰃ Action ݙ⫼Ѣᇕ㺙⫼᠋䇋∖খ᭄ⱘϸϾሲᗻ { public class LoginAction //Struts 2 ⱘ Action ㉏ህᰃϔϾ᱂䗮ⱘ Java ㉏ ᮍ⊩DŽׂᬍৢⱘ LoginAction ㉏ҷⷕབϟ˖ Ўњ䅽Struts 2 ๲ࡴ䕧᭄ܹ᥂᷵偠ⱘࡳ㛑ˈᬍݭ⿟ᑣЁⱘLoginActionˈ๲ࡴ䞡ݭvalidate 䋷䕧ߎDŽ ⦄䫭䇃ˈ߭ᇚ䫭䇃⏏ࡴࠄ ActionSupport ㉏ⱘ fieldErrors ඳЁˈ✊ৢ䗮䖛 OGNL 㸼䖒ᓣ䋳 ೼ Validatable ᥹ষЁᅮНњϔϾ validate()ᮍ⊩ˈ䞡ݭ䆹ᮍ⊩ˈབᵰ᷵偠㸼ऩ䕧ܹඳߎ ⱘ Action ᓔথDŽ Validateable ᥹ষˈᦤկњ᭄᥂᷵偠ࡳ㛑DŽ䗮䖛㒻ᡓ䆹 ActionSupport ㉏ˈৃҹㅔ࣪ Struts 2 ActionSupport ㉏ᰃϔϾᎹ݋㉏ˈᅗᏆ㒣ᅲ⦄њ Action ᥹ষDŽ䰸ℸП໪ˈᅗ䖬ᅲ⦄њ 2.8.1 㒻ᡓ ActionSupport ᴀⱘ᭄᥂᷵偠DŽ ᠻ㸠෎ܜˈⱘ䕧ܹᓩ䍋ᑩሖᓖᐌˈ䗮ᐌ៥ӀӮ೼䖯㸠Ϯࡵ䘏䕥᪡԰Пࠡ᠋⫼ܡЎњ䙓 䍋ᓖᐌDŽ 䳔㽕ֱᄬࠄ᭄᥂ᑧˈ៪㗙䳔㽕ḍ᥂⫼᠋䕧ܹⱘ⫼᠋ৡǃᆚⷕᶹ䆶᭄᥂ˈ䖭ѯぎ䕧ܹৃ㛑ᓩ ϡӮᓩ䍋໾໻ⱘ䯂乬DŽԚབᵰ᭄᥂މᭈϾ HelloWorld ᑨ⫼Ёˈ䖭⾡ぎ⫼᠋ৡǃぎᆚⷕⱘᚙ ೼Ϟ䴶ᑨ⫼ЁˈेՓ⌣㾜㗙䕧ܹӏԩ⫼᠋ৡǃᆚⷕˈ㋏㒳гӮ໘⧚⫼᠋䇋∖DŽ೼៥Ӏ 2.8 ๲ࡴ᭄᥂᷵偠 ݡ⃵Փ⫼ FireFox ⌣㾜఼⌣㾜 login.jsp 义䴶ˈᇚⳟࠄ䆹义䴶ব៤њ㣅᭛⬠䴶DŽ ㄀ 2 ゴ Struts 2 ϟⱘ HelloWorld 46 /welcome.jsp /error.jsp /login.jsp ሲᗻDŽϟ䴶ᰃׂᬍৢ login Action ⱘ䜡㕂⠛↉˖ Ўњ೼᷵偠༅䋹ৢˈ㋏㒳㛑ᇚ㾚೒䕀ܹ input ໘ˈᖙ乏೼䜡㕂䆹 Action ᯊ䜡㕂 input ࠄ input 䘏䕥㾚೒໘DŽ བᵰᠻ㸠䆹ᮍ⊩ПৢˈAction ㉏ⱘ fieldErrors ЁᏆ㒣ࣙ৿њ᭄᥂᷵偠䫭䇃ˈ䇋∖ᇚ㹿䕀থ Ϟ䴶ⱘ Action ㉏䞡ݭњ validate ᮍ⊩ˈ䆹ᮍ⊩Ӯ೼ᠻ㸠㋏㒳ⱘ execute ᮍ⊩Пࠡᠻ㸠ˈ } } } addFieldError("password", "pass.required"); { if (getPassword() == null || getPassword().trim().equals("")) //ᔧᆚⷕЎぎˈ៪㗙ᆚⷕЎぎᄫヺІᯊˈ⏏ࡴ㸼ऩ᷵偠䫭䇃 } addFieldError("username", "user.required"); //⏏ࡴ㸼ऩ᷵偠䫭䇃 { if (getUsername() == null || getUsername().trim().equals("")) //བᵰ⫼᠋ৡЎぎˈ៪㗙⫼᠋ৡЎぎᄫヺІ { public void validate() //ᅠ៤䕧ܹ᷵偠䳔㽕䞡ݭⱘ validate ᮍ⊩ } } return "error"; { else } return "success"; { && getPassword().equals("tiger") ) if (getUsername().equals("scott") //৺߭䖨ಲ error ⱘᄫヺІ ᄫヺІ //ᔧ⫼᠋䇋∖খ᭄ⱘ username ㄝѢ scottˈᆚⷕ䇋∖খ᭄Ў tiger ᯊˈ䖨ಲ success { public String execute() throws Exception //໘⧚⫼᠋䇋∖ⱘ execute ᮍ⊩ } Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 47 } } addFieldError("password", getText("pass.required")); { if (getPassword() == null || getPassword().trim().equals("")) } addFieldError("username", getText("user.required")); //⏏ࡴ᷵偠䫭䇃ᦤ⼎ˈՓ⫼ getText ᮍ⊩ᴹՓᦤ⼎ֵᙃ೑䰙࣪ { if (getUsername() == null || getUsername().trim().equals("")) //བᵰ⫼᠋ৡЎぎˈ៪㗙ЎぎᄫヺІ { public void validate() //ᠻ㸠᭄᥂᷵偠ⱘ validate ᮍ⊩ ⃵ᬍݭ Action 䞠ⱘ validate ᮍ⊩ˈᬍݭৢⱘ validate ᮍ⊩ҷⷕབϟ˖ key˅ᮍ⊩ˈ䆹ᮍ⊩⫼ѢҢ䌘⑤᭛ӊЁ㦋প೑䰙ֵ࣪ᙃDŽЎњ䅽᷵偠ֵᙃᬃᣕ೑䰙࣪ˈݡ ᅲ䰙ϞˈActionSupport ㉏Ꮖ㒣ᦤկњ೑䰙ֵ࣪ᙃⱘ㛑࡯ˈᅗᦤկњϔϾ getText˄String ⅏њüü䖭ህ༅এњ೑䰙࣪ⱘ㛑࡯DŽ 䞡ݭ validate ᮍ⊩ᯊˈབᵰথ⫳᷵偠༅䋹ⱘ䯂乬ˈ᷵偠䫭䇃ⱘᦤ⼎ֵᙃᰃҹ⹀㓪ⷕᮍᓣݭ ԚϞ䴶ⱘ⿟ᑣ䖬ᄬ೼ϔϾ䯂乬˖᷵偠ֵᙃⱘ೑䰙࣪DŽᶹⳟϞ䴶ⱘ Action ㉏ҷⷕথ⦄˖ ㌑䡻 Struts 2 ⱘ咬䅸Ꮖ㒣ᦤկњ䕧ߎ᷵偠䫭䇃ⱘ㛑࡯DŽ 㗠ᰃ⫼њᷛㅒˈStruts 2 ⱘᷛㅒᏆ㒣݋໛њ䕧ߎ᷵偠䫭䇃ⱘ㛑࡯DŽ 偠ֵᙃüü䖭ᰃ಴Ў Struts 2 ⱘᷛㅒˈϞ䴶ⱘ JSP 义䴶Ё㸼ऩՓ⫼ⱘᑊϡᰃ HTML 㸼ऩˈ ܹ义䴶䕧ߎ䖭ѯ᷵偠䫭䇃ֵᙃˈԚ೒ 2.13 ᠔⼎ⱘ义䴶ˈैৃҹⳟࠄ义䴶Ꮖ㒣䕧ߎњ䖭ѯ᷵ ⳟࠄ䖭䞠г䆌䇏㗙㾝ᕫ䴲ᐌ⼲༛˖៥Ӏҙҙ೼ Action ⏏ࡴњ᭄᥂᷵偠䫭䇃ˈᑊ᳾೼䕧 ೒ 2.13 䕧ܹ᷵偠ⱘ⬠䴶 ᔩ ᠔⼎ⱘ⬠䴶DŽ 䕀থ㒭 input 䘏䕥㾚೒DŽབᵰ៥Ӏϡ䕧ܹ⫼᠋ৡǃᆚⷕ㗠Ⳉ᥹ᦤѸ㸼ऩˈᇚⳟࠄབ೒ 2.13 ᮍ⊩໘⧚ˈབᵰ䆹ᮍ⊩໘⧚㒧ᴳৢˈAction ⱘ fieldErrors 䞠ⱘ᷵偠䫭䇃ϡЎぎˈ䇋∖ᇚ㹿 Ӯ㹿 validateܜˈࠡ䴶Ꮖ㒣ᦤࠄ˖ᔧ⫼᠋ᦤѸ䇋∖ᯊˈ䇋∖ᕫࠄ execute ᮍ⊩໘⧚Пࠡ input 䘏䕥㾚೒ⱘ䜡㕂ˈ䆹䘏䕥㾚೒᯴ᇘࠄ login.jsp 义䴶DŽ ᇍ↨Ϟ䴶ⱘ Action 䜡㕂Ϣࠡ䴶ⱘ Action 䜡㕂ˈ៥Ӏথ⦄䆹 Action 䜡㕂⠛↉Ё๲ࡴњ ㄀ 2 ゴ Struts 2 ϟⱘ HelloWorld 48 ㋴ -->ܗ ϟ䴶ᰃ᷵偠㾘߭ⱘᅮН᭛ӊ˖ ऩඳᖙ乏䕧ܹDŽ ᴀᑨ⫼Џ㽕Փ⫼њ requiredstring ᷵偠఼ˈ䆹᷵偠఼ᰃϔϾᖙ฿᷵偠఼üüᣛᅮᶤϾ㸼 ⱘ᭄᥂᷵偠఼ˈࣙᣀ㸼ऩඳ᷵偠఼੠䴲㸼ऩඳ᷵偠఼ϸ⾡DŽ ᷵偠䜡㕂᭛ӊ䗮䖛Փ⫼ Struts 2 Ꮖ᳝ⱘ᷵偠఼ˈᅠ៤ᇍ㸼ऩඳⱘ᷵偠DŽStruts 2 ᦤկњ໻䞣 ϟ䴶䖬ᰃՓ⫼ॳᴹⱘ Action ㉏˄ेϡ䞡ݭ validate ᮍ⊩˅ˈै๲ࡴϔϾ᷵偠䜡㕂᭛ӊˈ ᶊᅲ䰙Ϟᰃ෎Ѣ XWork ⱘ validator ḚᶊDŽ 䆌䗮䖛ᅮН䜡㕂᭛ӊᴹᅠ៤᭄᥂᷵偠DŽStruts 2 ⱘ᷵偠Ḛܕ㉏ԐѢ Struts 1ˈStruts 2 г 䫭ˈԚ䳔㽕໻䞣䞡ݭⱘ validate ᮍ⊩üü↩コˈ䞡໡кݭⳌৠⱘҷⷕϡᰃϔӊ਌ᓩҎⱘџᚙDŽ Ϟ䴶ⱘ䕧ܹ᷵偠ᰃ䗮䖛䞡ݭ ActionSupport ㉏ⱘ validate ᮍ⊩ᅲ⦄ⱘˈ䖭⾡ᮍ⊩㱑✊ϡ 2.8.2 Փ⫼ Struts 2 ⱘ᷵偠Ḛᶊ ೒ 2.14 ೑䰙᭄࣪᥂᷵偠ⱘ䫭䇃ᦤ⼎ ᔩ ℸᯊˈབᵰ≵᳝ӏԩ䕧ߎˈⳈ᥹ᦤѸⱏᔩ㸼ऩˈᇚⳟࠄབ೒ 2.14 ᠔⼎ⱘ⬠䴶DŽ ⱘ getText ᮍ⊩ৃҹḍ᥂䌘⑤᭛ӊࡴ䕑㦋ᕫ೑䰙࣪ᦤ⼎ֵᙃDŽ ㌑䡻 ActionSupport ๲ࡴњ䅽ᦤ⼎ֵᙃ೑䰙࣪ⱘ㛑࡯ˈActionSupport ᦤկ ᴵᦤ⼎ֵᙃDŽ њϸϾ೑䰙࣪ᦤ⼎˖user.required ੠ pass.requiredˈ಴ℸᑨ䆹೼೑䰙࣪䌘⑤᭛ӊЁ⏏ࡴ䖭ϸ Іˈ㗠ᰃ䇗⫼њ getText ᮍ⊩ᴹ㦋প䫭䇃ᦤ⼎DŽ಴Ў೼ Action ЁˈՓ⫼ getText ᮍ⊩ᴹ㦋প ೼Ϟ䴶ⱘ validate ᮍ⊩Ёˈ⏏ࡴ᷵偠䫭䇃ᦤ⼎ᯊˈᑊϡᰃⳈ᥹㒭ߎњ䫭䇃ᦤ⼎ⱘᄫヺ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 49 Ͼ໻㟈ⱘᥠᦵDŽ ᭄᥂᷵偠ǃ⿟ᑣ೑䰙࣪ㄝᐌ⫼ࡳ㛑DŽ䗮䖛䯙䇏ᴀゴⱘݙᆍˈ䇏㗙ᑨ䆹ᇍ Struts 2 Ḛᶊ᳝ϔ ⢊ᗕˈᇚ Action ໘⧚㒧ᵰӴಲ JSP 义䴶ᰒ⼎ˈᴀᑨ⫼г㓐ড়ᑨ⫼њ Struts 2 ⱘᷛㅒᑧǃ Struts 2 ᑨ⫼෎⸔Ϟˈҟ㒡њϔѯ Struts 2 ⱘ⏅ܹᑨ⫼ˈࣙᣀ೼ Action Ё䆓䯂 HttpSession ㉏෎ᴀ⌕⿟᥻ࠊ䆆䍋ˈ䆺㒚ҟ㒡њབԩᓔথϔϾ Struts 2 ᑨ⫼DŽᴀゴⱘৢ䴶䚼ߚ೼෎ᴀ ᴀゴҹϔϾ HelloWorld ᑨ⫼Ў՟ˈㅔ㽕ҟ㒡њ Struts 2 MVC Ḛᶊⱘ෎ᴀ⌕⿟ˈҢ Action 2.9 ᴀゴᇣ㒧 ৃ㓈ᡸᗻDŽ ᮍᓣϟˈ᮴䳔кݭ᷵偠ҷⷕˈা䳔㽕䗮䖛䜡㕂᭛ӊᣛᅮ᷵偠㾘߭ेৃˈ಴ℸᦤկњ᳈དⱘ བᵰϡ䕧ܹ⫼᠋ৡǃᆚⷕ㗠ᦤѸ㸼ऩˈᇚݡ⃵ⳟࠄབ೒ 2.14 ᠔⼎ⱘ⬠䴶DŽ೼䖭⾡᷵偠 䘏䕥㾚೒᯴ᇘࠄ login.jsp 义䴶DŽ ᔧ✊ˈ೼ struts.xml ᭛ӊⱘ Action ᅮНЁˈϔḋ䳔㽕ᅮН input ⱘ䘏䕥㾚೒ৡˈᇚ input WEB-INF/classes/lee 䏃ᕘϟेৃDŽ Action ㉏ⱘ class ᭛ӊԡѢৠϔϾ䏃ᕘϟDŽ಴ℸˈᇚϞ䴶ⱘ᷵偠㾘߭᭛ӊᬒ೼ ಴ℸϞ䴶ⱘ᷵偠㾘߭᭛ӊᑨ䆹ੑৡЎĀLoginAction-validation.xmlāˈϨ䆹᭛ӊᑨ䆹Ϣ ActionName-validation.xml˖݊Ё ActionName ህᰃ䳔㽕᷵偠ⱘ Action ⱘ㉏ৡDŽ ᅮНᅠ䆹᷵偠㾘߭᭛ӊৢˈ䆹᭛ӊⱘੑৡᑨ䆹䙉ᅜབϟ㾘߭˖ ㄀ 2 ゴ Struts 2 ϟⱘ HelloWorld ㄀ゴ Struts 2 ෎⸔ ᴀゴ㽕⚍  Struts 1 Ḛᶊⱘ෎ᴀⶹ䆚  Փ⫼ Struts 1 Ḛᶊᓔথ Web ᑨ⫼  WebWork Ḛᶊⱘ෎ᴀⶹ䆚  Փ⫼ WebWork Ḛᶊᓔথ Web ᑨ⫼  ೼ Eclipse Ёᭈড় Tomcat  Փ⫼ Eclipse ᓔথ Web ᑨ⫼  Ў Web ᑨ⫼๲ࡴ Struts 2 ᬃᣕ  Struts 2 Ḛᶊⱘ MVC 㒘ӊ  Struts 2 Ḛᶊⱘ⌕⿟  䗮䖛 web.xml ᭛ӊࡴ䕑 Struts 2 Ḛᶊ  䗮䖛 struts.properties ᭛ӊ䜡㕂 Struts 2 ሲᗻ  struts.xml ᭛ӊⱘ㒧ᵘ  ࠡ䴶ϔゴᏆ㒣໻㟈ҟ㒡њ Struts 2 ᑨ⫼ⱘ෎ᴀᚙމˈ䗮䖛ࠡϔゴⱘᄺдˈ䇏㗙ᑨ䆹 ᇍ Struts 2 Ḛᶊ᳝њϔϾ໻㟈ⱘᥠᦵDŽԚᇍѢ৘ⶹ䆚⚍ⱘ㒚㡖ˈ䖬䳔㽕䖯ϔℹᄺдˈ ᴀゴᇚ䆺㒚ҟ㒡 Struts 2 Ḛᶊⱘ෎⸔䚼ߚDŽ ಴Ў Struts 2 ᰃ೼ WebWork ੠ Struts 1 ⱘ෎⸔Ϟথሩ䍋ᴹⱘˈ಴ℸᥠᦵϔᅮⱘ WebWork ੠ Struts 1 Ḛᶊⱘⶹ䆚ˈᇚᇍѢᥠᦵ Struts 2 Ḛᶊ໻᳝㺼ⲞDŽ⡍߿ᰃ WebWork ᮍ䴶ⱘᓔথ 㒣偠ˈ㒱໻䚼ߚ䛑ৃҹ䗖⫼Ѣ Struts 2 ᑨ⫼ⱘᓔথDŽᴀゴᇚӮㅔ㽕ҟ㒡 Struts 1 ੠ WebWork ḚᶊⱘՓ⫼DŽ ᴀゴ䖬ᇚҟ㒡೼ Eclipse IDE Ꮉ݋Ёᓔথ Struts 2 ᑨ⫼ˈᑊϨ䆺㒚ҟ㒡 Struts 2 䜡㕂᭛ ӊⱘ৘Ͼ㒚㡖DŽ 3.1 Struts 1 ⱘ MVC ᅲ⦄ Struts 1 ᰃϪ⬠Ϟ᳔Āস㗕āⱘ MVC Ḛᶊˈᅗߎ⦄Ѣ 2001 ᑈDŽStruts 1 ҹ ActionServlet ԰ЎḌᖗ᥻ࠊ఼ˈ⬅ ActionServlet 䋳䋷ᢺ៾⫼᠋ⱘ᠔᳝䇋∖DŽStruts 1 Ḛᶊ᳝ 3 Ͼ䞡㽕㒘៤ 䚼ߚ˖ActionǃActionForm ੠ ActionForward ᇍ䈵DŽ 51
    • ${requestScope.err}

      <bean:message key="login"/> <%@include file="taglibs.jsp"%> <%@ page language="java" contentType="text/html;charset=gb2312" errorPage=""%> ⱘҷⷕབϟ˖ ϟ䴶ᰃϔϾㅔऩⱘⱏᔩ义䴶ˈࣙ৿ϸϾ㸼ऩඳˈߚ߿ҷ㸼⫼᠋ৡ੠ᆚⷕDŽ䆹ⱏᔩ义䴶 getter ᮍ⊩üüԚ ActionForm ⱘ䆒䅵ϡᰃ POJOˈᅗᖙ乏㒻ᡓ Struts 1 ⱘ ActionForm ෎㉏DŽ ᅗ䳔㽕Ў⫼᠋ⱘ↣Ͼ䇋∖খ᭄䛑ᦤկϔϾᇍᑨⱘሲᗻˈᑊЎ䆹ሲᗻ䆒㕂Ⳍᑨⱘ setter ੠ ⫼ऩ⣀ⱘ ActionForm ᴹᇕ㺙᠔᳝ⱘ䇋∖খ᭄DŽҢ㒧ᵘϞⳟˈActionForm ህᰃϔϾ POJO˖ Struts 1 Փ⫼ऩ⣀ⱘ ActionForm ᇕ㺙䇋∖খ᭄ˈᔧ⫼᠋ᦤѸϔϾ䇋∖ᯊˈStruts 1 ᇚՓ 3.1.2 ᅲ⦄ ActionForm Ўњ೼㓪䆥⿟ᑣᯊৃҹᇐܹ Struts 1 ㉏ᑧˈ䖬ᑨᇚ struts.jar ᭛ӊ๲ࡴࠄ⦃๗ব䞣ЁDŽ ˄㸼䖒ᓣ˅䇁㿔ˈ䖬ᑨᇚ contrib ϟⱘ JAR ᭛ӊ໡ࠊࠄ WEB-INF/lib ϟDŽ WEB-INF ϟˈݡᇚ validator-rules.xml ᭛ӊг໡ࠊࠄ WEB-INF ϟDŽབᵰ䳔㽕⫼ࠄ Struts-EL ᇚ lib ϟⱘ JAR ᭛ӊܼ䚼໡ࠊࠄ WEB-INF/lib ϟˈᑊᇚ TLD ᭛ӊܼ䚼໡ࠊࠄ  ঺໪䖬ϔѯ݇Ѣ Struts ⱘ䇈ᯢ੠ LICENSE ㄝ᭛ḷ  webapps˖䆹䏃ᕘϟᄬᬒњ Struts 1 Ḛᶊⱘ⼎՟ᑨ⫼ঞ᭛ḷᑨ⫼DŽ  lib˖䆹䏃ᕘϟᄬᬒњ Struts 1 ḚᶊⱘḌᖗ㉏ᑧDŽ  contrib˖䆹䏃ᕘϟᄬᬒњ Struts 1 㸼䖒ᓣ䇁㿔ᬃᣕⱘ㉏ᑧ੠ᷛㅒᑧ᭛ӊDŽ य़㓽ৢˈথ⦄᳝བϟ᭛ӊ㒧ᵘ˖ ⱏᔩ struts.apache.org キ⚍ˈϟ䕑 Struts ⱘড়䗖⠜ᴀDŽ㾷य़㓽ϟ䕑ࠄⱘय़㓽ࣙˈ㾷 ੠ᅝ㺙 Struts 1 Ḛᶊ䇋ᣝབϟℹ偸䖯㸠DŽ ᴀ㡖ⱘ⼎՟⿟ᑣ෎Ѣ Struts 1 ⱘ 1.2.9 ⠜ˈ䇋䇏㗙гϟ䕑䆹⠜ᴀⱘ Struts 1 Ḛᶊˈϟ䕑 3.1.1 ϟ䕑੠ᅝ㺙 Struts 1 Ḛᶊ ㄀ 3 ゴ Struts 2 ෎⸔ 52 䅽ܡᇍѢ Struts 1 㗠㿔ˈActionForm ㉏ᰃϔϾ᮶⚺⧤জ≵᳝ᡔᴃ৿䞣ⱘ㉏ˈݭ䍋ᴹ䲒 ಴Ў䆹 ActionForm 㒻ᡓњ ValidatorFormˈҢ㗠ᇐ㟈њ䆹㉏ⱘ∵ᶧˈ䰡Ԣњ䆹㉏ⱘҷⷕ໡⫼DŽ Ϟ䴶 ActionForm ㉏ⱘҷⷕ䴲ᐌㅔऩˈ䰸њ䆹㉏䳔㽕㒻ᡓ ValidatorForm П໪DŽгℷᰃ } } this.pass = pass; { public void setPass(String pass) } return pass; { public String getPass() } this.username = username; { public void setUsername(String username) } return username; { public String getUsername() //ҹϟᰃሲᗻᄫ↉ⱘ㋏߫ getǃset ᮍ⊩ private String pass; private String username; //ϟ䴶ϸϾሲᗻ⫼Ѣᇕ㺙ϸϾ㸼ऩඳⱘ䇋∖খ᭄ { public class LoginForm extends ValidatorForm ೒ 3.1 ㋏㒳ⱏᔩⱘ义䴶 ᔩ ϟ䴶ᰃᴀᑨ⫼Ё ActionForm ㉏ⱘҷⷕ˖ ಴ℸᴀᑨ⫼Ёⱘ ActionForm 㒻ᡓњ ValidatorForm ㉏DŽ ៪㗙ᰃᅗⱘᄤ㉏DŽ಴Ўᴀᑨ⫼Փ⫼њ Struts ⱘ᷵偠Ḛᶊˈ getter ᮍ⊩˅ˈԚ䆹 ActionFrom 䳔㽕㒻ᡓ ActionFormˈ ԐϔϾ POJO˄ᅮН޴ϾሲᗻˈЎ↣Ͼሲᗻᦤկ setter ੠ খ᭄ᰃ䗮䖛 ActionForm ᴹᇕ㺙ⱘˈActionForm 䴲ᐌ㉏ ৥᳡ࡵ఼থ䗕ⱘ䇋∖খ᭄ˈᇍѢ Strurts ᑨ⫼㗠㿔ˈ䇋∖ 䆹义䴶Ё᳝ϸϾ㸼ऩඳˈ䖭ϸϾ㸼ऩඳᇕ㺙њ䳔㽕 ㅔऩⱘⱏᔩ㸼ऩDŽ䆹义䴶ⱘ⬠䴶བ೒ 3.1 ᠔⼎DŽ ೼⌣㾜఼Ё⌣㾜䆹义䴶ᯊˈⳟࠄ䆹义䴶ࣙ৿њϔϾ


      Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 53 ActionForwardˈ৺߭䖨ಲ error ⱘ ActionForwardDŽ ∖খ᭄ⱘ⫼᠋ৡ੠ᆚⷕᰃ৺Ў scott ੠ tigerˈབᵰ⫼᠋ৡ੠ᆚⷕℷ⹂ˈ߭䖨ಲ welcome ⱘ Ϟ䴶ⱘ Action ҷⷕ䴲ᐌㅔऩˈ⫮㟇≵᳝䇗⫼ӏԩϮࡵ䘏䕥㒘ӊˈাᰃⳈ᥹߸ᮁ⫼᠋䇋 } } } return mapping.findForward("error"); { else } return mapping.findForward("welcome"); { if (username != null && username.equals("scott")) //໘⧚⫼᠋䇋∖ String pass = loginForm.getPass(); String username = loginForm.getUsername(); //Ң ActionForm Ёপߎ⫼᠋䇋∖খ᭄ LoginForm loginForm = (LoginForm)form; //㦋পᇕ㺙⫼᠋䇋∖খ᭄ⱘ ActionForm ᅲ՟ { Exception HttpServletRequest request, HttpServletResponse response)throws public ActionForward execute(ActionMapping mapping, ActionForm form, //ᖙ乏䞡ݭ䆹 execute ᮍ⊩ˈ䆹ᮍ⊩⫼Ѣ໘⧚⫼᠋䇋∖ { public class LoginAction extends Action ϟ䴶ᰃ㋏㒳ⱘ Action ໘⧚㉏ҷⷕDŽ ∖ˈᑊḍ᥂໘⧚㒧ᵰˈ䇗⫼ϡৠⱘ㾚೒义䴶ᴹਜ⦄໘⧚㒧ᵰDŽ Action ᅲ՟Ң ActionForm Ёপߎ⫼᠋䇋∖খ᭄ˈ✊ৢ䇗⫼Ϯࡵ䘏䕥㒘ӊ໘⧚⫼᠋䇋 Action ᅲ՟DŽ থ⫼᠋䇋∖ᯊˈӮᇚ䇋∖খ᭄ᇕ㺙៤ ActionForm ᅲ՟ˈᑊᇚ䆹 ActionForm ᅲ՟䕀থ㒭 ActionServlet ᢺ៾ࠄ⫼᠋䇋∖ˈᇚ䇋∖䕀থࠄ㋏㒳ⱘϮࡵ᥻ࠊ఼໘⧚DŽActionServlet ೼䕀 Action ህᰃ⫼Ѣ໘⧚⫼᠋䇋∖ⱘϮࡵ᥻ࠊ఼ˈᔧ⫼᠋䇋∖থ䗕ࠄ ActionServlet ৢˈ 3.1.3 ᅲ⦄ Action 䆌䗮䖛䜡㕂᭛ӊᴹᅮН ActionFormDŽܕՓ⫼ࡼᗕ FormBeanˈҢ㗠 䗮ⱘ JavaBeanˈԚᖙ乏㒻ᡓ ActionForm ㉏DŽ䰸ℸП໪ˈActionForm 䖬ৃҹ 㰩ぐ Struts 1 Ёⱘ ActionForm ㉏ᰃϔϾ䴲ᐌㅔऩⱘ㉏ˈᅗᅲ䋼ϞᰃϔϾ᱂ кݭ ActionForm ㉏DŽܡҎৃҹ䙓 Ԛ Struts 1 Ḛᶊгᛳ㾝ࠄ໻䞣䞡໡кݭ䆹㉏ᰃϔϾ䋳ᢙˈৢᴹᦤկњࡼᗕ ActionFormˈ䅽 ⿟ᑣਬ㾝ᕫᛣ݈㋶✊DŽ㱑✊᳝ᕜ໮ IDE˄䲚៤ᓔথ⦃๗˅ৃҹ㞾ࡼ⫳៤䆹 ActionForm ㉏ˈ ㄀ 3 ゴ Struts 2 ෎⸔ 54 validate="true" scope="request" input="/login.jsp"> ܗ ㋴ЁᅮН -->ܗ ܗ ϟ䴶ᰃᴀᑨ⫼ⱘ struts-config.xml ᭛ӊҷⷕDŽ ༅䋹ৢ䕀ܹⱘ㾚೒䌘⑤DŽ ࡴ validate ሲᗻˈᑊᇚ䆹ሲᗻؐ䆒㕂៤ true˗䖬䳔㽕๲ࡴ input ሲᗻˈ䆹ሲᗻᣛ৥䕧ܹ᷵偠 ㋴ᯊˈ䖬ᑨ䆹๲ܗ 4 minlength position="1"/> ^\w+$ mask
      ㋴ -->ܗ ϟ䴶ᰃ᷵偠㾘߭᭛ӊⱘҷⷕDŽ ᭛ӊᰃᓔথ㗙ᅮНⱘ㾘߭᭛ӊˈ䆹᭛ӊᅮНњ䳔㽕᷵偠ⱘ㸼ऩᑨ䆹⒵䎇ⱘ㾘߭DŽ 㾘߭᭛ӊˈ݊Ё validator-rules.xml ᭛ӊᰃϔϾ㋏㒳㾘߭᭛ӊˈ䗮ᐌ᮴䳔ׂᬍ˗validation.xml ᴀᑨ⫼Փ⫼њ Struts 1 ⱘ᷵偠Ḛᶊᴹᅠ៤䕧ܹ᷵偠ˈ಴ℸ೼ᴀᑨ⫼Ёࡴ䕑њϸϾ᷵偠 ㋴ᣛᅮDŽܗ
      /WEB-INF/validation.xml" /> <%@include file="taglibs.jsp"%> <%@ page contentType="text/html;charset=gb2312" errorPage="error.jsp"%> 䴶ⱘҷⷕDŽ ࠄ೑䰙࣪ⱘ⃶䖢ֵᙃˈᑊϨ䗮䖛 Struts 1 ⱘᷛㅒ䕧ߎⱏᔩⱘ⫼᠋ৡDŽϟ䴶ᰃ welcome.jsp 义 བᵰ⫼᠋䕧ܹⱘ⫼᠋ৡℷ⹂ǃᆚⷕℷ⹂ˈ⫼᠋ⱏᔩ៤ࡳˈৃҹ೼ welcome.jsp 义䴶㾕 ೒ 3.2 ㋏㒳乎ᑣ೒ ೒ 3.2 ᰒ⼎њ䆹ᑨ⫼ⱘ乎ᑣ೒DŽ welcome.jsp 义䴶˗བᵰ໘⧚㒧ᵰЎ error ⱘ ActionForwardˈ߭䖯ܹ error.jsp 义䴶DŽ Action ໘⧚⫼᠋䇋∖খ᭄ˈབᵰ໘⧚㒧ᵰЎ success ⱘ ActionForwardˈ߭䖯ܹ 䇋∖䕀থࠄ login.jsp 义䴶ˈ৺߭䇋∖ᇚ㹿䕀থࠄϮࡵ䘏䕥᥻ࠊ఼ Action ໘DŽ 义䴶ˈ⫼᠋䕧ܹ䇋∖ֵᙃˈথ䗕䇋∖DŽབᵰ⫼᠋ⱘ䇋∖খ᭄ϡ㛑䗮䖛䕧ܹ᷵偠ˈ߭㋏㒳ᇚ 㒣䖛Ϟ䴶ℹ偸ˈৃҹ䴲ᐌ⏙ἮഄⳟߎϞ䴶⼎՟ᑨ⫼ⱘ⌕⿟ˈ⫼᠋䖯ܹ㋏㒳ⱘ login.jsp 3.1.5 ᅠ៤ᑨ⫼⌕⿟ ⼎ֵᙃDŽ ㌑䡻 Փ⫼䕧ܹ᷵偠ᯊˈ䗮ᐌᓎ䆂Փ⫼೑䰙࣪䌘⑤᭛ӊᴹֱᄬ᷵偠༅䋹ⱘᦤ Փ⫼೑䰙࣪䌘⑤᭛ӊᴹֱᄬ᷵偠ᦤ⼎ֵᙃDŽ 䕧ܹ᷵偠ⱘᦤ⼎ֵᙃ䛑ֱᄬ೼೑䰙࣪䌘⑤᭛ӊЁˈ಴ℸՓ⫼䕧ܹ᷵偠ᯊˈ䗮ᐌ䛑ᓎ䆂 ㋴ᯊᣛᅮⱘ input ሲᗻ᠔ᇍᑨⱘ㾚೒䌘⑤DŽܗ

      ㄀ 3 ゴ Struts 2 ෎⸔ 58 this.user = user; { public void setUser(String user) } return user; { public String getUser() //user ሲᗻᇍᑨⱘ setter ੠ getter ᮍ⊩ private String tip; //བϟ䇋∖খ᭄⫼Ѣᇕ㺙 Action ໘⧚䇋∖ৢⱘড作 private String pass; private String user; //ϟ䴶ϸϾሲᗻ⫼Ѣᇕ㺙⫼᠋䇋∖খ᭄ { public class LoginAction extends ActionSupport ᴀᑨ⫼ⱘ Action ҷⷕབϟ˖ WebWork APIDŽ䖭Ͼ Action Ϣ Struts 1 ⱘ Action ϡৠˈᅗ䴲ᐌ㉏ԐѢϔϾ᱂䗮ⱘ POJODŽ 䆹 Action гࣙ৿ϔϾ execute ᮍ⊩ˈԚ䆹ᮍ⊩ϡݡࣙ৿ӏԩ Servlet APIˈгϡࣙ৿ӏԩ ⫼᠋䳔㽕ᅲ⦄ⱘ WebWork Ḍᖗ㒘ӊձ✊ᰃϮࡵ᥻ࠊ఼ˈϮࡵ᥻ࠊ఼ህᰃϔϾ Actionˈ 3.2.2 ᅲ⦄ Action ⱘ⦃๗ব䞣ЁDŽᔧ✊гৃҹՓ⫼ Ant Ꮉ݋ˈ߭᮴䳔๲ࡴ⦃๗ব䞣DŽ Ўњৃҹ㓪䆥 WebWork ᑨ⫼⿟ᑣˈᓎ䆂ᇚ webwork-2.2.5.jar ๲ࡴࠄ CLASSPATH Ё䳔㽕Փ⫼䖭ѯ㄀ϝᮍḚᶊᯊˈᠡ䳔㽕䖭ѯ㉏ᑧ᭛ӊDŽ 䚼 JAR ᭛ӊDŽ㟇Ѣ lib 䏃ᕘϟⱘ݊Ҫ JAR ᭛ӊˈ߭ᰃৃ䗝ⱘüüা᳝ᔧᑨ⫼ 㰩ぐ Փ⫼ WebWork Ḛᶊᖙ乏᳝ webwork-2.2.5.jar ੠ lib\default 䏃ᕘϟⱘܼ 䏃ᕘϟⱘ*.jar ᭛ӊܼ䚼໡ࠊࠄ WEB-INF/lib ϟDŽ ᇚ webwork-2.2.5.jar ᭛ӊ໡ࠊࠄ Web ᑨ⫼ⱘ WEB-INF/lib 䏃ᕘϟˈݡᇚ lib\default 㺙 WebWork ḚᶊDŽ WebWork Ḛᶊⱘᮍ⊩ˈϢ೼ Web ᑨ⫼Ё๲ࡴ݊ҪḚᶊⱘᮍ⊩ᅠܼϔḋDŽϟ䴶ҟ㒡བԩᅝ WebWork 䗮ᐌ䛑԰Ў Web ḚᶊՓ⫼ˈ೼ Web ᑨ⫼ЁՓ⫼䆹ḚᶊDŽЎ Web ᑨ⫼๲ࡴ  ℸ໪䖬᳝ϔѯ WebWork Ḛᶊⱘ㓪䆥㛮ᴀ᭛ӊঞ৘⾡䇈ᯢ᭛ӊDŽ  webwork-src-2.2.5.jar˖䆹य़㓽᭛ӊ䞠ࣙ৿њ WebWork Ḛᶊ᠔᳝⑤ҷⷕDŽ  webwork-2.2.5.jar˖䆹᭛ӊᰃ WebWork ḚᶊⱘḌᖗ㉏ᑧ᭛ӊDŽ ᵕདⱘ䌘᭭DŽ  webapps˖䆹䏃ᕘϟᄬᬒњ WebWork ⱘ⼎՟ᑨ⫼ˈ䖭ѯ⼎՟ᑨ⫼ᰃᄺд WebWork DŽއⱘ䯂乬ˈৃҹখ㗗䆹⑤᭛ӊ㾷 އ src˖䆹䏃ᕘϟᄬᬒњ WebWork ⱘ⑤᭛ӊˈབᵰՓ⫼ WebWork 䖛⿟Ё䘛ࠄ᮴⊩㾷 ⱘ᠔᳝ JAR ᭛ӊᰃ WebWork ᑨ⫼ᖙ䳔ⱘ㉏ᑧ᭛ӊDŽ  lib˖䆹䏃ᕘϟᄬᬒњ WebWork 㓪䆥੠䖤㸠᠔䳔ⱘ㄀ϝᮍ㉏ᑧˈ݊Ё default 䏃ᕘϟ ᭛ḷㄝDŽ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 59 ᳈֓᥋ഄᅲ⦄ Action ㉏ˈ՟བ⿟ᑣ೑䰙࣪DŽ ⱘҷⷕ∵ᶧˈWebWork ϡᔎࠊ㽕∖ Action ㉏㒻ᡓ ActionSupport ෎㉏ˈԚ㒻ᡓ䆹෎㉏ৃҹ ᴀᑨ⫼Ёⱘ Action 㒻ᡓњ WebWork ⱘ ActionSupport ෎㉏ˈ㒻ᡓ䆹෎㉏ᇐ㟈њ Action ActionForward ᇍ䈵DŽ ᄫヺІˈ㗠ϡᰃϔϾޚ WebWork ⱘ Action ⱘ execute ᮍ⊩䖨ಲⱘ䘏䕥㾚೒ᰃϔϾᷛ ⱘ໘⧚㒧ᵰDŽ  WebWork ⱘ Action ㉏䞠ࣙ৿ⱘሲᗻϡҙৃҹᇕ㺙⫼᠋䇋∖খ᭄ˈ䖬ৃҹᇕ㺙 Action ⫼᠋ⱘ䇋∖খ᭄DŽ  WebWork ⱘ Action ㉏ৃ᮴䳔 ActionForm ⱘᬃᣕˈᅗᴀ䑿ࣙ৿њϸϾሲᗻ⫼Ѣᇕ㺙 䆩DŽ⌟ܗ WebWork ⱘ Action ㉏᮴䳔Ϣ Servlet API 㗺ড়ˈҢ㗠ৃҹ᳈ᮍ֓ഄ䖯㸠ऩ Ϟ䴶 Action ㉏ⱘҷⷕϢ Struts 1 ⱘ Action ㉏ᄬ೼བϟ޴Ͼऎ߿˖ } } } return ERROR; setTip(getText("loginFail")); { else } return SUCCESS; setTip(getText("loginSuccess")); { && getPass().equals("tiger")) if (getUser().equals("scott") { public String execute() throws Exception //໘⧚⫼᠋䇋∖ⱘ execute ᮍ⊩ } this.tip = tip; { public void setTip(String tip) //tip ሲᗻᇍᑨⱘ setter ੠ getter ᮍ⊩ } return tip; { public String getTip() //tip ሲᗻᇍᑨⱘ setter ੠ getter ᮍ⊩ } this.pass = pass; { public void setPass(String pass) } return pass; { public String getPass() //pass ሲᗻᇍᑨⱘ setter ੠ getter ᮍ⊩ } ㄀ 3 ゴ Struts 2 ෎⸔ 60 ᷛㅒ䋳䋷⫳៤ϔϾৡЎ user ⱘ᭛ᴀ䕧ܹḚˈ䆹䕧ܹḚᇍᑨⱘ Label 䗮䖛%{getText('user')㸼 ᔧՓ⫼ᴹ⫳៤ϔϾ᭛ᴀḚᯊˈ䆹 ఼Ё⌣㾜䆹义䴶ⱘᬜᵰDŽ ⫮㟇᮴䳔Ў㸼ऩඳᣛᅮᇍᑨⱘ Label ֵᙃˈԚৃ⫳៤བϟ㸼ऩᅮНDŽ೒ 3.4 ᰒ⼎њ೼⌣㾜 ㋴DŽ಴ℸˈϞ䴶义䴶Ёⱘ㸼ऩᅮНЁˈܗWebWork ᷛㅒᑧˈৃҹ᳈ㅔऩⱘ⫳៤义䴶ⱘ㸼ऩ ࡽѢ׳ˈ䇏㗙ৃ㛑Ꮖ㒣⊼ᛣࠄˈϞ䴶ⱘ㸼ऩᅮНЁˈ໻䞣Փ⫼њ WebWork ⱘᷛㅒᑧ
      <ww:text name="loginTitle"/> <%@ taglib prefix="ww" uri="/webwork"%> <%@ page contentType="text/html;charset=GBK"%> 䖭ḋϸϾ㸼ऩඳDŽϟ䴶ᰃᇍᑨ JSP 义䴶ⱘҷⷕDŽ ಴ЎϞ䴶ⱘ Action ᇕ㺙њ user ੠ pass ϸϾ䇋∖খ᭄ˈᬙᇍᑨⱘ JSP 义䴶гᑨ䆹ࣙ৿ ᇕ㺙᭄᥂DŽ ῵ൟ偅ࡼ῵ᓣ᳈ᯧ⧚㾷˖Action ϧ䮼䋳䋷໘⧚⫼᠋䇋∖ˈ㗠῵ൟ߭ϧ䮼䋳䋷 ⱘ῵ᓣˈ῵ൟ偅ࡼ῵ᓣⱘ㓪⿟᳈ࡴ໡ᴖˈ಴Ў䳔㽕乱໪๲ࡴϔϾ῵ൟ㉏˗Ԛ POJOˈ᮴䳔㒻ᡓӏԩ WebWork ෎㉏DŽⳌᇍѢⳈ᥹Փ⫼ Action ᇕ㺙䇋∖খ᭄ ῵ൟЁᇕ㺙üü῵ൟህᰃϔϾ㉏ԐѢ ActionForm ⱘᇍ䈵ˈԚ䆹ᇍ䈵ᰃϔϾ খ᭄ˈгϡ䋳䋷ᇕ㺙 Action ⱘ໘⧚㒧ᵰDŽ㗠ᰃᇚ䇋∖খ᭄੠໘⧚㒧ᵰᬒ೼ ᭄DŽԚৃՓ⫼῵ൟ偅ࡼ῵ᓣˈ೼῵ൟ偅ࡼ῵ᓣϟˈAction ϡݡ䋳䋷ᇕ㺙䇋∖ 㰩ぐ WebWork ⱘ Action ㉏ϡᔎࠊ㽕∖Փ⫼ ActionForm ㉏ᇕ㺙⫼᠋䇋∖খ ActionSupport ෎㉏ⱘᮍ⊩DŽ Փ⫼ getText ᮍ⊩ˈ䆹ᮍ⊩ৃҹ䕧ߎᇍᑨ key ⱘ೑䰙ֵ࣪ᙃDŽ䆹 getText ᮍ⊩ℷᰃᴹ㞾Ѣ loginSuccess ੠ loginFailDŽЎњ೼ Action ㉏Ё䆓䯂䖭ϸϾ೑䰙ֵ࣪ᙃˈ䳔㽕೼ Action ㉏Ё Ϟ䴶ⱘ Action ᅲ⦄㉏Ёࣙ৿њϸϾ೑䰙ֵ࣪ᙃˈ䖭ϸϾ೑䰙ֵ࣪ᙃᇍᑨⱘ key ߚ߿Ў Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 61 ㋴ -->ܗ ᭛ӊˈ䆹᭛ӊЏ㽕䜡㕂ᑨ⫼ⱘ Actionˈҹঞ Action ᇍᑨⱘ URLDŽ ᑨ⫼ⱘ WEB-INF/classes 䏃ᕘϟⱘ xwork.xml ᭛ӊDŽxwork.xml ᭛ӊህᰃ WebWork ⱘ䜡㕂 ᣛᅮњϞ䴶ⱘ web.xml 䜡㕂᭛ӊৢˈWeb ᑨ⫼Ӯ㞾ࡼࡴ䕑 WebWork Ḛᶊˈᑊࡴ䕑 Web *.action webwork 2 com.opensymphony.webwork.dispatcher.ServletDispatcher webwork version="2.4"> com/xml/ns/j2ee/web-app_2_4.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ᭛ӊЁ䜡㕂 WebWork ⱘḌᖗ᥻ࠊ఼˖ServletDispatcherDŽׂᬍৢⱘ web.xml ᭛ӊབϟ˖ ಴Ў Web ᑨ⫼咬䅸াࡴ䕑 web.xml ᭛ӊˈ಴ℸЎњࡴ䕑 WebWork Ḛᶊˈᖙ乏೼ web.xml ೒ 3.4 ⱏᔩ义䴶ⱘᬜᵰ Ḛᶊህᰃ䜡㕂 WebWork ⱘḌᖗ᥻ࠊ఼ˈWebWork ⱘḌᖗ᥻ࠊ఼ᰃ ServletDispatcherDŽ ᑨ⫼ࡴ䕑 WebWork ḚᶊDŽ䅽 Web ᑨ⫼ࡴ䕑 WebWork 䜡㕂 Webܜ೼䜡㕂 WebWork ⱘ Action Пࠡˈ䳔㽕 ActionDŽ ActionDŽWebWork Փ⫼ϔϾ xwork.xml ᭛ӊᴹ䜡㕂 ᅲ⦄Ϟ䴶ⱘ Action ৢˈ䳔㽕೼ᑨ⫼Ё䜡㕂䆹 3.2.3 䜡㕂 Action ᔩ ೑䰙ֵ࣪ᙃᇍᑨⱘ key Ў userDŽ 䖒ᓣ⫳៤䕧ߎˈ䆹㸼䖒ᓣ䋳䋷䕧ߎϔϾ೑䰙ֵ࣪ᙃˈ䆹 ㄀ 3 ゴ Struts 2 ෎⸔ 62 ߿Ў"success"੠"error"ᄫヺІDŽ SUCCESS ੠ ERROR ϸϾᄫヺІᐌ䞣üüᅗӀⱘᅮНࣙ৿೼ ActionSupport ㉏Ёˈ݊ؐߚ ᇍᑨ/success.jsp 义䴶DŽᇍ↨Ϟ䴶ⱘ Action ㉏ҷⷕˈথ⦄䆹㉏ⱘ execute ᮍ⊩ߚ߿䖨ಲњ ݇㋏˖"input"ᄫヺІᇍᑨ/login.jsp 义䴶ˈ"error"ᄫヺІᇍᑨ/error.jsp 义䴶ˈ"success"ᄫヺІ Ϟ䴶䜡㕂 WebWork ᑨ⫼ᯊˈᣛᅮњ Action ໘⧚ৢ䘏䕥㾚೒ৡ੠⠽⧚㾚೒П䯈ⱘᇍᑨ ᭛ӊⱘ baseName Ў messDŽ 䛑Ӯ䞡䕑 WebWork ⱘ䜡㕂᭛ӊDŽϞ䴶䜡㕂᭛ӊⱘ᳔ৢϔ㸠ᣛᅮњ WebWork ⱘ೑䰙࣪䌘⑤ ಴Ўᴀᑨ⫼໘Ѣᓔথ䰊↉ˈᬙ䆒㕂њ WebWork ᑨ⫼ⱘᓔথ῵ᓣЎ trueˈᑊϨ↣⃵䇋∖ webwork.custom.i18n.resources=mess #ᣛᅮ WebWork ᑨ⫼ⱘ೑䰙࣪䌘⑤᭛ӊⱘ baseName Ў mess webwork.configuration.xml.reload=true #ᣛᅮ↣⃵䇋∖䛑䞡䕑 WebWork ⱘ䜡㕂᭛ӊ webwork.devMode = true #ᣛᅮ䆹 WebWork ᑨ⫼໘Ѣᓔথ῵ᓣϟˈབᵰߎ䫭ˈᇚ䕧ߎ᳈໮ߎ䫭䇗䆩ֵᙃ webwork.properties ᭛ӊⱘҷⷕDŽ ⱘሲᗻ᭛ӊˈ⬅ϔ㋏߫ⱘ WebWork ሲᗻⱘ key ੠ value 㒘៤DŽϟ䴶ᰃޚ䖭Ͼ᭛ӊᰃϔϾᷛ ᅲ䰙ϞˈWebWork ৃҹࣙ৿ϸϾ䜡㕂᭛ӊˈ䖬᳝ϔϾ webwork.properties 䜡㕂᭛ӊˈ থ䗕ⱘ䇋∖ˈ໘⧚䇋∖ⱘ Java ㉏Ў lee.LoginActionDŽ 䴶ⱘ Action 䜡㕂ˈ䆹 Action ⱘ name Ў loginˈ㸼⼎䆹 Action 䋳䋷໘⧚৥ login.action ⱘ URL ᗻᣛᅮњ䆹 Action 䋳䋷໘⧚ⱘ URLˈ㗠 class ሲᗻᣛᅮњ䆹 Action ᇍᑨⱘᅲ⦄㉏DŽ՟བϞ ㋴ᯊˈᖙ乏ᣛᅮϸϾЏ㽕ሲᗻ˖name ሲᗻ੠ class ሲᗻDŽ݊Ё name ሲܗ 䜡㕂 action ㋴DŽܗ㋴ⱘᄤܗ ㋴䛑ᰃ package ܗ 㰩ぐ WebWork ҹ package ᴹㅵ⧚ WebWork ⱘ Actionˈᬙ᠔᳝ⱘ action ᄬ೼DŽ ㋴ܗ㋴ⱘᄤܗ ㋴䛑԰Ў packageܗ WebWork ҹࣙᴹㅵ⧚᠔᳝ⱘ Actionˈ᠔ҹܼ䚼ⱘ action ㋴ϟㅵ⧚ˈ಴Ўܗ ೼Ϟ䴶ⱘ xwork.xml 䜡㕂᭛ӊЁˈ᠔᳝ⱘ Action ᬒ೼ package /success.jsp /error.jsp /login.jsp Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 63 ϸϾ㸼ऩඳ䖯㸠᷵偠ˈߚ߿᷵偠⫼᠋ৡᖙ乏⒵䎇ᖙ฿㾘߭ˈᆚⷕᖙ乏⒵䎇ᖙ฿ϨᄫヺІ䭓 Ϟ䴶ⱘ᷵偠᭛ӊՓ⫼њϸϾ㸼ऩ᷵偠఼˖requiredstring ੠ stringlengthˈߚ߿ᇍᑨ⫼ⱘ 6 3 ㋴ -->ܗ ϟ䴶ᰃᴀᑨ⫼ⱘ᭄᥂᷵偠᭛ӊҷⷕDŽ ⱘ䏃ᕘϟᄬᬒᇍᑨⱘ᷵偠᭛ӊेৃDŽ ߽⫼њ XWork ⱘ᷵偠Ḛᶊᴹᅠ៤᭄᥂᷵偠ˈᅠ៤᭄᥂᷵偠া䳔㽕೼ Action class ᭛ӊ᠔೼ 䕧ܹ᷵偠䳔㽕䞡ݭ Action ㉏ⱘ validate()ᮍ⊩ˈ䳔㽕໻䞣кݭ䞡໡ҷⷕˈⳌᔧ⚺⧤DŽᴀᑨ⫼ WebWork ৃҹᬃᣕ᠟ࡼᅠ៤䕧ܹ᷵偠ˈԚ䖭ϡᰃᴀᑨ⫼ᇚ䞛⫼ⱘᮍᓣˈ಴Ў᠟ࡼᅠ៤ 3.2.4 ᅠ៤᭄᥂᷵偠 ৢ䕀ܹⱘ㾚೒DŽ ㌑䡻 Ϣ Struts 1 ㉏ԐˈWebWork 䞛⫼ input 䘏䕥㾚೒ᴹ԰Ў䕧ܹ᷵偠༅䋹 М input 䘏䕥㾚೒ⱘ԰⫼ਸ਼˛input 䘏䕥㾚೒ᰃ䕧ܹ᷵偠༅䋹ৢ咬䅸䕀ܹⱘ㾚೒义䴶DŽ Ͼ success ⱘ䘏䕥㾚೒ˈ䆹䘏䕥㾚೒ᇍᑨЎ/success.jsp 义䴶˗৺߭ᇚ䖯ܹ/error.jsp 义䴶DŽ䙷 䖭ህᰃᴀ㋏㒳⌕⿟˖ᔧ⫼᠋䕧ܹⱘ⫼᠋ৡЎ scottˈϨᆚⷕЎ tiger ᯊˈAction 䖨ಲϔ ㄀ 3 ゴ Struts 2 ෎⸔ 64 welcomeMsg=⃶䖢ᙼˈ{0}ˈᙼᏆ㒣៤ࡳⱏᔩᴀ㋏㒳ʽ 䰙࣪⍜ᙃབϟ˖ ೼Ϟ䴶ⱘ义䴶Ёˈ៥ӀՓ⫼њᷛㅒᴹ䕧ߎ೑䰙࣪⍜ᙃˈ䆹ᷛㅒ䕧ߎⱘ೑

      <ww:property value="tip"/> <%@ taglib prefix="ww" uri="/webwork"%> <%@ page contentType="text/html;charset=GBK"%> ᦤ⼎ֵᙃDŽ义䴶ҷⷕབϟ˖ /success.jsp 义䴶DŽ䆹义䴶ⱘҷⷕ䴲ᐌㅔऩˈᅗাᰃㅔऩഄՓ⫼ WebWork ᷛㅒᴹ䕧ߎ tip ⱘ ᇚӮᇕ㺙ϔϾৡЎ tip ⱘሲᗻ೼㒧ᵰ䞠ˈᑊ䕀ܹ success ⱘ䘏䕥㾚೒ˈ䆹䘏䕥㾚೒ᇍᑨњ ℷབ៥Ӏ㾕ࠄⱘˈབᵰ៥Ӏ䕧ܹњড়䗖ⱘ⫼᠋ৡ੠ᆚⷕˈAction ໘⧚ᅠ⫼᠋䇋∖ৢˈ 3.2.5 ᅠ៤ᑨ⫼⌕⿟ 䅵᳈㉏ԐDŽ ᇍ↨Ϟ䴶ⱘ Struts 1 Ḛᶊ੠ WebWork ḚᶊⱘՓ⫼ˈϡ䲒থ⦄ WebWork Ϣ Struts 2 ⱘ䆒 ᑨ⫼ⱘ᭄᥂᷵偠DŽ 䗮䖛Ϟ䴶ⱘ⼎㣗ҷⷕˈৃҹⳟߎˈ㒧ড় XWork ⱘ᷵偠Ḛᶊˈৃҹ໻໻ㅔ࣪ WebWork ೒ 3.5 䕧ܹ᷵偠ⱘᬜᵰ ᔩ བᵰ⫼᠋䕧ܹϡ⒵䎇䆹㾘߭ˈ߭ⳟࠄབ೒ 3.5 ᠔⼎ⱘ⬠䴶DŽ ᑺᖙ乏೼ 3 ੠ 6 П䯈ⱘ㾘߭DŽ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 65 㡖⚍ϟⱘĀApplication Serversāᄤ㡖⚍ˈᇚⳟࠄབ೒ 3.8 ᠔⼎ⱘ⬠䴶DŽ ऩߏ೒ 3.7 ᠔⼎にষᎺ䖍ᇐ㟾ᷥЁⱘĀMyEclipseā㡖⚍ˈ✊ৢऩߏĀMyEclipseā াᰃ MyEclipse ᦦӊⱘⳌ݇ሲᗻDŽ ӊⱘⳌ݇ሲᗻDŽMyEclipse ᦦӊⱘⳌ݇ሲᗻህᰃ೼䆹にষ䞠䆒㕂ⱘˈ㗠ᭈড় Java EE ᳡ࡵ఼ 䆹 Preferences 䆒㕂にষᰃ Eclipse ໻䚼ߚখ᭄ⱘ䆒㕂にষˈ䆹にষ䖬ৃ䆒㕂 Eclipse ᦦ ᢽĀPreferencesā㦰ऩ乍ˈᇚᔍߎབ೒ 3.7 ᠔⼎ⱘ⬠䴶DŽ ऩߏ Eclipse にষ䞠ⱘĀWindowā㦰ऩˈ೼ߎ⦄ⱘĀWindowāⱘ㋏߫㦰ऩ乍Ё䗝 Ўњ೼ Eclipse Ёᭈড় Tomcat ᳡ࡵ఼ˈ䇋ᣝབϟℹ偸䖯㸠DŽ Ⳍᑨ䌍⫼ᠡৃՓ⫼DŽ ᑨ⫼ˈ䗮ᐌ䛑᥼㤤ᅝ㺙 MyEclipse ᦦӊˈ䆹ᦦӊᰃϔϾଚϮѻકˈᖙ乏ᬃҬ ㌑䡻 Eclipse ᖙ乏ᅝ㺙њⳌᑨᦦӊৢᠡ݋᳝Єᆠⱘࡳ㛑DŽЎњᓔথ Java EE ӏԩᦦӊˈEclipse ߭ᕜ䲒Ϣӏԩ Java EE ᳡ࡵ఼ᭈড়DŽ ߭ৃҹ೼ Eclipse Ёᭈড়໻䚼ߚⱘ Java EE ᳡ࡵ఼˄ࣙᣀ Web ᳡ࡵ఼˅DŽԚབᵰ៥Ӏ≵ᅝ㺙 ⦄೼ᓔྟ೼ Eclipse Ёᭈড় Tomcat ᳡ࡵ఼ˈᅲ䰙Ϟˈབᵰ៥Ӏᅝ㺙њ MyEclipse ᦦӊˈ ᑨ⫼ˈᑊᇚ䆹ᑨ⫼䚼㕆ࠄ Tomcat ᳡ࡵ఼ЁDŽ ϟ䴶ҹ Tomcat 5520 Ў՟ˈҟ㒡བԩ೼ Eclipse Ёᭈড় Tomcat ᳡ࡵ఼ˈᮄᓎϔϾ Web ᑨ⫼ˈᑊᇚ݊䚼㕆೼ Web ᳡ࡵ఼ЁDŽ ヨ㗙᠔Փ⫼ⱘ Eclipse Ꮖ㒣ᅝ㺙њ MyEclipse ᦦӊˈ಴ℸৃҹᕜᮍ֓ഄᓎゟϔϾ Web 3.3.1 ߯ᓎ Web ᑨ⫼ 催DŽᴀ㡖ᇚҹ Eclipse Ў՟ˈҟ㒡བԩ೼ IDE Ꮉ݋Ёᓔথ Struts 2 ᑨ⫼DŽ থᮍᓣ㱑✊㛑䅽䇏㗙᳈⏙Ἦᓔথⱘ㒚㡖ˈԚᇍѢᓔথᬜ⥛ैϡབՓ⫼ IDE Ꮉ݋ⱘᓔথᬜ⥛ ࠡ䴶Ꮖ㒣䆺㒚ҟ㒡њབԩᓔথϔϾ Struts 2 ᑨ⫼ˈԚ≵᳝Փ⫼ӏԩ IDE Ꮉ݋ˈ䙷⾡ᓔ 3.3 ೼ Eclipse Ёᓔথ Struts 2 ೒ 3.6 ⱏᔩ៤ࡳⱘ⬠䴶 ሲᗻؐDŽᔧ⫼᠋䕧ܹⱘ⫼᠋ৡ੠ᆚⷕℷ⹂ᯊˈᇚⳟࠄབ೒ 3.6 ᠔⼎ⱘ⬠䴶DŽ Ϟ䴶ҷⷕՓ⫼њᴹӴܹখ᭄ˈ䆹খ᭄ⱘؐህᰃ Action Ё user ㋴Ў㄀ѠϾখ᭄ᣛᅮؐĂĂձℸ㉏᥼DŽܗѠϾᄤ ㋴Ў㄀ϔϾখ᭄ᣛᅮؐˈ㄀ܗᄤᷛㅒᴹӴܹখ᭄˖㄀ϔϾᄤ ⳟࠄϞ䴶ⱘ೑䰙࣪⍜ᙃ䳔㽕ӴܹϔϾখ᭄ˈ៥Ӏ䗮䖛ЎᷛㅒᣛᅮϔϾ ㄀ 3 ゴ Struts 2 ෎⸔ 66 ܹњℷ⹂ⱘ Tomcat ᅝ㺙䏃ᕘˈϟ䴶ⱘϸϾ᭛ᴀḚӮ㞾ࡼ䕧ܹˈ᮴䳔⫼᠋ᑆ乘DŽ ⱘ tomcat5520 䏃ᕘϟˈᬙ೼䆹᭛ᴀḚݙ䕧ܹњ D:\tomcat5520 ⱘݙᆍDŽ䗮ᐌˈϔᮺ៥Ӏ䕧 ՟བˈTomcat Home Directory 䞠ᑨ䆹䕧ܹ Tomcat ⱘᅝ㺙䏃ᕘˈヨ㗙ⱘ Tomcat ᅝ㺙೼ D Ⲭ 䗝Ё Enable ऩ䗝ḚDŽ䗝Ё䆹ऩ䗝Ḛৢˈ䖬ᑨ䆹೼ϟ䴶ⱘϝϾ᭛ᴀ䕧ܹḚݙ䕧ܹⳌᑨⱘഄഔˈ ೼೒ 3.9 েϞᮍⳟࠄϸϾऩ䗝Ḛ˖Enable ੠ DisableˈЎњᭈড় Tomcatˈᰒ✊ᑨ䆹 ೒ 3.9 ᭈড় Tomcat5 5 㡖⚍ˈߎ⦄བ೒ 3.9 ᠔⼎ⱘ⬠䴶DŽ ՟བ JbossǃJettyǃResin ㄝˈᔧ✊гৃҹথ⦄៥Ӏ䳔㽕Փ⫼ⱘ Tomcat 㡖⚍ˈ✊ৢऩߏ Tomcat ᢪࡼ೒ 3.8 ᠔⼎にষᎺ䖍ⱘൖⳈ⒮ࡼᴵˈ៥Ӏৃҹথ⦄໻䚼ߚᑨ⫼᳡ࡵ఼ⱘৡᄫˈ ೒ 3.8 ೼ Eclipse Ёᭈড়ᑨ⫼᳡ࡵ఼ ೒ 3.7 Eclipse ⱘ Preferences 䆒㕂にষ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 67 㽕ׂᬍ web.xml ᭛ӊˈ䅽䆹᭛ӊ䋳䋷ࡴ䕑 Struts 2 ḚᶊDŽ ⳟࠄབ೒ 3.12 ᠔⼎ⱘ⬠䴶ˈ㸼ᯢ䆹 Web ᑨ⫼Ꮖ㒣ࡴܹњ Struts 2 ⱘḌᖗ㉏ᑧˈԚ䖬䳔 ⼎ⱘ⬠䴶DŽ ⚍ˈ䗝Ё䆹㡖⚍ˈ✊ৢᣝ F5 䬂ˈᇚⳟࠄ Eclipse Џ⬠䴶ᎺϞ㾦䌘⑤ᇐ㟾Ёߎ⦄བ೒ 3.12 ᠔ 䖨ಲ Eclipse ⱘЏ⬠䴶ˈ೼ Eclipse Џ⬠䴶ⱘᎺϞ㾦䌘⑤ᇐ㟾Ёⳟࠄњ struts2Demo 㡖 WEB-INF\libā䏃ᕘϟDŽ ᶊⱘḌᖗ㉏ᑧ໡ࠊࠄ Web ᑨ⫼ⱘ lib 䏃ᕘϟˈгህᰃĀ%workspace%Struts2Demo\WebRoot\ ᑨ⫼ЁDŽᇚ Struts 2 Ḛᶊϟ lib 䏃ᕘϟⱘ struts2-core-2.0.6.jarǃxwork-2.0.1.jar ㄝ Struts 2 Ḛ Ўњ䅽 Web ᑨ⫼݋᳝ Struts 2 ᬃᣕࡳ㛑ˈᖙ乏ᇚ Struts 2 ḚᶊⱘḌᖗ㉏ᑧ๲ࡴࠄ Web 3.3.2 ๲ࡴ Struts 2 ᬃᣕ ᣝ䪂ˈᮄᓎ Web ᑨ⫼៤ࡳDŽ Struts2Demo ԰Ўᑨ⫼ⱘৡᄫˈ䕧ܹᅠ៤ৢৃⳟࠄབ೒ 3.11 ᠔⼎ⱘᇍ䆱ḚDŽऩߏĀFinishā ೼བ೒ 3.11 ᠔⼎ᇍ䆱Ḛⱘ Project Name ᭛ᴀḚЁ䕧ܹᑨ⫼ⱘৡᄫˈᴀᑨ⫼Փ⫼ ೒ 3.10 ᮄᓎ Web ᑨ⫼ⱘᇍ䆱Ḛ ೒ 3.11 ᮄᓎ Web ᑨ⫼ ᑨ⫼ˈ✊ৢऩߏĀNextāᣝ䪂ˈⳟࠄབ೒ 3.11 ᠔⼎ⱘᇍ䆱ḚDŽ ೼བ೒ 3.10 ᠔⼎ⱘᇍ䆱ḚЁˈ䗝ЁĀWeb Projectā㡖⚍ˈ㸼ᯢᇚ㽕߯ᓎϔϾ Web 㦰ऩ乍ˈᇚᔍߎϔϾབ೒ 3.10 ᠔⼎ⱘᇍ䆱ḚDŽ ऩߏ Eclipse Џ⬠䴶ЁⱘĀFileā㦰ऩˈ೼ߎ⦄ⱘ㦰ऩЁऩߏĀNewāϟ ⱘĀ Other...ā ࡽѢ MyEclipse ᓎゟ Web ᑨ⫼ᣝབϟℹ偸䖯㸠DŽ׳ࡽѢ MyEclipse ⱘᐂࡽDŽ׳ ϟ䴶ҟ㒡བԩ೼ Eclipse ЁᓎゟϔϾ Web ᑨ⫼ˈ೼ Eclipse ЁᓎゟϔϾ Web ᑨ⫼гৃ ⱘᭈড়៤ࡳDŽ ऩߏ೒ 3.9 ᠔⼎にষেϟᮍⱘĀOKāᣝ䪂ˈ䖨ಲ Eclipse Џ⬠䴶ˈEclipse ੠ Tomcat ㄀ 3 ゴ Struts 2 ෎⸔ 68 ՟བᴀ⼎՟ህᰃ Struts2DemoDŽऩߏে䖍ⱘĀAddāᣝ䪂ˈ䆹ᣝ䪂⫼Ѣ⏏ࡴᛇ㽕䚼㕆ࠄⱘ ೼೒ 3.14 ᠔⼎ᇍ䆱ḚⱘϞ䴶ⳟࠄ䗝ᢽ乍Ⳃⱘϟᢝ߫㸼ˈ䗝Ё䳔㽕䚼㕆ⱘ Web ᑨ⫼ˈ ೒ 3.14 䚼㕆 Web ᑨ⫼ⱘᇍ䆱Ḛ ऩߏ䚼㕆 Web ᑨ⫼ⱘᣝ䪂ˈᔍߎབ೒ 3.14 ᠔⼎ⱘᇍ䆱ḚDŽ ೼ Eclipse Ё䚼㕆 Web ᑨ⫼䇋ᣝབϟℹ偸䖯㸠DŽ ೒ 3.13 ೼ Eclipse Ё䚼㕆 Web ᑨ⫼੠ਃࡼ᳡ࡵ఼ⱘᣝ䪂 ᑨ⫼੠ਃࡼ᳡ࡵ఼ⱘϸϾᣝ䪂བ೒ 3.13 ᠔⼎DŽ ᓎゟњ䖭ѯ JSP 义䴶ৢˈऩߏ Eclipse Џ⬠䴶Ϟ䚼㕆 Web ᑨ⫼ⱘᎹ݋ᣝ䪂ˈ䚼㕆 Web 䗄DŽ ೼ Eclipse Ё⏏ࡴ JSP 义䴶гᰃᕜㅔऩⱘˈℸ໘ϡݡ䌬 ᐌ㉏Ԑˈᬙℸ໘ϡݡ㒭ߎ䖭ѯ JSP 义䴶ⱘҷⷕDŽ೒ 3.12 ⏏ࡴњ Struts 2 ㉏ᑧ ಴ℸᴀ㡖᠔Փ⫼ⱘ JSP 义䴶Ϣ㄀ 2 ゴ᠔Փ⫼ⱘ JSP 义䴶г䴲 ᴀᑨ⫼ⱘࡳ㛑䴲ᐌ㉏ԐѢ㄀ 2 ゴ᠔ҟ㒡ⱘ Struts 2 ᑨ⫼ˈ 3.3.3 䚼㕆 Struts 2 ᑨ⫼ Web ᑨ⫼ᅠܼ݋໛њ Struts 2 ḚᶊⱘᬃᣕDŽ ᖗ FilterˈᑊᅮН䆹 Filter ᠔ᢺ៾ⱘ URL ῵ᓣDŽ㟇ℸˈ䆹 ϱ↿⡍⅞П໘ˈৠḋᰃ೼ web.xml ᭛ӊЁᅮН Struts 2 ⱘḌ ऩߏ web.xml ᭛ӊ㡖⚍ˈ㓪䕥䆹᭛ӊˈ㓪䕥䆹᭛ӊ≵᳝ 㡖⚍ϟࣙ৿ⱘ web.xml ᭛ӊᄤ㡖⚍DŽ ೼བ೒ 3.12 ᠔⼎ⱘᇐ㟾ᷥЁˈऩߏĀWEB-INFā㡖⚍ࠡⱘࡴোˈሩᓔ䆹㡖⚍ˈⳟࠄ䆹 Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 69 བ೒ 3.18 ᠔⼎ⱘ⬠䴶DŽ 㟇ℸˈStruts 2 ᑨ⫼Ꮖ㒣㦋ᕫњ೑䰙࣪ᬃᣕDŽབᵰ೼⌣㾜఼Ёݡ⃵⌣㾜䆹义䴶ˈᇚⳟࠄ struts.custom.i18n.resources=messageResource #ᣛᅮ Struts 2 ⱘ೑䰙࣪䌘⑤᭛ӊⱘ baseName Ў messageResource ӊݙᣛᅮབϟϔ㸠ҷⷕ˖ Ўњ䅽 Struts 2 ᑨ⫼ࡴ䕑䆹೑䰙࣪䌘⑤᭛ӊˈ䖬ᑨ䆹Փ⫼ struts.properties ᭛ӊˈ䆹᭛ ⫼ⱘ䌘⑤᭛ӊⳌৠˈℸ໘ϡݡ㒭ߎDŽ Ўᴀᑨ⫼㔎ᇥ೑䰙࣪䌘⑤᭛ӊDŽ๲ࡴ䆹ᑨ⫼᠔䳔ⱘ೑䰙࣪䌘⑤᭛ӊˈ䆹䌘⑤᭛ӊϢࠡ䴶ᑨ ೼೒ 3.17 Ёⳟࠄ䆹ⱏᔩ义䴶ⱘ᭛ᴀḚ≵᳝ӏԩᦤ⼎ֵᙃˈᣝ䪂Ϟг≵᳝᭛ᴀˈ䖭ᰃ಴ ೒ 3.16 ਃࡼ᳡ࡵ఼ ೒ 3.17 䚼㕆៤ࡳৢⱘⱏᔩ⬠䴶 login.jspˈᇚⳟࠄབ೒ 3.17 ᠔⼎ⱘ⬠䴶DŽ ⱘッষЎ 8888 Ў՟ˈᑨ䆹೼⌣㾜఼Ё䆓䯂བϟഄഔ˖http://localhost:8888/Struts2Demo/ ೼⌣㾜఼Ё䆓䯂߮ᠡⱘ Struts2Demo ᑨ⫼ˈᇚৃⳟࠄᴀᑨ⫼ⱏᔩ义䴶DŽҹ Tomcat ᳡ࡵ఼DŽ ϝ㾦ˈߎ⦄ϟᢝ㦰ऩˈ䗝ᢽĀTomcat 5āϟ ⱘĀStartā㦰ऩ乍ˈབ೒ 3.16 ᠔⼎ˈਃࡼ Tomcat Web ᑨ⫼䚼㕆៤ࡳৢˈऩߏབ೒ 3.13 ᠔⼎Ꮉ݋ᴵЁⱘĀਃࡼ᳡ࡵ఼āᣝ䪂ᮕⱘᇣ Web ᑨ⫼䚼㕆៤ࡳDŽ 䆒㕂খ✻೒ 3.15ˈ✊ৢऩߏĀFinishāᣝ䪂ˈ䖨ಲ೒ 3.14 ᠔⼎ᇍ䆱ḚˈऩߏĀOKāᣝ䪂ˈ ೼೒ 3.15 ᠔⼎ᇍ䆱ḚϞ䴶ⱘ Server ϟᢝ߫㸼Ё䗝ЁĀTomcat 5ā䗝乍ˈ݊Ҫ䗝乍 ೒ 3.15 䚼㕆ࠄᑨ⫼᳡ࡵ఼ Web ᳡ࡵ఼ˈᇚⳟࠄབ೒ 3.15 ᠔⼎ⱘᇍ䆱ḚDŽ ㄀ 3 ゴ Struts 2 ෎⸔ 70 /welcome.jsp /error.jsp /login.jsp ㋴ -->ܗ ӊҷⷕབϟ˖ ᴀϔ㟈ˈ䳔㽕ᅮН Action ⱘ name ሲᗻǃclass ሲᗻㄝDŽ๲ࡴ Action ᅮНৢⱘ struts.xml ᭛ ᓎゟ䆹᭛ӊৢˈЎ䆹᭛ӊ๲ࡴ Struts 2 ⱘ Action ᅮНˈᅮН䆹 Action Ϣࠡ䴶ҟ㒡ⱘ෎ ᑨ䆹ৃҹ᳝᳈ㅔऩⱘᮍ⊩ᴹᓎゟ Struts 2 ⱘ䜡㕂᭛ӊDŽ ᓣᴹᓎゟϔϾ Struts 2 ⱘ䜡㕂᭛ӊDŽㄝࠄϧ䮼ⱘ Struts 2 ᦦӊ㹿ᓔথߎᴹৢˈ ㌑䡻 ಴Ў Eclipse ᱖ᯊ䖬≵᳝ϧ䮼ⱘ Struts 2 ᦦӊˈ಴ℸা㛑⫼᳔ॳྟⱘᮍ ⱘĀFileā㦰ऩˈ✊ৢऩߏĀNewāϟⱘĀFileā㦰ऩ乍ᴹ᪡԰ⱘDŽ ๲ࡴњ Struts 2 ⱘ Action ㉏ৢˈ䖬䳔㽕๲ࡴᇍᑨⱘ䜡㕂᭛ӊˈгᰃ䗮䖛ऩߏ Eclipse ゴⱘ Action ㉏ᅠܼϔḋˈℸ໘гϡݡ㒭ߎDŽ ᮄᓎϔϾ Java ᭛ӊⱘᮍ⊩䴲ᐌㅔऩˈℸ໘ϡݡ䌬䗄DŽᴀᑨ⫼ⱘ Action ㉏ⱘҷⷕϢ㄀ 2 ೒ 3.18 ๲ࡴњ೑䰙࣪䌘⑤᭛ӊৢⱘⱏᔩ义䴶 ᔩ lee ⳂᔩϟDŽ ゴ᠔Փ⫼ⱘ LoginAction ㉏ҷⷕⳌৠˈᇚ䆹㉏᭛ӊֱᄬ೼ Struts2Demo ᑨ⫼ⱘ src 䏃ᕘϟⱘ ೼ Eclipse Ꮉ݋ЁᮄᓎϔϾ Java ㉏ˈ䆹 Java ㉏ⱘ㉏ৡЎĀLoginActionāˈ݊ҷⷕϢ㄀ 2 3.3.4 ๲ࡴᑨ⫼ⱘ Action Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ ㄀ 3 ゴ Struts 2 ෎⸔ 㟇ℸˈᭈϾ Struts 2 ᑨ⫼ᅠܼᓎゟ៤ࡳˈབᵰ䇏㗙䖬䳔㽕๲ࡴ᭄᥂᷵偠ˈ߭ৃ๲ࡴᇍ ᑨⱘ᷵偠᭛ӊˈᑊᇚ䆹᭛ӊᬒ೼Ϣᇍᑨ Action ⑤᭛ӊⱘৠϔ䏃ᕘϟेৃDŽ 71 Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 3.4 Struts 2 ⱘ෎ᴀ⌕⿟ 㒣䖛ࠡ䴶ҟ㒡ˈ៥ӀᏆ㒣෎ᴀњ㾷њ Struts 2 Ḛᶊⱘ MVC ᅲ⦄DŽ໻㟈ϞˈStruts 2 Ḛ ᶊ⬅ 3 Ͼ䚼ߚ㒘៤˖Ḍᖗ᥻ࠊ఼ FilterDispatcherǃϮࡵ᥻ࠊ఼੠⫼᠋ᅲ⦄ⱘϮࡵ䘏䕥㒘ӊDŽ ೼䖭 3 Ͼ䚼ߚ䞠ˈStruts 2 ḚᶊᦤկњḌᖗ᥻ࠊ఼ FilterDispatcherˈ㗠⫼᠋䳔㽕ᅲ⦄Ϯࡵ᥻ ࠊ఼੠Ϯࡵ䘏䕥㒘ӊDŽ 3.4.1 Ḍᖗ᥻ࠊ఼˖FilterDispatcher FilterDispatcher ᰃ Struts 2 ḚᶊⱘḌᖗ᥻ࠊ఼ˈ䆹᥻ࠊ఼԰ЎϔϾ Filter 䖤㸠೼ Web ᑨ⫼Ёˈᅗ䋳䋷ᢺ៾᠔᳝ⱘ⫼᠋䇋∖ˈᔧ⫼᠋䇋∖ࠄ䖒ᯊˈ䆹 Filter Ӯ䖛Ⓒ⫼᠋䇋∖DŽབ ᵰ⫼᠋䇋∖ҹ action 㒧ሒˈ䆹䇋∖ᇚ㹿䕀ܹ Struts 2 Ḛᶊ໘⧚DŽ Struts 2 Ḛᶊ㦋ᕫњ*.action 䇋∖ৢˈᇚḍ᥂*.action 䇋∖ⱘࠡ䴶䚼ߚއᅮ䇗⫼ાϾϮࡵ 䘏䕥㒘ӊˈ՟བˈᇍѢ login.action 䇋∖ˈStruts 2 䇗⫼ৡЎ login ⱘ Action ᴹ໘⧚䆹䇋∖DŽ Struts 2 ᑨ⫼Ёⱘ Action 䛑㹿ᅮН೼ struts.xml ᭛ӊЁˈ೼䆹᭛ӊЁᅮН Action ᯊˈᅮ Нњ䆹 Action ⱘ name ሲᗻ੠ class ሲᗻˈ݊Ё name ሲᗻއᅮњ䆹 Action ໘⧚ાϾ⫼᠋䇋 ∖ˈ㗠 class ሲᗻއᅮњ䆹 Action ⱘᅲ⦄㉏DŽ Struts 2 ⫼Ѣ໘⧚⫼᠋䇋∖ⱘ Action ᅲ՟ˈᑊϡᰃ⫼᠋ᅲ⦄ⱘϮࡵ᥻ࠊ఼ˈ㗠ᰃ Action ҷ⧚üü಴Ў⫼᠋ᅲ⦄ⱘϮࡵ᥻ࠊ఼ᑊ≵᳝Ϣ Servlet API 㗺ড়ˈᰒ✊᮴⊩໘⧚⫼᠋䇋∖DŽ 㗠 Struts 2 Ḛᶊᦤկњ㋏߫ᢺ៾఼ˈ䆹㋏߫ᢺ៾఼䋳䋷ᇚ HttpServletRequest 䇋∖Ёⱘ䇋∖ খ᭄㾷ᵤߎᴹˈӴܹࠄ Action Ёˈᑊಲ䇗 Action ⱘ execute ᮍ⊩ᴹ໘⧚⫼᠋䇋∖DŽ ᰒ✊ˈϞ䴶ⱘ໘⧚䖛⿟ᰃ݌ൟⱘ AOP˄䴶৥ߛ䴶㓪⿟˅໘⧚ᮍᓣDŽ೒ 3.19 ᰒ⼎њ䖭 ⾡໘⧚῵ൟDŽ ೒ 3.19 Struts 2 ⱘᢺ៾఼੠ Action Ң೒ 3.19 Ёৃҹⳟߎˈ⫼᠋ᅲ⦄ⱘ Action ㉏ҙҙᰃ Struts 2 ⱘ Action ҷ⧚ⱘҷ⧚ⳂᷛDŽ ⫼᠋ᅲ⦄ⱘϮࡵ᥻ࠊ఼˄Action˅߭ࣙ৿њᇍ⫼᠋䇋∖ⱘ໘⧚DŽ⫼᠋ⱘ䇋∖᭄᥂ࣙ৿೼ HttpServletRequest ᇍ䈵䞠ˈ㗠⫼᠋ⱘ Action ㉏᮴䳔䆓䯂 HttpServletRequest ᇍ䈵DŽᢺ៾఼ 72 ㄀ 3 ゴ Struts 2 ෎⸔ 䋳䋷ᇚ HttpServletRequest 䞠ⱘ䇋∖᭄᥂㾷ᵤߎᴹˈᑊӴ㒭Ϯࡵ䘏䕥㒘ӊ Action ᅲ՟DŽ 3.4.2 Ϯࡵ᥻ࠊ఼ ℷབҢ೒ 3.19 ᠔ⳟࠄⱘˈϮࡵ᥻ࠊ఼㒘ӊህᰃ⫼᠋ᅲ⦄ Action ㉏ⱘᅲ՟ˈAction ㉏ 䞠䗮ᐌࣙ৿њϔϾ execute ᮍ⊩ˈ䆹ᮍ⊩䖨ಲϔϾᄫヺІüü䆹ᄫヺІህᰃϔϾ䘏䕥㾚೒ ৡˈᔧϮࡵ᥻ࠊ఼໘⧚ᅠ⫼᠋䇋∖ৢˈḍ᥂໘⧚㒧ᵰϡৠˈexecute ᮍ⊩䖨ಲϡৠᄫヺІ üü↣ϾᄫヺІᇍᑨϔϾ㾚೒ৡDŽ ⿟ᑣਬᓔথߎ㋏㒳᠔䳔㽕ⱘϮࡵ᥻ࠊ఼ৢˈ䖬䳔㽕䜡㕂 Struts 2 ⱘ Actionˈे䳔㽕䜡 㕂 Action ⱘབϟϝϾ䚼ߚᅮН˖  Action ᠔໘⧚ⱘ URLDŽ  Action 㒘ӊ᠔ᇍᑨⱘᅲ⦄㉏DŽ  Action 䞠ࣙ৿ⱘ䘏䕥㾚೒੠⠽⧚䌘⑤П䯈ⱘᇍᑨ݇㋏DŽ ↣Ͼ Action 䛑㽕໘⧚ϔϾ⫼᠋䇋∖ˈ㗠⫼᠋䇋∖ᘏᰃࣙ৿њᣛᅮ URLDŽᔧ Filter Dispatcher ᢺ៾ࠄ⫼᠋䇋∖ৢˈḍ᥂䇋∖ⱘ URL ੠ Action ໘⧚ URL П䯈ⱘᇍᑨ݇㋏ᴹ໘ ⧚䕀থDŽ 3.4.3 Struts 2 ⱘ῵ൟ㒘ӊ ᅲ䰙Ϟˈ῵ൟ㒘ӊᏆ㒣䍙ߎњ MVC Ḛᶊⱘ㽚Ⲫ㣗ೈDŽᇍѢ Struts 2 Ḛᶊ㗠㿔ˈ䗮ᐌ≵ ᳝Ў῵ൟ㒘ӊⱘᅲ⦄ᦤկ໾໮ⱘᐂࡽDŽ Java EE ᑨ⫼䞠ⱘ῵ൟ㒘ӊˈ䗮ᐌᣛ㋏㒳ⱘϮࡵ䘏䕥㒘 ӊDŽ㗠䱤㮣೼㋏㒳ⱘϮࡵ䘏䕥㒘ӊϟ䴶ⱘˈৃ㛑䖬ࣙ৿њ DAOǃ乚ඳᇍ䈵ㄝ㒘ӊDŽ 䗮ᐌˈMVC Ḛᶊ䞠ⱘϮࡵ᥻ࠊ఼Ӯ䇗⫼῵ൟ㒘ӊⱘᮍ ⊩ᴹ໘⧚⫼᠋䇋∖DŽгህᰃ䇈ˈϮࡵ䘏䕥᥻ࠊ఼ϡӮᇍ⫼᠋ 䇋∖䖯㸠ӏԩᅲ䰙໘⧚ˈ⫼᠋䇋∖᳔㒜⬅῵ൟ㒘ӊ䋳䋷໘ ⧚DŽϮࡵ᥻ࠊ఼াᰃЁ䯈䋳䋷䇗ᑺⱘ䇗ᑺ఼ˈ䖭гᰃ⿄ Action Ў᥻ࠊ఼ⱘॳ಴DŽ ೒ 3.20 ᥻ࠊ఼䇗⫼῵ൟ㒘ӊ ೒ 3.20 ᰒ⼎њ䖭⾡໘⧚⌕⿟DŽ ㌑䡻 ೼೒ 3.20 Ёⳟࠄ Action 䇗⫼Ϯࡵ䘏䕥㒘ӊⱘᮍ⊩DŽᔧ᥻ࠊ఼䳔㽕㦋 ᕫϮࡵ䘏䕥㒘ӊᅲ՟ᯊˈ䗮ᐌᑊϡӮⳈ᥹㦋পϮࡵ䘏䕥㒘ӊᅲ՟ˈ㗠ᰃ䗮䖛 Ꮉॖ῵ᓣᴹ㦋ᕫϮࡵ䘏䕥㒘ӊⱘᅲ՟˗៪㗙߽⫼݊Ҫ IoC ᆍ఼˄བ Spring ᆍ఼˅ᴹㅵ⧚Ϯࡵ䘏䕥㒘ӊⱘᅲ՟DŽ 3.4.4 Struts 2 ⱘ㾚೒㒘ӊ Struts 2 Ꮖ㒣ᬍবњ Struts 1 া㛑Փ⫼ JSP ԰Ў㾚೒ᡔᴃⱘ⦄⢊ˈStruts 2 ܕ䆌Փ⫼݊Ҫ ⱘ῵ᵓᡔᴃˈབ FreeMarkerǃVelocity ԰Ў㾚೒ᡔᴃDŽ ᔧ Struts 2 ⱘ᥻ࠊ఼䖨ಲ䘏䕥㾚೒ৡᯊˈ䘏䕥㾚೒ᑊ᳾Ϣӏԩⱘ㾚೒ᡔᴃ݇㘨ˈҙҙ 73 74 䜡㕂 FilterDispatcher ⱘҷⷕ⠛↉བϟ˖ ࡴ䕑 FilterDispacherˈা䳔㽕೼ web.xml ᭛ӊЁ䜡㕂 FilterDispatcher ेৃDŽ ಴Ў Struts 2 ᇚḌᖗ᥻ࠊ఼䆒䅵៤ Filterˈ㗠ϡᰃϔϾ᱂䗮 ServletDŽᬙЎњ䅽 Web ᑨ⫼ ᇚӮࡴ䕑ᑨ⫼ⱘ Struts 2 ḚᶊDŽ 㿔ˈ䳔㽕ࡴ䕑 FilterDispatcherˈা㽕 Web ᑨ⫼䋳䋷ࡴ䕑 FilterDispatcherˈFilterDispatcher 䗮ᐌˈ᠔᳝ⱘ MVC Ḛᶊ䛑䳔㽕 Web ᑨ⫼ࡴ䕑ϔϾḌᖗ᥻ࠊ఼ˈᇍѢ Struts 2 Ḛᶊ㗠 ೼ web.xml ᭛ӊЁ Servlet ᠡӮ㹿ᑨ⫼ࡴ䕑DŽ ࡽѢ web.xml ᭛ӊˈা᳝䜡㕂׳ӏԩ MVC Ḛᶊ䛑䳔㽕Ϣ Web ᑨ⫼ᭈড়ˈ䖭ህϡᕫϡ 3.5.1 䜡㕂 web.xml ᭛ӊ 㕂᭛ӊ෎⸔ПϞˈ䖭ѯ䜡㕂᭛ӊⱘ䜡㕂ֵᙃгᰃ Struts 2 ᑨ⫼ⱘḌᖗ䚼ߚDŽ ࠡ䴶໻㟈њ㾷њ Struts 2 Ḛᶊⱘ෎ᴀݙᆍˈԚ䖭ѯ෎ᴀݙᆍ䛑ᖙ乏ᓎゟ೼ Struts 2 ⱘ䜡 3.5 Struts 2 ⱘ෎ᴀ䜡㕂 њ㾷 Struts 2 ⱘ䖤㸠⌕⿟DŽ ಴ℸˈStruts 2 ⱘ䖤㸠⌕⿟Ϣ WebWork ⱘ䖤㸠⌕⿟ᅠܼⳌৠˈ䇏㗙ৃҹখⳟ೒ 1.8 ᴹ М WebWork ੠ Struts 2 བℸⳌԐʽ 䖭䞠៥ӀৃҹⳟࠄˈStruts 2 ݊ᅲህᰃ WebWork 2.2 ⱘछ㑻⠜ˈ䖭гህϡ䲒⧚㾷˖ЎҔ 2.2 ᳈໡ᴖDŽ ৡ໪DŽҢ WebWork 2.2 䖕⿏ࠄ Struts 2 ϡӮ↨Ң WebWork 2.1 䖕⿏ࠄ WebWork üüᛣᗱᰃ䇈˖Struts 2.0 ᡔᴃㄝৠѢ WebWork 2.3 Ḛᶊˈ䰸њࣙ੠ሲᗻ㹿ᬍ renamingˈit isn't much different thanˈsayˈmigrating from WebWork 2.1 to 2.2 is the technical equivalent of WebWork 2.3DŽAside from the package and property ㌑䡻 ೼ Struts 2 ⱘᅬᮍキ⚍ˈ៥Ӏৃҹᡒࠄབϟ䇈⊩˖EssentiallyˈStruts 2.0 㒣䖛Ϟ䴶ҟ㒡ˈ៥Ӏথ⦄ Struts 2 Ḛᶊⱘ䖤㸠⌕⿟䴲ᐌ㉏ԐѢ WebWork Ḛᶊⱘ⌕⿟DŽ 3.4.5 Struts 2 ⱘ䖤㸠⌕⿟ ሲᗻĂĂ ሲᗻ˗བᵰᛇՓ⫼ Velocity ῵ᵓᡔᴃ԰Ў㾚೒䌘⑤ˈ߭Ў result ᣛᅮؐЎ velocity ⱘ type type ሲᗻेৃDŽ՟བˈབᵰ䳔㽕Փ⫼ FreeMarkerˈ߭Ў result ᣛᅮؐЎ freemarker ⱘ type ㋴ᯊˈᣛᅮⳌᑨⱘܗབᵰ䳔㽕೼ Struts 2 ЁՓ⫼݊Ҫ㾚೒ᡔᴃˈ߭ৃҹ೼䜡㕂 result ᄤ ㋴ᯊ≵᳝ᣛᅮ type ሲᗻˈ咬䅸Փ⫼ JSP ԰Ў㾚೒䌘⑤DŽ ܗ⠽⧚㾚೒П䯈ⱘ᯴ᇘDŽࠡ䴶᠔ҟ㒡ⱘᑨ⫼䛑Փ⫼њ JSP ᡔᴃ԰Ў㾚೒ˈᬙ䜡㕂 result ᄤ ㋴ᅮНϔϾ䘏䕥㾚೒੠ܗ㋴ˈ↣Ͼ result ᄤܗ㋴ᣛᅮ㋏߫ result ᄤܗ ሲᗻˈ䖬㽕Ў Action ᔧ៥Ӏ೼ struts.xml ᭛ӊЁ䜡㕂 Action ᯊˈϡҙ䳔㽕ᣛᅮ Action ⱘ name ሲᗻ੠ class ᰃ䖨ಲϔϾᄫヺІˈ䆹ᄫヺІ԰Ў䘏䕥㾚೒ৡDŽ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 75 ᶊⱘᷛㅒ᭛ӊˈ಴ℸᖙ乏೼ web.xml ᭛ӊЁ䜡㕂ࡴ䕑 Struts 2 ᷛㅒᑧDŽ བᵰ Web ᑨ⫼Փ⫼њ Servlet 2.3 ҹࠡⱘ㾘㣗ˈ಴Ў Web ᑨ⫼ϡӮ㞾ࡼࡴ䕑 Struts 2 Ḛ 䜡㕂њ Struts 2 ⱘḌᖗ FilterDispatcher ৢˈ෎ᴀᅠ៤њ Struts 2 ೼ web.xml ᭛ӊЁⱘ䜡㕂DŽ /* struts ϟ䴶ᰃ䜡㕂䆹 Filter ᢺ៾ URL ⱘ䜡㕂⠛↉˖ Filter ᢺ៾᠔᳝ⱘ⫼᠋䇋∖ˈ಴ℸՓ⫼䗮䜡ヺᴹ䜡㕂䆹 Filter ᢺ៾ⱘ URLDŽ ೼ web.xml ᭛ӊЁ䜡㕂њ䆹 Filterˈ䖬䳔㽕䜡㕂䆹 Filter ᢺ៾ⱘ URLDŽ䗮ᐌˈ៥Ӏ䅽䆹 ㌑䡻 ݇Ѣ Struts 2 ᐌ䞣ⱘ䆆㾷ˈ䇋খ䯙ᴀкⱘ 4.1.2 㡖DŽ ㋴ᣛᅮњᐌ䞣 valueDŽܗ㋴ᣛᅮњᐌ䞣 nameˈ㗠ᄤܗ䞣ˈ݊Ёᄤ ㋴䜡㕂ϔϾ Struts 2 ᐌܗ<䰸ℸП໪ˈ䖬ৃ೼ℸ໘䜡㕂 Struts 2 ᐌ䞣ˈ↣Ͼ lee.MyConfigurationProvider configProviders org.apache.struts2.showcase.person actionPackages org.apache.struts2.dispatcher.FilterDispatcher struts ㄀ 3 ゴ Struts 2 ෎⸔ Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 䜡㕂ࡴ䕑 Struts 2 ᷛㅒᑧⱘ䜡㕂⠛↉བϟ˖ /s /WEB-INF/struts-tags.tld ೼Ϟ䴶䜡㕂⠛↉Ёˈᣛᅮњ Struts 2 ᷛㅒᑧ䜡㕂᭛ӊ⠽⧚ԡ㕂˖/WEB-INF/struts- tags.tldˈ಴ℸ៥Ӏᖙ乏᠟ࡼ໡ࠊ Struts 2 ⱘᷛㅒᑧᅮН᭛ӊˈᇚ䆹᭛ӊᬒ㕂೼ Web ᑨ⫼ⱘ WEB-INF 䏃ᕘϟDŽ བᵰ Web ᑨ⫼Փ⫼ Servlet 2.4 ҹϞⱘ㾘㣗ˈ߭᮴䳔೼ web.xml ᭛ӊЁ䜡㕂ᷛㅒᑧᅮНˈ ಴Ў Servlet 2.4 㾘㣗Ӯ㞾ࡼࡴ䕑ᷛㅒᑧᅮН᭛ӊDŽ ㌑䡻 Struts 2 ⱘᷛㅒᑧᅮН᭛ӊࣙ৿೼ struts2-core-2.0.6.jar ᭛ӊ䞠ˈ೼ struts2-core-2.0.6.jar ᭛ӊⱘ META-INF 䏃ᕘϟˈࣙ৿њϔϾ struts-tag.tld ᭛ ӊˈ䖭Ͼ᭛ӊህᰃ Struts 2 ⱘᷛㅒᑧᅮН᭛ӊˈServlet 2.4 㾘㣗Ӯ㞾ࡼࡴ䕑䆹 ᷛㅒᑧ᭛ӊDŽ ᇍѢ Servlet 2.4 ҹϞⱘ㾘㣗ˈWeb ᑨ⫼㞾ࡼࡴ䕑䆹ᷛㅒᑧᅮН᭛ӊDŽࡴ䕑 struts-tag.tld ᷛㅒᑧᅮН᭛ӊᯊˈ䆹᭛ӊⱘᓔྟ䚼ߚࣙ৿བϟҷⷕ⠛↉˖ 2.2.3 1.2 s /struts-tags ... ಴Ў䆹᭛ӊЁᏆ㒣ᅮНњ䆹ᷛㅒᑧⱘ URI˖struts-tagsˈ䖭ህ䙓ܡњ೼ web.xml ᭛ӊЁ 䞡ᮄᅮН Struts 2 ᷛㅒᑧ᭛ӊⱘ URIDŽ 3.5.2 struts.xml 䜡㕂᭛ӊ Struts ḚᶊⱘḌᖗ䜡㕂᭛ӊህᰃ struts.xml 䜡㕂᭛ӊˈ䆹᭛ӊЏ㽕䋳䋷ㅵ⧚ Struts 2 Ḛ ᶊⱘϮࡵ᥻ࠊ఼ ActionDŽ ೼咬䅸ᚙމϟˈStruts 2 Ḛᶊᇚ㞾ࡼࡴ䕑ᬒ೼ WEB-INF/classes 䏃ᕘϟⱘ struts.xml ᭛ ӊDŽ೼໻䚼ߚᑨ⫼䞠ˈ䱣ⴔᑨ⫼㾘῵ⱘ๲ࡴˈ㋏㒳Ё Action ᭄䞣г໻䞣๲ࡴˈᇐ㟈 struts.xml 䜡㕂᭛ӊবᕫ䴲ᐌ㞗㚓DŽ Ўњ䙓ܡ struts.xml ᭛ӊ䖛Ѣᑲ໻ǃ㞗㚓ˈᦤ催 struts.xml ᭛ӊⱘৃ䇏ᗻˈ៥Ӏৃҹᇚ ϔϾ struts.xml 䜡㕂᭛ӊߚ㾷៤໮Ͼ䜡㕂᭛ӊˈ✊ৢ೼ struts.xml ᭛ӊЁࣙ৿݊Ҫ䜡㕂᭛ӊDŽ 76 77 .... class="org.apache.struts2.impl.StrutsObjectFactory" /> ㋴ -->ܗ ᶹⳟ struts-default.xml ᭛ӊˈⳟࠄ䆹᭛ӊҷⷕ⠛↉བϟ˖ ೒ 3.21 struts2-core-2.0.6.jar य़㓽᭛ӊⱘ᭛ӊ㒧ᵘ ⳟࠄ᳝ϔϾ struts-default.xml ᭛ӊDŽ ᷛ䗝Ёⱘϔ㸠ˈܝ㓽䕃ӊᠧᓔ struts2-core-2.0.6.jar ᭛ӊˈⳟࠄབ೒ 3.21 ᠔⼎ⱘ᭛ӊ㒧ᵘˈ೼ 䰸ℸП໪ˈStruts 2 䖬ᦤկњϔ⾡ᦦӊᓣⱘᮍᓣᴹㅵ⧚䜡㕂᭛ӊDŽ⫼ WinRAR ㄝ㾷य़ 䗮䖛䖭⾡ᮍᓣˈStruts 2 ᦤկњϔ⾡῵ഫ࣪ⱘᮍᓣᴹㅵ⧚ struts.xml 䜡㕂᭛ӊDŽ ... ㋴ᇐܹ݊Ҫ䜡㕂᭛ӊ -->ܗ ܗ ӊˈ䗮䖛䖭⾡ᮍᓣˈህৃҹᇚ Struts 2 ⱘ Action ᣝ῵ഫ䜡㕂೼໮Ͼ䜡㕂᭛ӊЁDŽ ϟ䴶ⱘ struts.xml ᭛ӊЁህ䗮䖛 include ᠟ࡼᇐܹњϔϾ䜡㕂᭛ӊ˖struts-part1.xml ᭛ ㄀ 3 ゴ Struts 2 ෎⸔ 78 ㋴᳝ܗ ࡼࡴ䕑䆹᭛ӊDŽᶹⳟ៥Ӏࠡ䴶Փ⫼ⱘ struts.xml ᭛ӊˈⳟࠄ៥Ӏ㞾ᏅᅮНⱘ package 䖭Ͼ struts-default.xml ᭛ӊᰃ Struts 2 Ḛᶊⱘ咬䅸䜡㕂᭛ӊˈStruts 2 Ḛᶊ↣⃵䛑Ӯ㞾 ӊⱘ᳔ৢ䖬ᅮНњ咬䅸ⱘᢺ៾఼ᓩ⫼DŽ Result ㉏ൟˈ䖬ᅮНњ Struts 2 ݙᓎⱘ㋏߫ᢺ៾఼ˈҹঞ⬅ϡৠᢺ៾఼㒘៤ⱘᢺ៾఼ᷜˈ᭛ 䜡㕂᭛ӊЁᅮНњϔϾৡᄫЎ struts-default ⱘࣙぎ䯈ˈ䆹ࣙぎ䯈䞠ᅮНњ Struts 2 ݙᓎⱘ ㋴ⱘҷ㸼DŽϞ䴶ܗϞ䴶ⱘҷⷕᑊ᳾ܼ䚼߫ߎ struts-default.xml ᭛ӊˈাᰃ߫ߎњ↣Ͼ ... ... ActionAutowiringInterceptor"/> class="com.opensymphony.xwork2.spring.interceptor. class="org.apache.struts2.views.freemarker. DispatcherResult" default="true"/> class="org.apache.struts2.dispatcher.Servlet ... Struts 2 ᴗ࿕ᣛफüü෎Ѣ WebWork Ḍᖗⱘ MVC ᓔথ 79 ೼ WEB-INF/lib 䏃ᕘϟˈStruts 2 Ḛᶊᇚ㞾ࡼࡴ䕑䆹᭛ӊDŽ гᰃ Struts 2 ḚᶊՓ⫼ Spring ᦦӊᖙ䳔ⱘ䜡㕂DŽা㽕ᇚ struts2-spring-plugin2.06.jar ᭛ӊᬒ ೼Ϟ䴶䜡㕂᭛ӊЁˈ䜡㕂њ Struts 2 Ϣ Spring Ḛᶊᭈড়ᖙ䳔ⱘᐌ䞣ǃᢺ៾఼ㄝüü䖭 SessionContextAutowiringInterceptor"/> class="org.apache.struts2.spring.interceptor. class="com.opensymphony.xwork2.spring.interceptor. class="org.apache.struts2.spring.StrutsSpringObjectFactory" /> "http://struts.apache.org/dtds/struts-2.0.dtd"> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ᭛ӊˈᠧᓔ䆹᭛ӊˈ䆹᭛ӊⱘҷⷕབϟ˖ Փ⫼ WinRAR Ꮉ݋ᠧᓔ struts2-spring-plugin2.06.jar ᭛ӊˈᡒࠄϔϾ struts-plugin.xml ᑨ⫼ⱘ WEB-INF/lib 䏃ᕘϟˈStruts 2 Ḛᶊᇚ㞾ࡼࡴ䕑䆹ḚᶊDŽ Ԑ struts2-Xxx-plugin.jar ⱘ᭛ӊüü䖭Ͼ᭛ӊህᰃᦦӊᅝ㺙᭛ӊˈা㽕ᇚ䆹᭛ӊᬒ೼ Web ⱘᮍᓣᴹᅝ㺙ᦦӊˈ՟བৢ䴶ᇚ㽕ҟ㒡ⱘ Spring ᦦӊǃJSF ᦦӊㄝˈᅗӀ䛑ᦤկњϔϾ㉏ 䆌ҹϔ⾡ĀৃᦦᢨāܕϡҙབℸˈStruts 2 Ḛᶊᦤկњϔ⾡㉏Ԑ Eclipse ⱘᠽሩᮍᓣˈᅗ ᭛ӊDŽ ࣙぎ䯈ᅮН೼ struts-default.xml ᭛ӊЁDŽৃ㾕ˈStruts 2 Ḛᶊ咬䅸Ӯࡴ䕑 struts-default.xml ೼Ϟ䴶䜡㕂᭛ӊЁˈৡЎ lee ⱘࣙぎ䯈ˈ㒻ᡓњৡЎ struts-default ⱘࣙぎ䯈ˈstruts-default ... ㋴ -->ܗܗ ܗ ܗ㋴ᰃ Struts 䜡㕂᭛ӊⱘḌᖗˈ䆹ܗ ܗ ܗ ܗ ܗ 䖭Ͼ᭛ӊ≵᳝ӏԩᅲ䰙ᛣНˈҙҙᰃϔϾ struts.xml ᭛ӊ⼎㣗DŽ struts.xml ᭛ӊᰃᭈϾ Struts 2 ḚᶊⱘḌᖗˈϟ䴶ᦤկњϔϾ᳔ᅠᭈⱘ struts.xml ᭛ӊˈ 3.5.4 struts.xml ᭛ӊ㒧ᵘ ㋴ᴹ䜡㕂 Struts 2 ሲᗻDŽܗ constant struts.properties ᭛ӊᴹ䜡㕂 Struts 2 ሲᗻˈгৃ䗮䖛೼ struts.xml ᭛ӊЁ䜡㕂 ㌑䡻 Struts 2 ᦤկњϸ⾡ᮍᓣᴹㅵ⧚ Struts 2 ⱘሲᗻ˖᮶ৃҹ䗮䖛 䳔ⱘ೑䰙࣪䌘⑤᭛ӊⱘ baseName Ў messDŽ Ϟ䴶ҷⷕ⠛↉䜡㕂њϔϾᐌ⫼ሲᗻ˖struts.custom.i18n.resourcesˈ䆹ሲᗻᣛᅮњᑨ⫼᠔ ... ㋴䜡㕂 Struts 2 ⱘሲᗻ -->ܗ struts.xml 䜡㕂⠛↉བϟ˖ ሲᗻDŽ ㋴ˈϔḋৃҹ䜡㕂䖭ѯܗ ᭛ӊЁㅵ⧚ Struts 2 ሲᗻˈ೼ struts.xml ᭛ӊЁ䗮䖛䜡㕂 constant 䆌೼ struts.xmlܕ ೼᳝ѯᯊ׭ˈᓔথ㗙ϡ୰⃶Փ⫼乱໪ⱘ struts.properties ᭛ӊˈStruts 2 ㄀ 3 ゴ Struts 2 ෎⸔ 84 খ᭄ؐ* ㋴ৃҹߎ⦄ 0 ⃵ˈгৃҹ᮴䰤໮⃵ -->ܗ ܗ ܗ ܗ ㋴ᖙ乏ߎ⦄ˈৃҹߎ⦄᮴䰤໮⃵-->ܗ ܗ ܗ ܗ ܗ ܗ ܗ ܗ ܗ ܗ ܗ ㋴੠ interceptor-stack 㟇ᇥߎ⦄݊ЁПϔˈܗ ㋴ⱘ interceptorܗ ܗ ܗ "http://struts.apache.org/dtds/struts-2.0.dtd"> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" * খ᭄ؐ* ㋴ৃҹߎ⦄ 0 ⃵ˈгৃҹ᮴䰤໮⃵ -->ܗ ܗ ܗ ܗ ܗ ܗ ܗ

      pdf贡献者

      velocity123

      贡献于2010-08-28

      下载需要 20 金币 [金币充值 ]
      亲,您也可以通过 分享原创pdf 来获得金币奖励!