{"id":898,"date":"2010-10-01T12:31:34","date_gmt":"2010-10-01T12:31:34","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=898"},"modified":"2010-10-01T12:31:34","modified_gmt":"2010-10-01T12:31:34","slug":"struts2-0%e4%b8%8espring%e7%9a%84%e6%95%b4%e5%90%88","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2010\/10\/01\/struts2-0%e4%b8%8espring%e7%9a%84%e6%95%b4%e5%90%88\/","title":{"rendered":"Struts2.0\u4e0eSpring\u7684\u6574\u5408"},"content":{"rendered":"<p>\u6ce8\uff1a\u8981\u4e0eSpring\u6574\u5408\uff0c\u8ba9Spring\u6258\u7ba1Struts\u7684Action,\u90a3\u4e48\u9700\u8981\u6dfb\u52a0Struts2.0\u7684<br \/>\n????? struts2-spring-plugin-2.1.6.jar<br \/>\n\u6ce8\u5165\u6548\u679c\uff1aaction\u7684\u5c5e\u6027\u5bf9\u8c61\u4f1a\u81ea\u52a8\u88abSpring\u521b\u5efa\uff0c\u5728action\u4e2d\u53ea\u9700\u5f15\u7528\u5373\u53ef<\/p>\n<p>1,\u6dfb\u52a0Spring\u80fd\u529b<br \/>\n\u9009\u4e2d\u4ee5\u4e0b\u53ca\u81ea\u52a8\u5173\u8054\u7684\u7c7b\u5e93<br \/>\nString 2.0 Core Libraries<br \/>\nString 2.0 Persistence JDBC Libraries<br \/>\nString 2.0 Web Libraries<\/p>\n<p>2, \u5728web.xml\u6dfb\u52a0spring\u76d1\u542c\u5668<br \/>\n&lt;listener&gt;<br \/>\n&lt;listener-class&gt;<br \/>\n??? org.springframework.web.context.ContextLoaderListener<br \/>\n&lt;\/listener-class&gt;<br \/>\n&lt;\/listener&gt;<\/p>\n<p>\u6ce8\uff1a\u82e5\u4e0d\u7528Spring\u6765\u4e3aStruts\u7684action\u8fdb\u884c\u81ea\u52a8\u5c5e\u6027\u6ce8\u5165\uff0c\u90a3\u4e48\u5c31\u4e0d\u5fc5\u4e3aspring\u914d\u7f6e\u76d1\u542c\u5668\uff0c\u4e14\u4e5f\u4e0d\u7528\u5bfc\u5165<br \/>\nstruts2-spring-plugin-2.1.6.jar\uff0c \u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u8fdb\u884c\u624b\u52a8\u6ce8\u5165\uff1a<br \/>\n?????????? ApplicationContext context = new ClassPathXmlApplicationContext( &#8220;applicationContext.xml&#8221; );<br \/>\n????????? ComeAction comeAction = (ComeAction) context.getBean( &#8220;comeAction&#8221; );<br \/>\n\u53ef\u770b\u51fa\u5728web.xml\u914d\u7f6e\u7684\u591a\u4e3astruts\u4e0eservlet,\u56e0\u4e3a\u5b83\u4eec\u8981\u62e6\u622a\u9875\u9762\u8bf7\u6c42\u7684url\u8fdb\u884c\u8f6c\u5411\uff0c\u800c\u914d\u7f6e<br \/>\n?? Spring\u76d1\u542c\u5668\u7684\u539f\u56e0\u662f\u4e3a\u4e86\u76d1\u542cStruts action\u7684\u5efa\u7acb\u5e76\u4e3a\u5176\u52a8\u6001\u6ce8\u5165\u5c5e\u6027\u3002<\/p>\n<p>3, \u5c06applicationContext.xml\u653e\u7f6e\u5728web-inf\u4e0b\uff0c \u540d\u5b57\u53ef\u4efb\u610f\u5982ac.xml,ab.xml<br \/>\n\u4e0d\u8fc7\u8981\u5728web.xml\u6307\u5b9a??<br \/>\n&lt;context-param&gt;<br \/>\n&lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt;<br \/>\n&lt;param-value&gt;\/WEB-INF\/a*.xml &lt;\/param-value&gt;<br \/>\n&lt;\/context-param&gt;<br \/>\n\u6ce8\uff1a&lt;context-param&gt;\u4e5f\u53ef\u4e0d\u52a0\uff0c\u9ed8\u8ba4\u8bfb\u53d6\u7684\u662f\/WEB-INF\/\u4e0b\u7684applicationContext.xml,\u53ea\u8981?????? applicationContext.xml\u653e\u7f6e\u5728web-inf\u4e0b\u5373\u53ef\u3002<\/p>\n<p>4, \u914d\u7f6e applicationContext.xml, \u521b\u5efa\u76f8\u5e94\u7684\u6ce8\u5165\u5c5e\u6027<br \/>\n\u5982\uff1aloginAction\u6709\u4e00\u4e2auserDao\u5c5e\u6027\uff0c\u8bbe\u7f6e\u4e86get,set\u65b9\u6cd5\uff0c\u90a3\u4e48\u5728 applicationContext.xml,\u914d\u7f6e\uff1a<br \/>\n??? &lt;bean id=&#8221;userDao&#8221;&gt;<br \/>\n???? &lt;\/bean&gt;<br \/>\n\u6ce8\uff1a1,loginAction\u4e2d\u7684\u5c5e\u6027\u540duserDao\u4e00\u5b9a\u8981\u4e0eapplicationContext.xml\u4e2d\u7684bean\u7684id\u76f8\u540c\uff01\uff01\uff01\uff01\uff01<br \/>\n\u8fd9\u6837Spring\u624d\u80fd\u81ea\u52a8\u6ce8\u5165action\u7684\u5c5e\u6027\u3002\u6307\u7684\u662f\u4e0esetUserDao()\u4e2duserDao\u76f8\u540c\uff0c\u53d8\u91cf\u540d\u4e0d\u5fc5\u8981\u76f8\u540c\u3002<br \/>\n\u56e0\u4e3a\u5728\u6ce8\u5165\u65f6\uff0c\u4f1a\u6839\u636e\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684userDao\u8c03\u7528action\u7684setUserDao\u65b9\u6cd5\uff0c\u53ea\u8981\u6b64\u65b9\u6cd5\u5b58\u5728\u5373\u53ef\uff01\uff01<br \/>\n???? ( \u4ee5\u4e0b2\uff0c3\u53ef\u4e0d\u7528\u914d )<br \/>\n?????? 2, \u5728struts.properties\u4e2d\u8bbe\u7f6estruts.objectFactory\u5c5e\u6027\u503c<br \/>\n?????????? struts.objectFactory = spring?<br \/>\n????????? \u6216\u8005\u662f\u5728XML\u6587\u4ef6\u4e2d\u8fdb\u884c\u5e38\u91cf\u914d\u7f6e<br \/>\n?????????? struts.xml???? &lt;constant name=&#8221;struts.objectFactory&#8221; value=&#8221;spring&#8221; \/&gt;?<br \/>\n???? 3, \u5728struts.properties\u4e2d\u6307\u5b9a\u6ce8\u5165\u65b9\u5f0f\u3002name \u6309\u7167\u4f60\u7684action\u7684\u5c5e\u6027\u7684\u540d\u5b57\u548cSpring\u91cc\u7684bean\u7684\u540d\u5b57\u5339\u914d\uff0c\u5982\u679c\u5339\u914d\u5c31\u81ea\u52a8\u88c5\u914d\u3002\u8fd9\u662f\u7f3a\u7701\u7684<br \/>\ntype \u6309\u7167\u4f60\u7684action\u7684\u5c5e\u6027\u7684\u7c7b\u578b\uff0c\u5728Spring\u6ce8\u518c\u7684bean\u4e2d\u67e5\u627e\uff0c\u5982\u679c\u76f8\u540c\u5c31\u81ea\u52a8\u88c5\u914d\u3002\u8fd9\u9700\u8981\u4f60\u5728Spring\u4e2d\u4ec5\u6ce8\u518c\u4e86\u4e00\u4e2a\u6b64\u7c7b\u578b\u7684bean<br \/>\nauto Spring\u4f1a\u8bd5\u56fe\u81ea\u52a8\u76d1\u6d4b\u6765\u627e\u5230\u6700\u597d\u7684\u65b9\u6cd5\u81ea\u52a8\u88c5\u914d\u4f60\u7684action<br \/>\nconstructor Spring\u4f1a\u81ea\u52a8\u88c5\u914dbean\u7684\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570<br \/>\nps :<br \/>\n\u5728action\u7684class\u6307\u5b9a\u5177\u4f53\u7684\u7c7b,Spring\u81ea\u52a8\u5b8c\u6210\u5c5e\u6027\u7684\u6ce8\u5165,<br \/>\n\u5728action\u7684class\u6307\u5b9abean id, Spring\u6258\u7ba1\u6574\u4e2aaction\u751f\u547d\u5468\u671f.<br \/>\n1, \u5c5e\u6027\u6ce8\u5165:<br \/>\n(1) struts.xml\u4e2d\u914d\u7f6e:<br \/>\n?&lt;action name=&#8221;login&#8221; method=&#8221;login&#8221;&gt;???<br \/>\n&lt;result name=&#8221;success&#8221; type=&#8221;redirect&#8221;&gt;success.jsp&lt;\/result&gt;<br \/>\n&lt;\/action&gt;<br \/>\n(2) applicationContext.xml\u4e2d\u914d\u7f6e<br \/>\n?\u5728applicationContext.xml\u4e2d\u914d\u7f6e\u6b64action\u7684\u5c5e\u6027,\u8981\u6ce8\u610f\u7684\u662fbean id \u8981\u4e0eaction\u5185\u7684\u5c5e\u6027\u540d\u76f8\u540c,\u4e0d\u7528\u4e3a???????????????? LoginAction\u672c\u8eab\u5b9a\u4e49bean id.<br \/>\n\u8fd9\u6837Spring\u5c31\u53ea\u8d1f\u8d23action\u5c5e\u6027\u7684\u6ce8\u5165,<\/p>\n<p>2, \u6240\u6709\u6ce8\u5165:?? \u8ba9spring\u4e0d\u4ec5\u8d1f\u8d23action\u5c5e\u6027\u7684\u521b\u5efa,\u8fd8\u8d1f\u8d23action\u672c\u8eab\u7684\u521b\u5efa.<br \/>\n? (1) struts.xml\u4e2d\u914d\u7f6e:??<br \/>\n?? struts.xml\u4e2d\u914d\u7f6e:<br \/>\n? &lt;action name=&#8221;login&#8221; method=&#8221;login&#8221;&gt;???<br \/>\n&lt;result name=&#8221;success&#8221; type=&#8221;redirect&#8221;&gt;success.jsp&lt;\/result&gt;<br \/>\n?&lt;\/action&gt;<br \/>\n? class\u4e3aSpring\u4e2dapplicationContext.xml\u4e2d\u7684bean id<br \/>\n?(2) applicationContext.xml\u4e2d\u914d\u7f6e<br \/>\n??? \u914d\u7f6eloginAction\u7684bean\u5373\u53ef.<br \/>\n\u6240\u6709\u6ce8\u5165\u7684\u597d\u5904: \u7ed3\u5408aop,\u53ef\u5b8c\u6210\u5bf9action\u6267\u884c\u5177\u4f53\u65b9\u6cd5\u65f6\u8fdb\u884c\u62e6\u622a,\u8fdb\u884c\u65e5\u5fd7\u8bb0\u5f55\u7b49.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u8981\u4e0eSpring\u6574\u5408\uff0c\u8ba9Spring\u6258\u7ba1Struts\u7684Action,\u90a3\u4e48\u9700\u8981\u6dfb\u52a0Struts2.0\u7684 ?? [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-898","post","type-post","status-publish","format-standard","hentry","category-struts2-0"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/comments?post=898"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/898\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}