Tuesday, August 6, 2013

Struts 2 Annotation Tutorial 2

This example is the continuation of the previous annotation example. If you are new to Struts 2 annotations then go through that example first ( Struts 2 Annotations - Part 1 ). Here we will see the same hello user example with the following changes.
  • Our Action class ends with the world Action and does not implement com.opensymphony.xwork2.Action.
  • We use /results directory for storing our result pages instead of WEB-INF/content.
The directory structure of the hello user example is shown below.
Our WelcomeUserAction class is a simple pojo class. The important thing to note here is that our Action classs name ends with the word Action.
01.import org.apache.struts2.convention.annotation.Action;
02.import org.apache.struts2.convention.annotation.Result;
03. 
04.public class WelcomeUserAction {
05.private String userName;
06.private String message;
07. 
08. 
09.@Action(value="/welcome",results={@Result(name="success",location="/results/successPage.jsp")})
10.public String execute() {
11.message = "Welcome " + userName + " !";
12.return "success";
13.}
14. 
15.public void setUserName(String userName) {
16.this.userName = userName;
17.}
18. 
19.public void setMessage(String message) {
20.this.message = message;
21.}
22. 
23.public String getUserName() {
24.return userName;
25.}
26. 
27.public String getMessage() {
28.return message;
29.}
30.}
Here we use Action and Result annotations just to show you how to use them, for simple example like this you can use the intelligent defaults provided by the Convention plug-in.
The Convention plug-in uses the Action class name to map the action URL. The Convention plug-in first removes the world Action at the end of the class name and then converts the camel case name to dashes. So by default our WelcomeUserAction will be invoked for the request URL welcome-user. But if you want the Action to be invoked for a different URL then you can do this by using the Action annotation.
The value of the Action annotation is "/welcome", this means that the action will be invoked for the request URL "/welcome". You can change the default action and URL mapping using the Action annotation.
Now based on the result code from action the Convention plug-in will look for the result namewelcome-resultcode in the directory WEB-INF/content. You can change this to a different location by setting the property struts.convention.result.path to a new value in the Struts properties file. In this example we store the result pages in /results directory.
1.struts.properties file
2.-----------------------
3.struts.convention.result.path=/results
Our result page name is successPage.jsp, the Convention plug-in will look for a page like welcome.jsp ( the file can even be a freemaker or velocity file ) since our URL is "/welcome". In this case it will give an error, if we are not specifying which result it should invoke when the result is "success". To do this we use the Result annotation.
The Result annotation maps the result code with the result page. Here the result code "success" is mapped to the result "/results/successPage.jsp".
The annotations needs to be specified only when you are not using the default naming conventions, if you use them you can keep writing action classes and result pages without any configuration and the framework know exactly when to invoke them.

No comments:

Post a Comment