注解实现struts2零配置

时间:2023-03-08 22:56:47
注解实现struts2零配置

零配置指的是不经过配置文件struts.xml配置Action

首先:导入jar   struts2-convention-plugin-2.3.24.1.jar

package com.action;

import javax.xml.ws.Action;

import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport;
/**
* @Namespace
* @Result
*/
@Namespace(value="/test")
@Results({
@Result(name="success",location="/success.jsp"),
@Result(name="redirect",location="/redirect.jsp",type="ServletRedrectResult.class"),
@Result(name="login",location="/login.jsp")
})
@ExceptionMappings( {
@ExceptionMapping(exception = "java.lange.RuntimeException", result = "error")
})
public class AnnotatedAction extends ActionSupport {
@Action(input="login")
public String login()throws Exception{
return SUCCESS;
}
@Action(input="add",output="/add.jsp")
public String add()throws Exception{
return "add";
} }

1) @ParentPackage 指定父包

2) @Namespace 指定命名空间

3) @Results 一组结果的数组

4) @Result(name="success",location="/msg.jsp") 一个结果的映射

5) @Action(input="login") 指定某个请求处理方法的请求URL。注意,它不能添加在Action类上,要添加到方法上。

6) @ExceptionMappings 一级声明异常的数组

7) @ExceptionMapping 映射一个声明异常

由于这种方式不是很常用,所以大家只做了解即可