学 习 struts2 对 annotation的 理 解 Action Annotations

本文详细介绍了在Struts2框架中如何通过Annotations注解简化配置过程,特别是对于result注解的使用,使得配置文件更加清晰易懂。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    众 所 周 知  SDK5.0 的 annotation给 学 习 java的人们带来了很多方便。

在struts2中同样用到了它的功能。

    格格struts2我也是刚刚使用,所以把我用到的学到的东西和大家 交流一下。

首先来介绍一个struts2中的annotations

我从result annotation开始讲起:

用了result annotation就可以让struts.xml配置文件简单起来。不会看上去很复杂。

 

如:action:

 

public class UserAction extends ActionSupport implements ModelDriven{

 

    public String login(){

            

    }

    //......

    

}

 

如果不使用result则要这样配置struts.xml进行结果跳转

<!-- 用户登录 -->
  <action name="userLogin" class="userRegister" method="login">
   <result name="success">/index.jsp</result>
   <result name="loginFailure">/login.jsp</result>
  </action>

 

而使用了annotation后,则清楚多了。

@Results({
    @Result(name="success", value="/index.jsp", type=TilesResult.class),
    @Result(name="loginFailure", value="/login.jsp", type=TilesResult.class)

    params={"contentType", "application/xml"}
})

public class UserAction extends ActionSupport implements ModelDriven{

 

    public String login(){

            

    }

    

}

 

一看就可以明白吧。

让我们来看看它的参数设置吧,这是官方文档

@Result Annotation Parameters

  • name - Result name; default Action.SUCCESS
  • value - Value of result (result destination)
  • type - Type of result; default NullResult. For example:
    • ServletRedirectResult
    • ServletActionRedirectResult - Equivalent to redirectAction type in XML config.
    • TilesResult
  • params - An Array of the parameters in the form {key1, value1, key2, value2}

今天先介绍annotations中的@Result annotation.

您同样可以参考这里进行学习。

http://struts.apache.org/2.0.11/docs/result-annotation.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值