SPRINGMVC Learning (11)--springmvc realize resultful service

Source: Internet
Author: User

RESTful is a resource positioning and resource operation style, not the standard is not a protocol, but a style, is the interpretation of the HTTP protocol.

    • Resource positioning: All things on the internet are resources, requiring no verbs in the URL, only nouns, no parameters. The style of the URL request is like this:

      http://blog.csdn.net/eson_15/article/details/51743514

      Resource operation: Use PUT, delete, post, get and other methods to operate the resources, respectively, corresponding to add, delete, modify, query. Generally used or post and get,put and delete are hardly used.

Now there is a requirement: use restful ways to make product information inquiries. Need, we need to solve the demand. We can transform the EditItem method in the Itemcontroller class into:

 @RequestMapping ("/itemedit/{id}" )  //    Public  String EditItem (@PathVariable ("id"  
     
      //
       Invoke service  items items =     Itemservice.getitembyid (IID);     //  pass data to the page, need to use the model interface      Model.addattribute ("Item"  //  Return to logical view   return  "EditItem" ;}  

@RequestMapping(value="/itemEdit/{id}"): {XXX} represents a placeholder, the requested URL can be "/ITEMEDIT/1" or "/ITEMEDIT/2", by using @pathvariable in the method to get the XXX variable in {XXX}. @PathVariable is used to map template variables in the request URL to the parameters of a functional processing method. If @requestmapping is represented as "/viewitems/{id}", the ID and formal parameter names are the same, then @pathvariable does not have to specify a name.

In addition to this, configure the rest in the front-end controller to transform the front-end controller configuration in the Web. xml file to:

 <!--Configuring the front-end controller--><servlet> <servlet-name>springmvc</servlet-name> <servlet-class  >org.springframework.web.servlet.dispatcherservlet</servlet-/*  : Intercepts all requests, including JSPs, should be configured "/" and <url-pattern>/</url-pattern></ Servlet-mapping>  

In the SPINGMVC framework, / all requests are intercepted, but the JSP is not intercepted, which means that /* all interception, including JSPs. Here obviously should be configured / , because you want to ah! If the Controller class method inside has finished processing, to jump to the JSP page, if you configure /* , this JSP page jump will be intercepted, will be reported 404 error.
To facilitate testing, the modified hyperlinks in itemlist.jsp are converted to:

<td><a href= "${pagecontext.request.contextpath}/item/itemedit/${item.id}" > Modify </a></td>


The final test result is:



However, there is a problem, using the above configuration will block all the URLs (although not including JSP), then the static resources will be intercepted, so Dispatcherservlet will also parse the static resources, but this will be an error, so we have to set up not to let it parse static resources. The SPRINGMVC <mvc:resources mapping="" location=""> tag allows for mapping access to static resources. Here is the access configuration for the JS file:

<mvc:resources location= "/js/" mapping= "/js/**"/>


If we put the JS folder in the Web-inf directory of the project, it is certainly not accessible to the outside world, but we have to visit it? It is then possible to <mvc:resources mapping="" location=""> map the access by tag, as follows:

SPRINGMVC Learning (11)--springmvc realize resultful service

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.