需要導入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();
}
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:攔截器引用組