Struts2 Result Annotation传参

本文介绍了如何使用Struts2中的@Result注解来实现动态参数传递,并提供了具体的代码示例。同时对比了XML配置文件中的相应设置。

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

在Struts的官方网站上有如下说明:

@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 redirect-action type in XML config.
    • TilesResult
  • params - An Array of the parameters in the form {key1, value1, key2, value2}

可知@Result是可以传参的,然而网上很多例子都只是传常量参数,但实际应用中往往需要传变量。其实可以这样写:

@Action(value = "/admin/info/edit", results = {@Result(name = "success", location = "show", params={"id", "${id}"}, type="redirectAction") })
public String edit(){

    ......

}

 

其实跟写xml配置是类似的。

<action name="actionName!*" class="actionClass" method="{1}">
    <result name="show" type="redirectAction">show?id=${id}</result>
</action>

 

顺便一提,在xml配置中,result里的路径注意不要带.action这样的后缀名,而且不能用类似<param name="id">${id}</param>这样的方式传参,不过这个bug已经将会在struts2.1.7中修复,详见https://issues.apache.org/struts/browse/WW-1714

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值