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中)