Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does spring mvc have response.write to output to the browser directly?

I am using spring mvc with freetemplate.

In asp.net, you can write straight to the browser using Response.Write("hello, world");

Can you do this in spring mvc?

like image 619
Blankman Avatar asked Jun 30 '10 04:06

Blankman


2 Answers

You can either:

  • get the HttpServletResponse and print to its Writer or OutputStream (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";
    }
    

Thus your Hello World text will be written to the response stream.

like image 165
Bozho Avatar answered Sep 27 '22 16:09

Bozho


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

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)

like image 45
Ben J Avatar answered Sep 27 '22 17:09

Ben J