Struts2中页面的跳转方式

1. 转发

2. 重定向

3. 转发到自己的action

<!--转发到自己的action -->
<action name="chainAction_*" class="cn.hd.jump.ChainAction" method="{1}">

   <result name="success" type="chain">
       <!--跳转到下一个action 的action的名字-->
    <param name="actionName">targetAction_error</param>
       <!--跳转到下一个action的命名空间-->
    <param name="namespace">/</param>
   </result>
</action>


在浏览器中访问这个地址 即可实现转发的操作


4. 重定向到自己的action

<!--重定向   action-->
<action name="redirectAction_*" class="cn.hd.jump.RedirectAction" method="{1}">
    <result name="success" type="redirectAction">
        <param name="actionName">targetAction_jump</param>
        <param name="namespace">/</param>
    </result>
</action>

 

    


5. 不跳转  接收ajax请求

(1) 使用原生的servlet Api

 

ActionContext 在struts2 中,我们把它叫做数据中心。

Request    原生对象   

Response    原生对象   

Session     原生对象     

ServletContext 原生对象

 

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

Response    域   (Map)(键值对)

Session     域     (Map)(键值对)

ServletContext  Application  域   (Map)(键值对)

Attr             域   (Map)(键值对)

 

Action Context生命周期:

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

  每次请求的到来,Action Context 都会重新创建。

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

 

使用servletApi 响应Ajax请求

1. 首先要获得response 对象(2种办法) 调用回到原生态的servlet相应中   

  (1)// 通过 ServletActionContext 获得

public class Demo1 extends ActionSupport {
    @Override
    public String execute() throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        String name = request.getParameter("name");
        HttpSession session = request.getSession();
        System.out.println(name);

        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write("name:张三");
        out.flush();
        out.close();

        return NONE;
    }
}
(2) //继承ActionSupport 类 和 ServletResponseAware 的接口 并重写里面的方法
public class Demo2 extends ActionSupport implements ServletResponseAware {
    private HttpServletResponse response;

    @Override
    public String execute() throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg","访问成功");
        jsonObject.put("code",1);
        String result = jsonObject.toJSONString();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(result);
        out.flush();
        out.close();
        return NONE;
    }

    @Override
    public void setServletResponse(HttpServletResponse httpServletResponse) {
        this.response = httpServletResponse;
    }
}

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

 

(2) Stream

1. action中创建一个InputStream对象

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

3. Struts.xml 中的result标签中设置type=stream

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


public class StreamAction extends ActionSupport {
    private InputStream inputStream;
    @Override
    public String execute() throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg","访问Stream成功");
        jsonObject.put("code",1);
        String result = jsonObject.toJSONString();
        inputStream = new ByteArrayInputStream(result.getBytes("UTF-8"));

        return SUCCESS;
    }

    public InputStream getInputStream() {
        return inputStream;
    }
}

(3) 使用 json 插件

1. 导包

 

2. 建立一个action类

定义你想要返回的json的键名

生成get set 方法

然后在action 访问的方法中为这个json键名的对象进行赋值

  

3.配置struts.xml文件

 

<package name="plugin" namespace="/" extends="json-default">
    <action name="pluginAction" class="cn.hd.ajax.PluginAction" method="execute">
        <result name="success" type="json">

        </result>
    </action>
</package>

重新建package extends 继承不能写struts-default 要写json-default

(在struts-json-plugin包中)

Action不变

Result name 属性不变 type 属性变为json(在struts-default-plugin包中的struts-plugin.xml中)

public class PluginAction extends ActionSupport {
    private String msg;
    private Integer code;
    @Override
    public String execute() throws Exception {
        msg = "访问PluginAction成功";
        code = 1;
        return SUCCESS;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值