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

前端 未结 4 770
盖世英雄少女心
盖世英雄少女心 2020-12-08 09:01

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

4条回答
  •  失恋的感觉
    2020-12-08 09:31

    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.

提交回复
热议问题