@responsebody annotation Analysis of controller in SPRINGMVC

Source: Internet
Author: User
Tags html tags

Demand analysis: the need to use an Out object to return to the Tenpay to receive success. Then you will need the following code:

[Java] view plain Copy/** * Returns processing results to the Tenpay server.   * @param msg:success or fail.       * @throws IOException */public void sendtocft (String msg) throws IOException {string strhtml = msg;       PrintWriter out = This.gethttpservletresponse (). Getwriter ();       Out.println (strhtml);       Out.flush ();      Out.close (); }
Then the method in the controller is annotated with this method:


[Java] view plain copy//tenpay return URL @ResponseBody @RequestMapping ("/pay/tenpay") pu Blic String Tenpayreturnurl (httpservletrequest request, httpservletresponse response) throws Exception {UNPACKC           Ookie (request, response);           Payreturnurl.payreturnurl (request, response);       return "Pay/success"; }

Using this method, the annotation will output a string on the page visited: strhtml, without jumping to the pay/success.jsp page

The reasons are:

@ResponseBody

Role:

The annotation is used to write the object returned by the controller's method to the body data area of the response object after the appropriate httpmessageconverter is converted to the specified format.

Use time:

The returned data is not a page of HTML tags, but is used in some other form of data (JSON, XML, etc.);


Then just delete the note: @ResponseBody can return to page pay/success.jsp. It also achieves the effect of interacting with the client behind the scenes.   That is: [Java] view plain copy out.println (strhtml); And does not output strings on the page.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.