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数据.

时间: 2024-08-05 23:30:58

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

Strut2 的 Action获取JSP 页面参数的方法

struts2 Action获取表单传值1.通过属性驱动式JSP:<form action="sys/login.action" method="post">      <input type="text" name="username">      <input type="submit" value="submit"></form> A

Struts2一个Action内包含多个请求处理方法的处理,method的使用方法,struts2中

struts2的关于method="{1}"意思详解 <action   name= "Login_* "   method= "{1} "   class= "mailreader2.Login ">中Login_*带*是什么意思?method= "{1} "带{}这个是什么意思?====================================================name=

Struts2中关于&quot;There is no Action mapped for namespace / and action name&quot;的总结

今天在调试一个基础的Struts2框架小程序.总是提示"There is no Action mapped for namespace / and action name"的错误.上网查询后发现这是一个初学者经常碰到的问题,导致错误的原因主要有两种.总结如下: 一.struts.xml文件错误.这种错误又分为以下几种:1,struts.xml文件名错误.一定要注意拼写问 题:2,struts.xml文件放置路径错误.一定要将此文件放置在src目录下.编译成功后,要确认是否编译到clas

struts2中 模型驱动(model)action用法

Struts2的Action属于MVC模型层,Action中的方法代表业务逻辑,Action中的属性代表请求中的参数,当页面请求参数较多的时候,把过多的参数对象的属性定义在Action中不太符合Struts所倡导的松耦合原则,所以我们推荐单独用JavaBean来封装参数,在Action中为JavaBean赋值,这就是ModelDriven的Action. 模型驱动的Action要求Action实现ModelDriven接口,假如登录页面需要传输参数name和password,我们把这2个参数封装

项目中的小点_java项目某jsp页面报404

1.将项目文件夹直接放在tomcat服务器的webapps路径下 2.从一个tomcat服务器webapps下拷贝一个项目到另一个tomcat服务器webapps路径下 3.重启tomcat后,访问新saba上的项目,发现其中一个jsp页面报404,别的同路径下的页面都可访问 4.在tomcat的work路径下找到该项目,发现该jsp页面未生成对应的.class文件,所以报404 5.重新部署项目,重启服务器 6.生成对应的.class文件,正常访问

如何将数据库中的值经过servlet传入到jsp页面,并且用EL表达式显示出值

方法一:通过id查询某一数据库表中具体的行,将值封装在相应的对象中,如下面的对象Notice servlet中 String noticeId=request.getParameter("noticeId"); Notice displayEditnotice=publicnoticeservice.displayEditnotice(Integer.valueOf(noticeId)); request.setAttribute("list_displayEditnotic

struts2中错误There is no Action mapped for namespace [/] and action name [] associated with context path

1 There is no Action mapped for namespace [/] and action name [] associated with context path [/Struts2_0300_Action2]. - [unknown location] 2 at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185) 3 at org.apache.struts2.i

Struts2中的Unable to load configuration错误的分析与解决方法

当我们遇到 Unable to load configuration. 这样的错误时,可以根据具体的错误提示找出错误的原因. Unable to load configuration. - interceptor-ref - file:/D:/Java/apache-tomcat-6.0.32/webapps/examquestions/WEB-INF/classes/struts.xml:28:49 at com.opensymphony.xwork2.config.Configuration

JSTL标签库中&lt;c:choose&gt;&lt;/c:choose&gt;不能放JSP页面&lt;!-- --&gt;注释

笔者最近在使用JSTL标签库的<c:choose>标签时候,发现在该标签体中加了JSP的<!-- -->注释时,总是会显示报错信息.错误的信息如下: <span style="font-size:18px;">org.apache.jasper.JasperException: <h3>Validation error messages from TagLibraryValidator for c in /list.jsp</h3&