Let’s understand the Struts 2 configuration with a simple example. We can have a form which has a text field to accept your name and on submit it should forward to a display page showing hello {YourName}.
Let’s have the following files for our example.
1) web.xml
2) struts.xml
3) Helloworld.java
4) input.jsp
5) display.jsp
Now let’s see all the above components in details
Web.xml
The filter and the filter-mapping elements are used to setup the Struts 2 FilterDispatcher.
The filter is mapped to the URL pattern “/*“. This means all the incoming request that targets to the Struts 2 action will be handled by FilterDispatcher class.
1 2 3 4 5 6 7 8 9 |
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
struts.xml
This is the struts configuration XML file. The struts.xml file contains the following action mapping.
1 2 3 4 5 6 7 |
<struts> <package name="default" extends="struts-default"> <action name="HelloWorld"> <result name="SUCCESS">/display.jsp</result> </action> </package> </struts> |
input.jsp
Below is the input.jsp code which will take the input from the user.
For using the struts tags in the jsp page “struts-tags” taglib directive should be included.
1 |
<%@taglib uri="/struts-tags" prefix="s" %> |
1 2 3 4 5 6 7 8 9 10 11 |
<html> <head> <title>Hello World</title> </head> <body> <s:form action="HelloWorld" > <s:textfield name="userName" label="User Name" /> <s:submit /> </s:form> </body> </html> |
HelloWorld.java
Now let’s see the Helloworld.java which will take care of the request from the client. This class is very simple and it contains two properties one for the user name and the other for displaying the message.
1 |
public class HelloWorld { |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
private String message; private String userName; public HelloWorld() { } public String execute() { setMessage("Hello " + getUserName()); return "SUCCESS"; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } } |
You can notice the following in the HelloWorld class
1) In the execute() method of the HelloWorld action we define the message to be displayed.
2) We need not have a separate form bean like struts 1 to access the form data. We can have a simple java class as action.
3) The action need not extend any class or implement any interface.
4) We need to have an execute() method which returns a String and has a public scope.
display.jsp
In the display page we display the “Hello User” message using the property tag.
1 2 3 4 5 6 7 8 9 |
<%@taglib uri="/struts-tags" prefix="s" %> <html> <head> <title>First struts 2 application</title> </head> <body> <h1><s:property value="message" /></h1> </body> </html> |
Leave a Reply