Here is a simple example of Struts2.
Directory hierarchy
your-application
│ result1.jsp
│
└───WEB-INF
│ web.xml
│
└───classes
struts.xml
First of all, configure strut2 filter into web.xml of your application
<web-app>
<display-name>Struts 2 eg 1</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Now, in struts.xml file which is located into classes folder of you web application, write following contetns
<web-app>
<display-name>Struts 2 eg 1</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Here contastant element is used to set some properties of struts. We used struts.devMode property value to true. this enable us to view all information like errors, exception and warning at the time of execution.
Element package-name defines the name of package that can be used to group diffrent action into a specific namespace. The attribute namespace defines relative path/pattern to be followed while invoking these actions.
Elenment action declare action to be executed for specified url-pattern . Here is "/hello" url pattern for which this action will be executed.
Element result inside action element declare the resultant view to be returned to client after successful execution of this action. Here we created result1.jsp page to be returned to view.
result1.jsp
this file contains only following message
Now deploy this aplication into server and write following address into address bar of web browser.
http://localhost:8080/your-application/hello
You will get following output.
This is test application
Resources: struts2, Apache tomcat
Directory hierarchy
your-application
│ result1.jsp
│
└───WEB-INF
│ web.xml
│
└───classes
struts.xml
First of all, configure strut2 filter into web.xml of your application
<web-app>
<display-name>Struts 2 eg 1</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Now, in struts.xml file which is located into classes folder of you web application, write following contetns
<web-app>
<display-name>Struts 2 eg 1</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Here contastant element is used to set some properties of struts. We used struts.devMode property value to true. this enable us to view all information like errors, exception and warning at the time of execution.
Element package-name defines the name of package that can be used to group diffrent action into a specific namespace. The attribute namespace defines relative path/pattern to be followed while invoking these actions.
Elenment action declare action to be executed for specified url-pattern . Here is "/hello" url pattern for which this action will be executed.
Element result inside action element declare the resultant view to be returned to client after successful execution of this action. Here we created result1.jsp page to be returned to view.
result1.jsp
this file contains only following message
This is test application
Now deploy this aplication into server and write following address into address bar of web browser.
http://localhost:8080/your-application/hello
You will get following output.
This is test application
Resources: struts2, Apache tomcat
Comments