Struts2零配置

六、常用注解

1.@Action注解

①标记在Action方法上,指定访问当前Action方法的URL地址

package com.example.actions;

 

import com.opensymphony.xwork2.ActionSupport;

import org.apache.struts2.convention.annotation.Action;

 

public class HelloWorld extends ActionSupport {

  @Action("/different/url")

  public String execute() {

    return SUCCESS;

  }

}

 

②与@Actions注解配合,为当前Action方法匹配多个URL地址

public class HelloWorld extends ActionSupport {

  @Actions({

    @Action("/different/url"),

    @Action("/another/url")

  })

  public String execute() {

    return SUCCESS;

  }

}

 

③标记在Action方法上,定义execute()以外的Action方法

public class HelloWorld extends ActionSupport {

  @Action("/different/url")

  public String execute() {

    return SUCCESS;

  }

 

  @Action("url")

  public String doSomething() {

    return SUCCESS;

  }

}

 

 

④指定当前Action方法引用的拦截器或拦截器栈

@Action(interceptorRefs={@InterceptorRef("validation"), @InterceptorRef("defaultStack")})

  public String execute() {

    return SUCCESS;

  }  @Action(interceptorRefs=@InterceptorRef(value="validation",params={"programmatic", "false", "declarative", "true}))

  public String execute() {

    return SUCCESS;

  }

 

2.Result注解

①为Action类声明类级别的result

@Results({

  @Result(name="failure", location="fail.jsp")

})

public class HelloWorld extends ActionSupport {

  @Action(value="/different/url",

    results={@Result(name="success", location="http://struts.apache.org", type="redirect")}

  )

  public String execute() {

    return SUCCESS;

  }

 

  @Action("/another/url")

 

  public String doSomething() {

    return SUCCESS;

  }

}

②指定结果类型并设置参数

  @Action(value="/different/url",

    results={@Result(name="success", type="httpheader", params={"status", "500", "errorMessage", "Internal Error"})}

  )

  public String execute() {

    return SUCCESS;

  }

 

 


本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值