@requestmapping problem in SPRINGMVC and the return value problem of controller method

Source: Internet
Author: User

@requestMapping

1. URL mapping

Defines the URL for the controller method, which is used for processor mapping

@RequestMapping ("/items")

public class Itemscontroller {...}

2. Narrowing request Mapping

3. Restricting the HTTP request method

For security reasons, the link to the HTTP method is limited.

If the limit request is a post method, make a get request with an error:

But because method={} is an array, you can write the post and Get methods in

The return value of the Controller method

1. Return to Modelandview

At the end of the method, define the Modelandview and set the model and view separately:

Modelandview.addobject ("item", Itemscustom);

Modelandview.setviewname ("EditItem");

2. Return string

If the Controller method returns a string

A. Represents the return logical view name . (the so-called logical view name is: The prefix and suffix are configured in Springmmvc, so the specific path is reduced to the path of removing the prefix and suffix)

Real View (JSP path ) = prefix + Logical View name + suffix

B,redirect redirect

After the product modification is submitted, redirect to the product query list.

REDIRECT redirection feature: The URL in the browser address bar will change. The request data for the modification submission cannot be uploaded to the redirected address. Re- requestafter redirection(request cannot be shared)

C, forward page forwarding

Page forwarding via forward, the URL of the browser address bar is not changed,request can be shared.

3. return void

request and response can be defined on the controller method parameters, using the request or Response Specify the response result:

a , using Request turn to the page as follows:

Request.getrequestdispatcher ("page path"). Forward (request, response);

b , you can also pass Response page redirection:

Response.sendredirect ("url")

C , you can also pass Response Specify response results, such as response JSON The data are as follows:

Response.setcharacterencoding ("Utf-8");

Response.setcontenttype ("Application/json;charset=utf-8");

Response.getwriter (). Write ("JSON string");

 

@requestmapping problem in SPRINGMVC and the return value problem of controller method

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.