SpringMVC series Annotation Version -- 03

Source: Internet
Author: User

SpringMVC series Annotation Version -- 03

Abstract: springMVC of Annotation Version can be used to access pages, test GET methods, and POST methods.

 

I. Implementation sequence

1. to retain the content of the previous project, here to create a springMVC configuration file: springAnnotation-servlet.xml, of course, to change the web. xml in the configuration of the Load file path.

2. enable the use of annotation in the way configured in springAnnotation-servlet.xml,

3. Configure the packages to be scanned, that is, which of the following classes are implemented using annotation.

4. Create a specific Controller under the specified package scanned by annotation, pay attention to the use of annotation, temporarily class annotation, method annotation, and specific meaning code.

 

Ii. Detailed steps

1, the configuration of the implementation of springAnnotation-servlet.xml, one is to open, one is to scan, the Code has Annotation

 

2. Specific UserController: The specific code is also described.

 

 

Package com. chy. web. controller. annotation; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. bind. annotation. requestMethod; import org. springframework. web. servlet. modelAndView; // class annotation, indicating that this class uses the annotation form, will be scanned by springMVC @ Controllerpublic class UserController {// method annotation, there are two parameters: value indicates the request to use this method, the type of the request to be processed by the method, and the error @ RequestMapping (value =/user/toUser, method = RequestMethod. GET) public ModelAndView toUser () {return new ModelAndView (/annotation);} @ RequestMapping (value =/user/addUser, method = RequestMethod. POST) public ModelAndView addUser () {String result = this is addUser -------; return new ModelAndView (/annotation, result, result) ;}@ RequestMapping (value =/user/delUser, method = RequestMethod. GET) public ModelAndView delUser () {String result = this is delUser -------; return new ModelAndView (/annotation, result, result );}}

 

3. Test the GET and POST methods.

A) Enter http: // localhost: 8080/springMVC4/user/toUser in the address bar to access annotation. jsp page (in this case, the url request is in get mode, and the "/user/toUser" method in UserController is also requested in get mode, and can be accessed normally at this time) because no parameters are passed to annotation. jsp, so annotation. jsp does not display the reuslt result in the page div.

B) Click annotation. jsp form submission button, post submission, request the addUser method of UserController through action = "/springMVC/user/addUser", but the two are inconsistent, access failure, when the UserController when the request method of the addUser method of is changed to post, the access is normal,

C) try it by yourself. No more textures, water, and more water...

Supplement:

1. The request path defined by the UserController method is/user/addUser, while the request in the form action is/springMVC/user/addUser.

2. The entire project structure:

 

 

More: springMVC series directory -- 00

 

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.