{"id":902,"date":"2010-10-01T12:33:22","date_gmt":"2010-10-01T12:33:22","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=902"},"modified":"2010-10-01T12:33:22","modified_gmt":"2010-10-01T12:33:22","slug":"struts2%e4%b8%ad%e5%b8%b8%e7%94%a8result%e7%b1%bb%e5%9e%8b%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2010\/10\/01\/struts2%e4%b8%ad%e5%b8%b8%e7%94%a8result%e7%b1%bb%e5%9e%8b%e7%9a%84%e7%94%a8%e6%b3%95\/","title":{"rendered":"struts2\u4e2d\u5e38\u7528Result\u7c7b\u578b\u7684\u7528\u6cd5"},"content":{"rendered":"<p>1, dispatcher -forward \u8f6c\u5230\u6307\u5b9a\u7684jsp\u9875\u9762<br \/>\n2,redirect    \u91cd\u5b9a\u5411\u5230jsp\u9875\u9762\u6216action<br \/>\n3,chain       \u7531\u5f53\u524daction\u8f6c\u5230\u53e6\u4e00action, \u5f62\u6210\u4e00\u4e2aaction\u94fe\uff0c\u5f7c\u4e9b\u95f4\u5171\u4eab\u4e00\u4e2aActionContext,\u5728\u4e00\u4e2aaction\u4e2d\u8bbe\u7f6eActionContext\u5c5e\u6027\uff0c\u53ef\u88ab\u5176\u5b83action\u5171\u4eab\u3002<\/p>\n<p>redirect \u4e0e dispatcher\u7684\u533a\u522b\u4e0e reidrect\uff0cforward\u76f8\u4f3c<\/p>\n<p>\u4e00\u3001dispatcher<\/p>\n<p>\uff081\uff09\u4e3a\u7f3a\u7701\u7684result\u7c7b\u578b\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u6211\u4eec\u5728struts.xml\u4f1a\u8fd9\u4e48\u5199\uff1a<br \/>\nXml\u4ee3\u7801<\/p>\n<p>1. <result name=\"success\">\/main.jsp<\/result>  <\/p>\n<p><result name=\"success\">\/main.jsp<\/result><\/p>\n<p>\u4ee5\u4e0a\u5199\u6cd5\u4f7f\u7528\u4e86\u4e24\u4e2a\u9ed8\u8ba4\uff0c\u5176\u5b8c\u6574\u7684\u5199\u6cd5\u4e3a\uff1a<br \/>\nXml\u4ee3\u7801<\/p>\n<p>1. <result name=\"success\" type=\"dispatcher\"><br \/>\n2.         <param name=\"location\">\/maini.jsp<\/param>\n3. <\/result>  <\/p>\n<p><result name=\"success\" type=\"dispatcher\">\n<param name=\"location\">\/maini.jsp<\/param>\n<\/result><\/p>\n<p>\u7b2c\u4e00\u4e2a\u9ed8\u8ba4\uff1atype=&#8221;dispatcher&#8221;\uff1b\u7b2c\u4e8c\u4e2a\u9ed8\u8ba4\uff1a\u8bbe\u7f6e\u7684\u4e3alocation\u53c2\u6570\uff0clocation\u53ea\u80fd\u662f\u9875\u9762\uff0c\u4e0d\u80fd\u662f\u53e6\u4e00\u4e2aaction\uff08\u53ef\u7528type=&#8221;chain&#8221;\u89e3\u51b3\uff09\u3002<\/p>\n<p>\uff082\uff09\u5b9e\u73b0\u65b9\u5f0f<\/p>\n<p>\u4ecedoExecute\u65b9\u6cd5\u770b\u51fa\uff0c\u6709\u4e09\u4e2a\u51fa\u53e3\uff08finalLocation\u4e3a\u8981\u8df3\u8f6c\u7684\u5730\u5740\uff09\uff1a<\/p>\n<p>*<\/p>\n<p>  pageContext.include(finalLocation);<br \/>\n*<\/p>\n<p>  dispatcher.forward(request, response); (dispatcher\u662f\u6839\u636efinalLocation\u521b\u5efa\u7684)<br \/>\n*<\/p>\n<p>  dispatcher.include(request, response);<\/p>\n<p>\u800c\u6211\u4eec\u77e5\u9053\uff0cforward\u4e0einclude\u90fd\u662f\u8f6c\u53d1\u5230context\u5185\u90e8\u7684\u8d44\u6e90\u3002<\/p>\n<p>\u4e8c\u3001redirect<\/p>\n<p>\uff081\uff09\u53ef\u4ee5\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a\u9875\u9762\uff0c\u53e6\u4e00\u4e2aaction\u6216\u4e00\u4e2a\u7f51\u5740\u3002<br \/>\nXml\u4ee3\u7801<\/p>\n<p>1. <result name=\"success\" type=\"redirect\">aaa.jsp<\/result><br \/>\n2. <result name=\"success\" type=\"redirect\">bbb.action<\/result><br \/>\n3. <result name=\"success\" type=\"redirect\">www.baidu.com<\/result>  <\/p>\n<p><result name=\"success\" type=\"redirect\">aaa.jsp<\/result><br \/>\n<result name=\"success\" type=\"redirect\">bbb.action<\/result><br \/>\n<result name=\"success\" type=\"redirect\">www.baidu.com<\/result><\/p>\n<p>\uff082\uff09\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n<p>\u67e5\u770bdoExecute\u65b9\u6cd5\uff0c\u53ea\u6709\u4e00\u4e2a\u51fa\u53e3\uff1a<\/p>\n<p>response.sendRedirect(finalLocation);<\/p>\n<p>sendRedirect\u662f\u91cd\u5b9a\u5411\uff0c\u662f\u91cd\u65b0\u4ea7\u751f\u4e00\u4e2aHTTP\u8bf7\u6c42\u5230\u670d\u52a1\u5668\uff0c\u6545\u91cd\u5b9a\u5411\u540e\u5176\u539f\u6765\u6240\u5728\u7684action\u4e0a\u4e0b\u6587\u5c31\u4e0d\u53ef\u7528\u4e86\u3002<\/p>\n<p>\u4e09\u3001chain<\/p>\n<p>\uff081\uff09\u4e3b\u8981\u7528\u4e8e\u628a\u76f8\u5173\u7684\u51e0\u4e2aaction\u8fde\u63a5\u8d77\u6765\uff0c\u5171\u540c\u5b8c\u6210\u4e00\u4e2a\u529f\u80fd\u3002<br \/>\nXml\u4ee3\u7801<\/p>\n<p><action name=\"step1\" class=\"test.Step1Action\"><br \/>\n<result name=\"success\" type=\"chain\">step2<\/result>  \/\/\u6ce8\uff1a\u662fstep2\u800c\u4e0d\u662fstep2.action<br \/>\n<\/action><\/p>\n<p><action name=\"step2\" class=\"test.Step2Action\"><br \/>\n<result name=\"success\">finish.jsp<\/result><br \/>\n<\/action><\/p>\n<p>\uff082\uff09\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n<p>\u67e5\u770bexecute()\u65b9\u6cd5\uff0c\u4e3b\u8981\u601d\u60f3\u5982\u4e0b\uff1a<\/p>\n<p>\/\/ \u6839\u636eAction\u540d\u79f0finalActionName\u53ca\u8981\u8c03\u7528\u7684\u65b9\u6cd5finalMethodName\u6765new\u4e00\u4e2a\u4ee3\u7406\u5bf9\u8c61proxy\uff0c\u5e76\u6267\u884c\u4e4b<br \/>\nJava\u4ee3\u7801<\/p>\n<p>proxy = actionProxyFactory.createActionProxy(finalNamespace,<br \/>\n          finalActionName, finalMethodName, extraContext);<br \/>\nproxy.execute();<\/p>\n<p>\uff083\uff09\u591a\u4e2aaction\u95f4\u6570\u636e\u7684\u4f20\u9012<\/p>\n<p>\u4e3b\u8981\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n<p>1\uff09\u7531\u4e8e\u5904\u4e8echain\u4e2d\u7684action\u5c5e\u4e8e\u540c\u4e00\u4e2ahttp\u8bf7\u6c42\uff0caction\u5904\u7406\u5b8c\u540e\u8f6c\u53d1\u5230\u4e00\u4e2aaction\uff0c\u8bf7\u6c42\u53c2\u6570\u5168\u90e8\u4e22\u5931\uff0caction\u5904\u7406\u7ed3\u679c\u4e0d\u4f1a\u4e22\u5931,\u5171\u4eab\u4e00\u4e2aActionContext\uff0c\u6545\u53ef\u4ee5\u5728\u4e0a\u4e0b\u6587\u4e2d\u83b7\u53d6\uff0c\u5728\u9875\u9762\u4e0a\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002\u624b\u52a8\u83b7\u53d6\u7684<br \/>\n\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>Java\u4ee3\u7801<\/p>\n<p>HttpServletRequest request = ServletActionContext.getRequest();<br \/>\nString s=(String)request.getAttribute(&#8220;propName&#8221;);<\/p>\n<p>2\uff09\u5b9e\u73b0ModelDriven\u63a5\u53e3<\/p>\n<p>\u5728Step1Action\u4e2d\uff0c\u52a0\u5165getModel\uff1a<br \/>\nJava\u4ee3\u7801<\/p>\n<p>1. public Object getModel() {<br \/>\n2.         return message;<br \/>\n3. }     <\/p>\n<p>public Object getModel() {<br \/>\nreturn message;<br \/>\n}    <\/p>\n<p>\u5728Step2Action\u4e2d\uff0c\u52a0\u5165setModel:<br \/>\nJava\u4ee3\u7801<\/p>\n<p>1. public void setModel(Object o){<br \/>\n2.         System.out.println(&#8220;message is:&#8221;+o);<br \/>\n3. }  <\/p>\n<p>public void setModel(Object o){<br \/>\nSystem.out.println(&#8220;message is:&#8221;+o);<br \/>\n}<\/p>\n<p>\u6ce8\u610f\uff0csetModel\u7684\u8c03\u7528\u5148\u4e8eexecute()\u65b9\u6cd5\u540e\u4e8e\u6784\u9020\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1, dispatcher -forward \u8f6c\u5230\u6307\u5b9a\u7684jsp\u9875\u9762 2,redirect \u91cd\u5b9a\u5411\u5230jsp\u9875\u9762\u6216 [&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-902","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\/902","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=902"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/902\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}