Struts2中如何接收另一个action 或者JSP页面经过POST方法传过来的字符串

经验总结:

最近在用struts2 写接口,要给请求者返回一个json字符串,但是,请求是用POST请求的,各种方法尝试,最后终于得到了答案:

/**
     * 封装接收客户端传过来的post数据
     * @param ctx ActionContext的对象 
     * @return
     */
    public static String getRequestBody(ActionContext ctx){
        try {
            HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
            InputStream inputStream = request.getInputStream();
            String strMessage = "";
            StringBuffer buff = new StringBuffer();
            BufferedReader bufferReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8")); 
            while((strMessage = bufferReader.readLine()) != null){
                buff.append(strMessage);
            }
            bufferReader.close();
            inputStream.close();
            return buff.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

经过这个方法后,在另一个需要使用到POST的json的时候,只需要

ActionContext ctx = ActionContext.getContext();
String json = getRequestBody(ctx);

上面那个json就是POST中的json数据.

转载于:https://www.cnblogs.com/llynic/p/6613515.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值