Wicket http发布,从servletrequest获取原始数据? [英] Wicket http post, get raw data from servletrequest?

查看:110
本文介绍了Wicket http发布,从servletrequest获取原始数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将一些数据发布到Wicket网页上.如果数据采用表格形式,则可以正常工作.但是,我想用jQuery的ajax-post发布数据.我无法在Page构造函数中获取此数据.

I try do POST some data to a Wicket WebPage. This works fine if the data is in a form. However, I want to post the data with jQuery's ajax-post. I am unable to get this data in my Page-constructor.

这是我的jquery命令:

This is my jquery command:

$.ajax({
                    type: "post",
                    cache: false,
                    url: "http://localhost:8888/testjson",
                    data: JSON.stringify({"aap":"noot"),

                    contentType: 'application/json',
                    success: function(ydata){alert("aap");},
                    failure: function(errMsg) {alert(errMsg);},
                        contentType: false,
                    dataType: "json"
                });

/testjson是已安装的网页.

The /testjson is a mounted WebPage.

public TestJsonApiPage( PageParameters pp )
{
    try
    {
        byte[] data = IOUtils.toByteArray( ( (ServletWebRequest) TestJsonApiPage.this.getRequest() ).getContainerRequest().getInputStream() );
    }
    catch ( IOException e )
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

这是构造函数.我看到的是输入流为空.但是,在调试时,我在WicketApplication

This is the constructor. What I see happening is that the inputstream is empty. However, when debugging, I see the raw data that I posted in HttpServletRequest in the newWebRequest in my WicketApplication

tl; dr 如何在Wicket页面中获取原始帖子数据?

tl;dr How to get the raw post data in Wicket Page?

推荐答案

似乎Page对后参数有作用.

It seems Page does something to the post-parameters.

我的问题的解决方案是使用资源.

The solution for my problem is to use a Resource.

    public class MyResource extends AbstractResource
    @Override
    protected ResourceResponse newResourceResponse( Attributes attributes )
    {

        ResourceResponse resourceResponse = new ResourceResponse();
        resourceResponse.setContentType( "text/json" );
        resourceResponse.setTextEncoding( "utf-8" );

        HttpServletRequest request = (HttpServletRequest) attributes.getRequest().getContainerRequest();

        try
        {
            this.json = IOUtils.toString( request.getInputStream() );
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }

        resourceResponse.setWriteCallback( new WriteCallback()
        {
            @Override
            public void writeData( Attributes attributes ) throws IOException
            {
                OutputStream outputStream = attributes.getResponse().getOutputStream();
                Writer writer = new OutputStreamWriter( outputStream );


                writer.write( MyResource.this.json );
                writer.close();
            }
        } );

        return resourceResponse;
    }

这篇关于Wicket http发布,从servletrequest获取原始数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆