Struts2页面跳转方式

1、    转发

<action name="helloAction"class="cn.hd.hello.HelloAction" method="hello">

    <!--
    result
        name  对应的是action类中的method的返回值
              名字可以随便写,只要和action类中的返回值结果是一样的
              但是我们一般情况下使用特定的。
       type dispatcher  转发
            redirect    重定向
       标签中间的值   跳转页面的地址
    -->
    <result name="success"type=" dispatcher ">/hello.html</result>
</action>

 

2、    重定向

<action name="helloAction"class="cn.hd.hello.HelloAction" method="hello">

   <!--
    result
        name  对应的是action类中的method的返回值
              名字可以随便写,只要和action类中的返回值结果是一样的
              但是我们一般情况下使用特定的。
       type  dispatcher 转发
            redirect    重定向
       标签中间的值   跳转页面的地址
    -->
    <result name="success"type="redirect">/hello.html</result>
</action>

 

3、    转发到自己的Action

<struts>

<package name="jump"namespace="/" extends="struts-default">
        <actionname="chainAction_*" class="cn.hd.jump.ChainAction"method="{1}">
            <resultname="success" type="chain">
                <!--跳转到下一个action的action的名字-->
                <paramname="actionName">targetAction</param>
                <!--跳转到下一个action的命名空间-->
                <paramname="namespace">/</param>
            </result>
            <!--高版本要加这个属性,不然版本会不生效-->
           <allowed-methods>jump</allowed-methods>
        </action>
        <actionname="targetAction" class="cn.hd.jump.TargetAction">
           <resultname="success">/hello.html</result>
        </action>
    </package>
</struts>

4、    重定向到自己的请求

<package name="jump"namespace="/" extends="struts-default">

<!--重定向到Action-->

<action name="redirectAction_*"class="cn.hd.jump.RedirectAction" method="{1}">
    <result name="success"type="redirectAction">
        <paramname="actionName">targetAction</param>
        <paramname="namespace">/</param>
    </result>

<allowed-methods>jump</allowed-methods>
</action>

<action name="targetAction"class="cn.hd.jump.TargetAction">
       <resultname="success">/hello.html</result>
    </action>
</package>

5、    不跳转   接受ajax请求

(1).使用原生的Servlet API

ActionContext对象在struts2中,我们把它叫做数据中心。包含:

       1、Request      原生对象

       2、Response   原生对象

3、Session       原生对象

4、ServletContext       原生对象

5、Request      域         Map     Struts2不建议使用,如果要用就用ActionContext对象

       6、Session       域         Map

       7、ServletContext 对应的是Application       域         Map

       8、attr        域         Map

ActionContext生命周期:

       所有的Servlet对象都存了,他的生命周期是随着其中最短的对象而创建和销毁。也就是request对象。

       每次请求到来,ActionContext都会重新创建。

所以他解决了线程安全的问题。

使用ServletApi响应Ajax请求,首先获得response方法(2种方法)然后回到原生态Servlet响应中

Action中的方法的返回值必须是NONE(如果设置为none,那么在struts.xml中就不用书写result标签了)

(2).stream

       1.在Action类中创建一个inputStream对象

       2.给这个对象增加一个get方法

       3.struts.xml中的result标签中设置type-stream

       4.Action最终可以将json字符串转化为inputStream流

(3).使用json插件

1、导包

2、建立一个Action类,

然后将定义几个想要返回的JSON字符串的键名,生成get/set方法,然后在Action访问的方法中为这个JSON键名的对象进行赋值。

3、配置struts.xml文件

       需要重新建package,extends   继承不能写struts-default,要写json-default (在struts2-json-plugin包中)

       Action不变

       result name属性不变,type属性变成json(在struts2-json-plugin包的struts-plugin.xml中)


  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值