struts中采用注解配置Action

需要导入struts2-convention-plugin的jar包

Action

省略getters和setters

@ParentPackage("xystruts-default")
@Namespace("/login")
public class LoginAction extends BaseAction
{
private String verifyCode;

@Action(value = "login", results = { @Result(location = "/pages/main.jsp"),@Result(name = "login", location = "/pages/login.jsp") })
public String login()
{
String sysVerifyCode = (String) getSession().get("verifyCode");
if (StringHelper.isEmpty(verifyCode) || !sysVerifyCode.equalsIgnoreCase(verifyCode))
{
addActionError("验证码错误");
return "login";
}
return "success";
}

@Action(value = "logout", results = { @Result(location = "/pages/login.jsp") })
public String logout()
{
Map session = getSession();
if (session != null)
session.clear();
return "success";
}

}

JSP

<form action="login/login.action"></form>

<a href="login/logout.action">登出</a>

其中Result注解中name属性为空,表示默认为"success"

常用注解如下

Namespace:指定命名空间

ParentPackage:指定父包

Result:提供了Action结果的映射(一个结果的映射)

Results:Result注解列表

ResultPath:指定结果页面的基路径

Action:指定Action的访问URL

Actions:Action注解列表

ExceptionMapping:指定异常映射(映射一个声明异常)

ExceptionMappings:一级声明异常的数组

InterceptorRef:拦截器引用

InterceptorRefs:拦截器引用组

你可能感兴趣的:(struts)