Wicket http帖子,从servletrequest获取原始数据?

时间:2013-07-26 06:49:07

标签: ajax servlets post wicket

我尝试将一些数据POST到Wicket WebPage。如果数据在表单中,这可以正常工作。但是,我想用jQuery的ajax-post发布数据。我无法在我的Page-constructor中获取此数据。

这是我的jquery命令:

$.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是一个挂载的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();
    }

这是构造函数。我看到的是输入流是空的。但是,在调试时,我会在HttpServletRequest

中的newWebRequest中看到我在WicketApplication中发布的原始数据

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

1 个答案:

答案 0 :(得分:2)

似乎Page对后置参数做了些什么。

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

    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;
    }