{"id":1832,"date":"2016-12-27T15:01:48","date_gmt":"2016-12-27T07:01:48","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1832"},"modified":"2020-05-08T20:10:32","modified_gmt":"2020-05-08T12:10:32","slug":"%e5%88%9d%e8%af%86struts2","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1832","title":{"rendered":"\u521d\u8bc6Struts2"},"content":{"rendered":"<h2>\u4e00\u3001struts2\u4ecb\u7ecd<\/h2>\n<p>Apache Struts2 \u662f\u6d41\u884c\u548c\u6210\u719f\u7684\u57fa\u4e8eMVC\u8bbe\u8ba1\u6a21\u5f0f\u7684Web\u5e94\u7528\u7a0b\u5e8f\u6846\u67b6\u3002\u5b83\u6210\u529f\u5730\u7ed3\u5408\u4e86 WebWork\u548cStruts1.x \u4e24\u79cd web \u6846\u67b6\u3002\u4f7f\u7528OGNL\u8868\u8fbe\u5f0f\u548cStruts2\u6807\u7b7e\u6765\u89e3\u51b3\u5e94\u7528\u7a0b\u5e8f\u6570\u636e\u3002\u901a\u8fc7Struts2\u53ef\u4ee5\u51cf\u5c11\u4f7f\u7528MVC\u6a21\u5f0f\u5f00\u53d1Web\u5e94\u7528\u7a0b\u5e8f\u65f6\u95f4.<\/p>\n<h2>\u4e8c\u3001\u7b2c\u4e00\u4e2astruts2\u7a0b\u5e8f<\/h2>\n<h3>1.\u4e0b\u8f7dstruts2\u76f8\u5173jar\u5305.<\/h3>\n<p>\u5730\u5740:http:\/\/struts.apache.org\/<\/p>\n<h3>2.struts2\u5f15\u5165\u6700\u5c11\u5305,\u5982\u4e0b(\u82e5\u7f3a\u5c11\u4f1a\u62a5\u9519)<\/h3>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/12\/struts2.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1835\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/12\/struts2.png\" alt=\"struts2\" width=\"229\" height=\"161\" \/><\/a><\/p>\n<h3>3.\u5b9e\u73b0Action\u7c7b<\/h3>\n<p>[java]import com.opensymphony.xwork2.ActionSupport;<\/p>\n<p>public class UserAction extends ActionSupport {<br \/>\n\tprivate Logger log = Logger.getLogger(UserAction.class);<br \/>\n\t@Override<br \/>\n\tpublic String execute() throws Exception {<\/p>\n<p>\t\t\/*<br \/>\n\t\t *  \u63a5\u6536\u8bf7\u6c42<br \/>\n\t\t *  \u6267\u884c\u4e1a\u52a1\u5904\u7406<br \/>\n\t\t *  \u8fd4\u56de\u54cd\u5e94<br \/>\n\t\t *\/<br \/>\n\t\tlog.debug(&quot;\u6267\u884c\u6dfb\u52a0\u7528\u6237\u64cd\u4f5c &gt;&gt;&gt;&gt;&gt;&gt;&quot;);<\/p>\n<p>\t\treturn SUCCESS;<br \/>\n\t}<br \/>\n}[\/java]<\/p>\n<h3>4.\u6838\u5fc3\u914d\u7f6e\u6587\u4ef6struts.xml<\/h3>\n<p>[java]   &lt;!DOCTYPE struts PUBLIC<br \/>\n    &quot;-\/\/Apache Software Foundation\/\/DTD Struts Configuration 2.5\/\/EN&quot;<br \/>\n    &quot;http:\/\/struts.apache.org\/dtds\/struts-2.5.dtd&quot;&gt;<br \/>\n&lt;struts&gt;<br \/>\n\t&lt;package name=&quot;student&quot; namespace=&quot;\/&quot; extends=&quot;struts-default&quot;&gt;<\/p>\n<p>\t\t&lt;action name=&quot;user&quot; class=&quot;com.ittx.strutsproject.action.UserAction&quot;&gt;<br \/>\n\t\t\t&lt;result name=&quot;success&quot;&gt;\/sucess.jsp&lt;\/result&gt;<br \/>\n\t\t&lt;\/action&gt;<\/p>\n<p>\t&lt;\/package&gt;<br \/>\n&lt;\/struts&gt;[\/java]<\/p>\n<h3>5.\u914d\u7f6eweb.xml<\/h3>\n<p>[java]  &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br \/>\n&lt;web-app xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;<br \/>\n\txmlns=&quot;http:\/\/java.sun.com\/xml\/ns\/javaee&quot;<br \/>\n\txsi:schemaLocation=&quot;http:\/\/java.sun.com\/xml\/ns\/javaee http:\/\/java.sun.com\/xml\/ns\/javaee\/web-app_3_0.xsd&quot;<br \/>\n\tid=&quot;WebApp_ID&quot; version=&quot;3.0&quot;&gt;<br \/>\n\t&lt;display-name&gt;strutsdemo&lt;\/display-name&gt;<br \/>\n\t&lt;filter&gt;<br \/>\n\t\t&lt;filter-name&gt;struts2&lt;\/filter-name&gt;<br \/>\n\t\t  &lt;filter-class&gt;org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter&lt;\/filter-class&gt;<br \/>\n\t&lt;\/filter&gt;<\/p>\n<p>\t&lt;filter-mapping&gt;<br \/>\n\t\t&lt;filter-name&gt;struts2&lt;\/filter-name&gt;<br \/>\n\t\t&lt;url-pattern&gt;\/*&lt;\/url-pattern&gt;<br \/>\n\t&lt;\/filter-mapping&gt;<\/p>\n<p>\t&lt;welcome-file-list&gt;<br \/>\n\t\t&lt;welcome-file&gt;login.jsp&lt;\/welcome-file&gt;<br \/>\n\t&lt;\/welcome-file-list&gt;<br \/>\n&lt;\/web-app&gt;[\/java]<\/p>\n<h3>6. \u6267\u884c\u8bf7\u6c42<\/h3>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/12\/result1.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1838\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/12\/result1.png\" alt=\"result1\" width=\"376\" height=\"90\" \/><\/a><\/p>\n<h3>\u4e09\u3001struts2\u5de5\u4f5c\u539f\u7406<\/h3>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/12\/s1.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1841\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/12\/s1.png\" alt=\"s1\" width=\"605\" height=\"646\" \/><\/a><\/p>\n<p>\u4e00\u4e2a\u8bf7\u6c42\u5728Struts2\u6846\u67b6\u4e2d\u7684\u5904\u7406\u5927\u6982\u5206\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4(\u53ef\u67e5\u770b\u6e90\u7801\uff1ahttps:\/\/github.com\/apache\/struts):<\/p>\n<p>[java]1 \u5ba2\u6237\u7aef\u521d\u59cb\u5316\u4e00\u4e2a\u6307\u5411Servlet\u5bb9\u5668\uff08\u4f8b\u5982Tomcat\uff09\u7684\u8bf7\u6c42<br \/>\n2 \u8fd9\u4e2a\u8bf7\u6c42\u7ecf\u8fc7\u4e00\u7cfb\u5217\u7684\u8fc7\u6ee4\u5668\uff08Filter\uff09\uff08\u8fd9\u4e9b\u8fc7\u6ee4\u5668\u4e2d\u6709\u4e00\u4e2a\u53eb\u505aActionContextCleanUp\u7684\u53ef\u9009\u8fc7\u6ee4\u5668\uff0c\u8fd9\u4e2a\u8fc7\u6ee4\u5668\u5bf9\u4e8eStruts2\u548c\u5176\u4ed6\u6846\u67b6\u7684\u96c6\u6210\u5f88\u6709\u5e2e\u52a9\uff0c\u4f8b\u5982\uff1aSiteMesh Plugin\uff09\u00a0<br \/>\n3 \u63a5\u7740StrutsPrepareAndExecuteFilter\u88ab\u8c03\u7528\uff0cStrutsPrepareAndExecuteFilter\u8be2\u95eeActionMapper\u6765\u51b3\u5b9a\u8fd9\u4e2a\u8bf7\u662f\u5426\u9700\u8981\u8c03\u7528\u67d0\u4e2aAction<br \/>\n4 \u5982\u679cActionMapper\u51b3\u5b9a\u9700\u8981\u8c03\u7528\u67d0\u4e2aAction\uff0cStrutsPrepareAndExecuteFilter\u628a\u8bf7\u6c42\u7684\u5904\u7406\u4ea4\u7ed9ActionProxy<br \/>\n5 ActionProxy\u901a\u8fc7Configuration Manager\u8be2\u95ee\u6846\u67b6\u7684\u914d\u7f6e\u6587\u4ef6struts.xml\uff0c\u627e\u5230\u9700\u8981\u8c03\u7528\u7684Action\u7c7b\u00a0<br \/>\n6 ActionProxy\u521b\u5efa\u4e00\u4e2aActionInvocation\u7684\u5b9e\u4f8b\u3002<br \/>\n7 ActionInvocation\u5b9e\u4f8b\u4f7f\u7528\u547d\u540d\u6a21\u5f0f\u6765\u8c03\u7528\uff0c\u5728\u8c03\u7528Action\u7684\u8fc7\u7a0b\u524d\u540e\uff0c\u6d89\u53ca\u5230\u76f8\u5173\u62e6\u622a\u5668\uff08Intercepter\uff09\u7684\u8c03\u7528\u3002\u00a0<br \/>\n8 \u4e00\u65e6Action\u6267\u884c\u5b8c\u6bd5\uff0cActionInvocation\u8d1f\u8d23\u6839\u636estruts.xml\u4e2d\u7684\u914d\u7f6e\u627e\u5230\u5bf9\u5e94\u7684\u8fd4\u56de\u7ed3\u679c\u3002\u8fd4\u56de\u7ed3\u679c\u901a\u5e38\u662f\uff08\u4f46\u4e0d\u603b\u662f\uff0c\u4e5f\u53ef\u80fd\u662f\u53e6\u5916\u7684\u4e00\u4e2aAction\u94fe\uff09\u4e00\u4e2a\u9700\u8981\u88ab\u8868\u793a\u7684JSP\u6216\u8005FreeMarker\u7684\u6a21\u7248\u3002\u5728\u8868\u793a\u7684\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u4f7f\u7528Struts2 \u6846\u67b6\u4e2d\u7ee7\u627f\u7684\u6807\u7b7e\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u9700\u8981\u6d89\u53ca\u5230ActionMapper[\/java]<\/p>\n<p>\u5728\u4e0a\u8ff0\u8fc7\u7a0b\u4e2d\u6240\u6709\u7684\u5bf9\u8c61\uff08Action\uff0cResults\uff0cInterceptors\uff0c\u7b49\uff09\u90fd\u662f\u901a\u8fc7ObjectFactory\u6765\u521b\u5efa\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001struts2\u4ecb\u7ecd Apache Struts2 \u662f\u6d41\u884c\u548c\u6210\u719f\u7684\u57fa\u4e8eMVC\u8bbe\u8ba1\u6a21\u5f0f\u7684Web\u5e94\u7528\u7a0b\u5e8f\u6846\u67b6\u3002\u5b83 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[59,87],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1832"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1832"}],"version-history":[{"count":10,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1832\/revisions"}],"predecessor-version":[{"id":2939,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1832\/revisions\/2939"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}