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.xworkAction.
·
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.
import org.apache.strutsconvention.annotation.Action;
import org.apache.strutsconvention.annotation.Result;
public class WelcomeUserAction {
private String
userName;
private String message;
@Action(value="/welcome",results={@Result(name="success",location="/results/successPage.jsp")})
public String
execute() {
message = "Welcome
" + userName + "
!";
return "success";
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setMessage(String message) {
this.message = message;
}
public String
getUserName() {
return userName;
}
public String
getMessage() {
return message;
}
}
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.
struts.properties file
-----------------------
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