简体   繁体   English

返回Tapestry 5中的非HTML,非JSON HTTP正文吗?

[英]Return non-HTML, non-JSON http bodies in Tapestry 5?

I have to implement the service provider of the OAuth protocol in a project that uses Tapestry5. 我必须在使用Tapestry5的项目中实现OAuth协议的服务提供程序。 Therefor I just need to return a very simple HTTP response body that is neither HTML or JSON. 因此,我只需要返回一个既不是HTML也不是JSON的非常简单的HTTP响应主体。

At first I tried to use the standard tml & pojo (java class, page) approach but this doesn't work because Tapestry tries to parse the templates. 最初,我尝试使用标准的tml&pojo(java类,页面)方法,但是这种方法行不通,因为Tapestry试图解析模板。

So I think I have to try something different. 所以我认为我必须尝试一些不同的东西。 Maybe it is possible to use a render() method in a page? 也许可以在页面中使用render()方法? But I couldn't find any documentation that would answer this question. 但是我找不到任何可以回答这个问题的文档。

Or should I just use another framework that would better fit my needs? 还是应该使用另一个更适合我需求的框架?

Thank you for your advice, 感谢您的意见,

Richard 理查德

Brian pushed me in the right direction, but the actual solution to the problem was even simpler: Brian将我推向正确的方向,但实际解决问题的方法却更加简单:

StreamResponse onActivate() {
     return new TextStreamResponse("text/plain", "foo=bar");
}

You can stream text directly from the page without using a template: 您可以直接从页面流式传输文本,而无需使用模板:

StreamResponse onActivate() {
  return new StreamResponse(
    public String getContentType() {
      return "text/plain";
    }

    public InputStream getStream() {
      return new ByteArrayInputStream("foo=bar".getBytes());
    }

    public void prepareResponse(Response response) {
      // response.setHeader(...
    }
}

If you were doing it for a lot of pages, I think you could contribute your own DocumentLinker that lets you bypass all the xml/html/head stuff that Tapestry adds to the page by default. 如果您要在很多页面上使用它,我想您可以贡献自己的DocumentLinker,让您绕过Tapestry默认添加到页面的所有xml / html / head内容。 Then you could go back to using templates. 然后,您可以返回使用模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM