struts 2 注解类型——迹忆客-ag捕鱼王app官网
struts 2 应用程序可以使用 java 5 注解作为 xml 和 java 属性配置的替代方案。 以下是与不同类别相关的最重要注解列表
action 中注解
namespace 注解
@namespace
注解允许在 action 类中定义 action 的命名空间,而不是基于零配置的约定。
@namespace("/content")
public class employee extends actionsupport{
...
}
result 注解
@result
注解允许在 action 类而不是 xml 文件中定义 action 结果。
@result(name = "success", value = "/success.jsp")
public class employee extends actionsupport{
...
}
results 注解
@results
注解定义了一组 action 的结果。
@results({
@result(name = "success", value = "/success.jsp"),
@result(name = "error", value = "/error.jsp")
})
public class employee extends actionsupport{
...
}
拦截器 注解
after 注解
@after
注解标记了一个 action 方法,需要在主动作方法之后调用并执行结果。 返回值被忽略。
public class employee extends actionsupport{
@after
public void isvalid() throws validationexception {
// 验证模型对象,如果失败则抛出异常
}
public string execute() {
// 执行安全操作
return success;
}
}
before 注解
@before
注解标记了一个动作方法,需要在主动作方法和结果执行之前调用。 返回值被忽略。
public class employee extends actionsupport{
@before
public void isauthorized() throws authenticationexception {
// 验证模型对象,如果失败则抛出异常
}
public string execute() {
// 执行安全操作
return success;
}
}
beforeresult 注解
@beforeresult
注解标记了需要在结果之前执行的操作方法。 返回值被忽略。
public class employee extends actionsupport{
@beforeresult
public void isvalid() throws validationexception {
// 验证模型对象,如果失败则抛出异常
}
public string execute() {
// 执行动作
return success;
}
}
验证 注解
conversionerrorfieldvalidator 注解
此验证注解检查字段是否存在任何转换错误,如果存在则应用它们。
public class employee extends actionsupport{
@conversionerrorfieldvalidator(message = "default message",
key = "i18n.key", shortcircuit = true)
public string getname() {
return name;
}
}
daterangefieldvalidator 注解
此验证注解检查日期字段是否具有指定范围内的值。
public class employee extends actionsupport{
@daterangefieldvalidator(message = "default message",
key = "i18n.key", shortcircuit = true,
min = "2005/01/01", max = "2005/12/31")
public string getdob() {
return dob;
}
}
doublerangefieldvalidator 注解
此验证注解检查浮点数字段是否具有指定范围内的值。 如果既没有设置 min 也没有设置 max,则什么都不做。
public class employee extends actionsupport{
@doublerangefieldvalidator(message = "default message",
key = "i18n.key", shortcircuit = true,
mininclusive = "0.123", maxinclusive = "99.987")
public string getincome() {
return income;
}
}
emailvalidator 注解
如果字段包含非空字符串,则此验证注解检查该字段是否为有效的电子邮件地址。
public class employee extends actionsupport{
@emailvalidator(message = "default message",
key = "i18n.key", shortcircuit = true)
public string getemail() {
return email;
}
}
expressionvalidator 注解
此非字段级别的验证器验证提供的正则表达式。
@expressionvalidator(message = "default message", key = "i18n.key",
shortcircuit = true, expression = "an ognl expression" )
intrangefieldvalidator 注解
此验证注解检查数字字段是否具有指定范围内的值。 如果既没有设置 min 也没有设置 max,则什么都不做。
public class employee extends actionsupport{
@intrangefieldvalidator(message = "default message",
key = "i18n.key", shortcircuit = true,
min = "0", max = "42")
public string getage() {
return age;
}
}
regexfieldvalidator 注解
此注解使用正则表达式验证字符串字段。
@regexfieldvalidator( key = "regex.field", expression = "yourregexp")
requiredfieldvalidator 注解
此验证注解检查字段是否为非空。 注解必须在方法级别应用。
public class employee extends actionsupport{
@requiredfieldvalidator(message = "default message",
key = "i18n.key", shortcircuit = true)
public string getage() {
return age;
}
}
requiredstringvalidator 注解
此验证注解检查字符串字段是否不为空(即长度 > 0 的非空字段)。
public class employee extends actionsupport{
@requiredstringvalidator(message = "default message",
key = "i18n.key", shortcircuit = true, trim = true)
public string getname() {
return name;
}
}
stringlengthfieldvalidator 注解
此验证器检查字符串字段的长度是否正确。 它假定该字段是一个字符串。 如果既没有设置 minlength 也没有设置 maxlength,则什么也不做。
public class employee extends actionsupport{
@stringlengthfieldvalidator(message = "default message",
key = "i18n.key", shortcircuit = true,
trim = true, minlength = "5", maxlength = "12")
public string getname() {
return name;
}
}
urlvalidator 注解
此验证器检查字段是否为有效 url。
public class employee extends actionsupport{
@urlvalidator(message = "default message",
key = "i18n.key", shortcircuit = true)
public string get {
return url;
}
}
validations 注解
如果你想使用多个相同类型的注解,这些注解必须嵌套在@validations()
注解中。
public class employee extends actionsupport{
@validations(
requiredfields =
{@requiredfieldvalidator(type = validatortype.simple,
fieldname = "customfield",
message = "you must enter a value for field.")},
requiredstrings =
{@requiredstringvalidator(type = validatortype.simple,
fieldname = "stringisrequired",
message = "you must enter a value for string.")}
)
public string getname() {
return name;
}
}
customvalidator 注解
此注解可用于自定义验证器。 使用 @validationparameter
注解来提供额外的参数。
@customvalidator(type ="customvalidatorname", fieldname = "myfield")
类型转换 注解
conversion 注解
这是类型级别的类型转换的标记注解。 @conversion
注解必须在类型级别应用。
@conversion()
public class conversionaction implements action {
}
createifnull 注解
此注解设置类型转换的 createifnull。 @createifnull
注解必须在字段或方法级别应用。
@createifnull( value = true )
private list users;
element 注解
此注解设置 element 来进行类型转换。@element
注解必须在字段或方法级别应用。
@element( value = com.acme.user )
private list userlist;
key 注解
此注解设置类型转换的 key。 @key
注解必须在字段或方法级别应用。
@key( value = java.lang.long.class )
private map usermap;
keyproperty 注解
此注解设置类型转换的 keyproperty。 @keyproperty
注解必须在字段或方法级别应用。
@keyproperty( value = "username" )
protected list users = null;
typeconversion 注解
此注解用于类和应用程序范围的转换规则。 @typeconversion
注解可以在属性和方法级别应用。
@typeconversion(rule = conversionrule.collection,
converter = "java.util.string")
public void setusers( list users ) {
this.users = users;
}