{"id":1851,"date":"2011-03-30T11:59:02","date_gmt":"2011-03-30T06:29:02","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1851"},"modified":"2011-03-30T11:59:02","modified_gmt":"2011-03-30T06:29:02","slug":"dynamic-results-in-struts-2","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/struts\/dynamic-results-in-struts-2\/","title":{"rendered":"Dynamic Results in Struts 2"},"content":{"rendered":"<p>In Struts 2, we can decide next action to be executed at runtime with complete set of its own\u00a0interceptors. \u00a0here <a title=\"Struts 2 login application example and sourcecode\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/struts\/step-by-step-simple-login-application-in-struts-2\/\" target=\"_blank\"> i will assume the example and source code of the previous article.<\/a><\/p>\n<p>For this demo, change the existing Action class code:<br \/>\n<!--more--><\/p>\n<pre class=\"brush: java; highlight: [20,23,24,25,27,28,29]; title: ; notranslate\" title=\"\">\npackage com.G2.Actions;\n\nimport com.G2.Model.LoginValidate;\nimport com.opensymphony.xwork2.ActionSupport;\n\npublic class LoginAction extends ActionSupport {\n\n\tString uName,pwd;\n\tString nextAction;\n\n\t@Override\n\tpublic String execute() throws Exception {\n\t\tboolean res = LoginValidate.isLoginValid(uName, pwd);\n\n\t\tif(res)\n\t\t\tnextAction = &quot;LoginSuccess&quot;;\n\t\telse\n\t\t\tnextAction = &quot;LoginInput&quot;;\n\n\t\treturn  &quot;next&quot;;\n\t}\n\n\tpublic String getNextAction() {\n\t\treturn nextAction;\n\t}\n\n\tpublic void setNextAction(String nextAction) {\n\t\tthis.nextAction = nextAction;\n\t}\n\n\tpublic String getuName() {\n\t\treturn uName;\n\t}\n\n\tpublic void setuName(String uName) {\n\t\tthis.uName = uName;\n\t}\n\n\tpublic String getPwd() {\n\t\treturn pwd;\n\t}\n\n\tpublic void setPwd(String pwd) {\n\t\tthis.pwd = pwd;\n\t}\n}\n<\/pre>\n<p>As you can see, I have added one more filed with setters and getters named &#8220;nextAction&#8221;\u009d, which will be accessed in struts configuration file.<\/p>\n<p>Change in Configuration file :<\/p>\n<pre class=\"brush: xml; highlight: [18]; title: ; notranslate\" title=\"\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;\n&lt;!DOCTYPE struts PUBLIC\n        &quot;-\/\/Apache Software Foundation\/\/DTD Struts Configuration 2.0\/\/EN&quot;\n        &quot;http:\/\/struts.apache.org\/dtds\/struts-2.0.dtd&quot;&gt;\n\n&lt;struts&gt;\n\n\t&lt;package name=&quot;Login&quot; extends=&quot;struts-default&quot;&gt;\n\t\t&lt;action name=&quot;LoginInput&quot;&gt;\n\t\t\t&lt;result&gt;\/pages\/Login.jsp&lt;\/result&gt;\n\t\t&lt;\/action&gt;\n\n\t\t&lt;action name=&quot;LoginSuccess&quot;&gt;\n\t\t\t&lt;result &gt;\/pages\/LoginSuccess.jsp&lt;\/result&gt;\n\t\t&lt;\/action&gt;\n\n\t\t&lt;action name=&quot;LoginAction&quot; class=&quot;com.G2.Actions.LoginAction&quot;&gt;\n\t\t\t&lt;result name=&quot;next&quot; type=&quot;redirectAction&quot;&gt;${nextAction}&lt;\/result&gt;\n\t\t&lt;\/action&gt;\n\n\t&lt;\/package&gt;\n&lt;\/struts&gt;\n\n\n<\/pre>\n<p>Here we are redirecting, on the next action if the result name is &#8220;next&#8221;\u009d. The next action is decided by the variable &#8220;nextAction&#8221; of the Action class. In above configuration if we change the result type to &#8220;chain&#8221;\u009d instead of &#8220;redirectAction&#8221;\u009d, then still application will run.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Example and sourcecode of creating Dynamic Results in Struts 2<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[16],"tags":[335],"class_list":["post-1851","post","type-post","status-publish","format-standard","hentry","category-struts","tag-struts"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1834,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/struts\/step-by-step-simple-login-application-in-struts-2\/","url_meta":{"origin":1851,"position":0},"title":"Step by Step Simple Login Application in Struts 2","author":"Jitendra","date":"March 24, 2011","format":false,"excerpt":"Example of Simple Login Application in Struts 2","rel":"","context":"In &quot;Struts&quot;","block_context":{"text":"Struts","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/struts\/"},"img":{"alt_text":"Final Structure of Struts 2 login application","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Final-Structure-of-Struts-2-login-application.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1643,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/servlet-hibernate-jquery-and-ajax-based-google-like-chat\/","url_meta":{"origin":1851,"position":1},"title":"Servlet, Hibernate, jQuery and Ajax based google like chat","author":"Jitendra","date":"March 14, 2011","format":false,"excerpt":"Servlet, Hibernate, jQuery and Ajax based google like chat with source code","rel":"","context":"In &quot;Hibernate&quot;","block_context":{"text":"Hibernate","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/hibernate\/"},"img":{"alt_text":"Servlet, Hibernate, jQuery and Ajax based google like chat","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Servlet-Hibernate-jQuery-and-Ajax-based-google-like-chat.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1860,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/struts\/error-resolved-form-action-defaulting-to-action-attributes-literal-value\/","url_meta":{"origin":1851,"position":2},"title":"Error Resolved- Form action defaulting to &#8216;action&#8217; attribute&#8217;s literal value","author":"Jitendra","date":"March 30, 2011","format":false,"excerpt":"Error Resolved in Struts 2- Form action defaulting to 'action' attribute's literal value","rel":"","context":"In &quot;Struts&quot;","block_context":{"text":"Struts","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/struts\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1864,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/struts\/explain-dynamic-method-invocation-in-struts-2\/","url_meta":{"origin":1851,"position":3},"title":"Explain dynamic method invocation in Struts 2","author":"Jitendra","date":"March 30, 2011","format":false,"excerpt":"Explain dynamic method invocation in Struts 2","rel":"","context":"In &quot;Struts&quot;","block_context":{"text":"Struts","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/struts\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1683,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/reading-and-writing-custom-annotation\/","url_meta":{"origin":1851,"position":4},"title":"Reading and Writing Custom Annotation","author":"Jitendra","date":"March 16, 2011","format":false,"excerpt":"Article on Reading and Writing Custom Annotation with example","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1756,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/searching-xml-file-using-dom-parser-of-jaxp\/","url_meta":{"origin":1851,"position":5},"title":"Searching XML File using DOM Parser of JAXP","author":"Jitendra","date":"March 22, 2011","format":false,"excerpt":"Example of Searching XML File using DOM Parser of JAXP","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1851"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1851\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}