In this tutorial session we will see the important key aspects of Struts2 Actions. If you want to get clear idea over struts 2 from the beginning please refer the below posts.<\/p>\n
Struts2 Introduction<\/a> In nutshell, Struts2 actions are string returning java methods. Those methods are organized in a effective way for Struts2 Framework. See a sample action below.<\/p>\n The above method is a valid struts2 action. So in short, any java method returns a string can be a struts2 action.<\/p>\n So now we want to know how the framework executes the correct java method when request arrives. Here we take tomcat apache as our example server where our application is running. If suppose the user types the url\u00a0http:\/\/localhost:8080\/Struts2Action<\/span>\u00a0 in the browser it will come to the application server (i.e) here tomcat.\u00a0 Then the application server look for the application Struts2Action in its webapps folder.<\/p>\n Once it identifies it will look for web.xml file inside the application WEB_INF folder.Once it finds the web.xml it will read and execute based on that file.<\/p>\n As I explained in the HelloWorld Application<\/a> the web.xml will redirect all the requests it receives to struts filter (For Struts2 only; not all). Because we added a filter to all the requests. See a sample web.xml below.<\/p>\n So here we redirected all the requests to org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter filter class. So the control will go to the org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter class. That class takes struts.xml as it’s configuration. So it will look for struts.xml in the class folder path.<\/p>\n See a sample struts.xml file below.<\/p>\n Once the struts.xml is found based on that configuration the flow continuous. Clear? As per our example once the user entered the URL\u00a0http:\/\/localhost:8080\/Struts2Action<\/span>\u00a0 the control will open this struts.xml file and tries to find corresponding action. In our example we never mentioned any name after the application Context Struts2Action.<\/em><\/p>\n But in struts.xml we mentioned a action with empty name as below.<\/p>\n So it will get match with our request http:\/\/localhost:8080\/Struts2Action<\/span>\u00a0. If suppose we type the url http:\/\/localhost:8080\/Struts2Action\/welcome<\/span> then the welcome action mentioned in the struts.xml will get executed. See that below.<\/p>\n Now we will see how to declare an action in the struts.xml. The syntax to declare an action is<\/p>\n where<\/p>\n With this we\u00a0 finish this session for action. We will continue with a simple example in the net session.<\/p>\n","protected":false},"excerpt":{"rendered":" In this tutorial session we will see the important key aspects of Struts2 Actions. If you want to get clear idea over struts 2 from the beginning please refer the below posts. Struts2 Introduction Struts2 HelloWorld Application In nutshell, Struts2 … Continue reading
\nStruts2 HelloWorld Application<\/a><\/p>\npublic String welcome() {\r\n return \"success\";\r\n}<\/pre>\n
Actions mainly do three thinks.<\/h4>\n
\n
<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n<web-app xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xmlns=\"http:\/\/java.sun.com\/xml\/ns\/javaee\" xsi:schemaLocation=\"http:\/\/java.sun.com\/xml\/ns\/javaee http:\/\/java.sun.com\/xml\/ns\/javaee\/web-app_3_0.xsd\" id=\"WebApp_ID\" version=\"3.0\">\r\n <display-name>Struts2Action<\/display-name>\r\n <filter>\r\n <filter-name>struts2<\/filter-name>\r\n <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter<\/filter-class>\r\n <\/filter>\r\n <filter-mapping>\r\n <filter-name>struts2<\/filter-name>\r\n <url-pattern>\/*<\/url-pattern>\r\n <\/filter-mapping>\r\n<\/web-app><\/pre>\n
<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n<!DOCTYPE struts PUBLIC\r\n\"-\/\/Apache Software Foundation\/\/DTD Struts Configuration 2.0\/\/EN\"\r\n\"http:\/\/struts.apache.org\/dtds\/struts-2.0.dtd\">\r\n<struts>\r\n <package name=\"default\" namespace=\"\/\" extends=\"struts-default\">\r\n <action name=\"\">\r\n <result>home.jsp<\/result>\r\n <\/action>\r\n <action name=\"welcome\" class=\"com.rajesh.struts2.Welcome\" method=\"welcome\">\r\n <result name=\"success\">welcome.jsp<\/result>\r\n <\/action>\r\n <\/package>\r\n<\/struts><\/pre>\n
<action name=\"\">\r\n <result>home.jsp<\/result>\r\n<\/action>\r\n<\/pre>\n
<action name=\"welcome\" class=\"com.rajesh.struts2.Welcome\" method=\"welcome\">\r\n <result name=\"success\">welcome.jsp<\/result>\r\n<\/action><\/pre>\n
<action name={actionName} [class={className}]>\r\n<result [name={resultValue1}]>{resultFileA}<\/result>\r\n<result [name={resultValue2}]>{resultFileB}<\/result>\r\n ...\r\n<result [name={resultValuen}]>{resultFileZ}<\/result>\r\n<\/action><\/pre>\n
actionName is<\/h4>\n
\n
className is<\/h4>\n
\n
resultName is<\/h4>\n
\n
resultFile is<\/h4>\n
\n