Struts2中自定义的Result

引言

所谓自定义Result,就是由我们自行开发Result,而不是使用由Struts2预定义的result。
在实际的开发中使用自定义的result机会不大,因为常见的各种页面展示技术,都有struts2给我们做的比较好好的。

自定义的Result

观看Result的源码如下:

public interface Result extends Serializable {

    /**
     * Represents a generic interface for all action execution results.
     * Whether that be displaying a webpage, generating an email, sending a JMS message, etc.
     *
     * @param invocation  the invocation context.
     * @throws Exception can be thrown.
     */
    public void execute(ActionInvocation invocation) throws Exception;
}

我们可以看到是一个接口,并且只有一个方法,那我们自定义的饿时候需要实现这个接口。

public class MyResult implements Result {

    public void execute(ActionInvocation invocation) throws Exception {
        System.out.println("要处理的Result字符串是"+invocation.getResultCode());
    }
}

可以看到,我们可以通过ActionInvocation 去获取ActionContext,在ActionContext里边封装着所需要的值。

然后我们需要在struts2的配置文件中进行配置和使用:

<struts>
    <package name="helloworld" extends="struts-default">
        <result-types>
            <result-type name="MyResult" class="com.lc.action.MyResult" default="false">
            </result-type>
        </result-types>
        <action name="loginAction" class="com.lc.action.LoginAction">
            <result name="toWelocome" type="MyResult">/welcome.jsp</result>
        </action>
    </package>
</struts>  

标签里边的就是我们配置的一个自定义的result,然后我们在action中使用的时候是通过type=”MyResult”进行使用的。

到此一个自定义的result完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐刘根

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值