这个例子将继续前面讲的例子
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.
和上个例子对比,我们做了如下变更,
- Action类以Action结尾,同时不实现com.opensymphony.xwork2.Action接口
- 我们使用/results目录为存放我们的result页面,而不再放在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.
我们的WelcomeUserAction类是一个简单的pojo类。但重要的是我们的类名药以Action结尾。
package com.vaannila.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.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.
这里,我们使用Action和Result注解主要是为了来介绍如何使用它们,我们也可以想前一个简单例子那样使用插件包默认的方式。
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.
Convention 插件使用Actin类名来映射器URL。它首先去掉类后面的Action后缀,然后在使用驼峰的地方转换为破折号。所以,默认情况下WelcomeUserAction要通过请求welcome-user来访问。但如果你要让Action使用一个不同的URL来进行,你必须使用Action注解。
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.
Action注解的值为“/welcome”,这意味着这个action将通过/welcome进行访问。你能够通过使用Action注解来改变默认的action和URL映射。
Now based on the result code from action the Convention plug-in will look for the result name welcome-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.
现在,在Action的result code基础上,Convention将在WEB-INF/content查找welcome-resultcode的文件。有可以通过改变struts的properties文件中的struts.convention.result.path 值来改变results的目录。
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.
我们的result页面的名字是successPage.jsp。Convention插件会查找welcome.jsp文件,因为我们Action的URL为“/welcome”。在这个例子中,如我们没有指定他的的success的result,那么将会出错。为此,我们可以使用Result注解。
The Result annotation maps the result code with the result page. Here the result code “success” is mapped to the result “/results/successPage.jsp”.
在Result注解中,我们用一个result code来指定映射的页面。这里的result code为“success”,被映射到的“/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.
当你没有使用插件默认的名字规约的时候来需要使用注解。如果你使用了,你可以实现没有任何配置文件下些Action和result页面,而框架能够根据需要进行调用。
源码
Source :Download
Source + Lib :Download
原文地址:http://www.vaannila.com/struts-2/struts-2-example/struts-2-annotations-example-1.html

