import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.hpsvse.service.IEmpService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
@Controller //用于标注控制层组件(如struts中的action)
@ParentPackage("struts-default") //用户指定Action所在的包要继承的父包
@Namespace("/") //用户指定action所属于的命名空间
@Scope("prototype") //设置prototype后,当我们从 Spring 容器中获取 base 时,每次返回的都是一个新的实例
@ResultPath("/") //指定该Action到/目录下寻找物理视图资源
@Results( //@Results:用于组织多个@Result。
{ @Result(name = "success", location = "/ok.jsp"),
@Result(name = "input", location = "/index.jsp") }
)
//这里在Results定义主要是整个action的全局设置
/*
* Results、Result
*/
/*
@Result 相当于struts.xml文件中的<result>元素的内容。
使用@Result必须指定一个name属性,相当于<result name=””/>另外,它还有几个可选的属性。
type相当于<result type=””/>指定返回视图资源的类型
location 相当于<result>…..</result>中间部分,用于指定实际视图位置
params:该属性相当于<result/>元素里多个<param../>子元素的作用,用于为该Result指定参数值。该属性应满足{“name1”,”value1”,”name2”,”value2”}格式
*/
/*@results主要返回类型如下:
@Results({
@Result(name="ajaxData",params= {"root","jsdata"},type="json"),
@Result(name="result",params= {"root","result"},type="json"),
@Result(name="indexView",location="/metadata/indexView.jsp",type="dispatcher"),
@Result(name="getAtomTgtList", type="redirect",location="/iap/getAtomTgtList.action"),
@Result(name="jsonData",params= {"root","jsdata_index"},type="json"),
@Result(name="excel",params= {"contentType","application/vnd.ms-excel",
"inputName","excelStream","bufferSize","1024","contentDisposition","filename=export.xls"},type="stream")
}) */
/**
* 验证注解
*/
@Validations(
requiredStrings={
@RequiredStringValidator(fieldName="loginName",message="用户名不能为空!"),
@RequiredStringValidator(fieldName="password",message="密码不能为空!")
},
regexFields={
@RegexFieldValidator(fieldName="telNum",expression="^(//+86|0|1)//d{10,11}$",
message="电话号码格式不正确!")
}
)
/**
*
* ExceptionMapping:指定异常映射。(映射一个声明异常)
* ExceptionMappings:一级声明异常的数组。
* exception: 用于指定异常类
* result:用于指定逻辑视图
*
*/
@ExceptionMappings(
{ @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") }
)
/**
*
* 与拦截器配置的Annotation有@InterceptorRef、@InterceptorRefs和@DefaultInterceptorRef
* @InterceptorRefs用于组织多个@InterceptorRef,因此它只需要指定一个value属性值,
* 该value属性值为多个@InterceptorRef
* @InterceptorRef用于为指定Action引用lanjieq或者是拦截器栈。
* 也就相当于strut.xml中位于<action../>元素内部的<interceptor-ref../>子元素的作用。
* 使用@InterceptorRefAnnotation时,必须制定一个value属性,用于指定所引用的拦截器或拦截器栈的名字。
* 相当于<interceptor-ref../>子元素里name属性的作用。
*
*/
@Action(value = "file-manager",
interceptorRefs = {
@InterceptorRef(value = "fileUpload",params={"maximumSize","1024000","allowedTypes","image/pjpeg"}),
@InterceptorRef(value = "basicStack")},
results = {@Result(name = ActionSupport.SUCCESS, location = "/view/file-manager-sucess.jsp"),
@Result(name = ActionSupport.ERROR, location = "/view/file-manager-error.jsp") },
exceptionMappings = {@ExceptionMapping(exception = "java.lang.Exception", result = ActionSupport.ERROR)}
)
public class TestStruts3Action extends BaseAction{
@Autowired //注解Service
private IEmpService empSerivce;
private String loginName;
private String password;
@Action("login") //或者写成 @Action(value = "login")
public String login() throws Exception {
if ("yjd".equals(loginName) && "yjd".equals(password)) {
return SUCCESS;
} else {
return ERROR;
}
}
@Action(value = "add", results = { @Result(name = "success", location = "/index.jsp") })
public String add() throws Exception {
return SUCCESS;
}
}