Java spring mvc 有 response.write 直接输出到浏览器吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3146461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does spring mvc have response.write to output to the browser directly?
提问by Blankman
I am using spring mvc with freetemplate.
我正在使用带有 freetemplate 的 spring mvc。
In asp.net, you can write straight to the browser using Response.Write("hello, world");
在 asp.net 中,您可以使用 Response.Write("hello, world"); 直接写入浏览器;
Can you do this in spring mvc?
你能在 spring mvc 中做到这一点吗?
采纳答案by Bozho
You can either:
您可以:
get the
HttpServletResponse
and print to itsWriter
orOutputStream
(depending on whether you want to send textual or binary data)@RequestMapping(value = "/something") public void helloWorld(HttpServletResponse response) { response.getWriter().println("Hello World") }
Use
@ResponseBody
:@RequestMapping(value = "/something") @ResponseBody public String helloWorld() { return "Hello World"; }
获取
HttpServletResponse
并打印到其Writer
或OutputStream
(取决于您要发送文本数据还是二进制数据)@RequestMapping(value = "/something") public void helloWorld(HttpServletResponse response) { response.getWriter().println("Hello World") }
@RequestMapping(value = "/something") @ResponseBody public String helloWorld() { return "Hello World"; }
Thus your Hello World
text will be written to the response stream.
因此,您的Hello World
文本将被写入响应流。
回答by Ben J
If you use an annotated controller (or non-annotated for that matter I believe...), you can use the method argument HttpServletResponse
in your controller to get the output stream and then write to the screen - see http://download.oracle.com/docs/cd/E17410_01/javaee/6/api/javax/servlet/ServletResponse.html#getOutputStream%28%29
如果您使用带注释的控制器(或者我相信没有注释...),您可以使用HttpServletResponse
控制器中的方法参数来获取输出流,然后写入屏幕 - 请参阅http://download.oracle .com/docs/cd/E17410_01/javaee/6/api/javax/servlet/ServletResponse.html#getOutputStream%28%29
For more information about the parameters you can use in your controllers/handlers, see http://static.springsource.org/spring/docs/2.5.x/reference/mvc.html(section 13.11.4)
有关您可以在控制器/处理程序中使用的参数的更多信息,请参阅 http://static.springsource.org/spring/docs/2.5.x/reference/mvc.html(第 13.11.4 节)
回答by Stephen C
I'm sure it is possible in some contexts. For example, if you have the HttpServletResponse
object available to you (as you do in a Controller
, or if you write your own View
), then you can call getWriter()
or getOutputStream()
and write to that.
我相信在某些情况下这是可能的。例如,如果您有HttpServletResponse
可用的对象(就像您在 a 中所做的那样Controller
,或者如果您编写自己的View
),那么您可以调用getWriter()
或getOutputStream()
写入该对象。
But you need to be careful to make sure that what you are doing doesn't interfere with your use of FreeMarker templates. And I'm not sure if you could manage it from within a FreeMarker template.
但是你需要小心确保你正在做的事情不会干扰你对 FreeMarker 模板的使用。而且我不确定您是否可以从 FreeMarker 模板中管理它。
回答by razor
If you want to send something to OutputStream, even if you are using Freemaker, just use @ResponseBody
如果您想向 OutputStream 发送一些内容,即使您使用的是 Freemaker,也只需使用 @ResponseBody
example:
例子:
@RequestMapping(value = "report1", method = RequestMethod.GET, produces = "application/pdf")
@ResponseBody
public void getReport1(OutputStream out) {