SpringMVC series annotation optimized version -- 04

Source: Internet
Author: User

SpringMVC series annotation optimized version -- 04

Abstract: springMVC is not exposed, efficient development, flexibility, low coupling, seamless connection with spring, and other advantages are lingering. Although previous contacts have been revealed, they are not much obvious, it is optimized here, and the simplified and characteristic of the optimized Annotation Version can be seen.

 

I. Optimization

1. Use the mvc tag instead of setting the two beans to be scanned

2. Add "package space" to different controllers to avoid request conflicts, reduce the amount of code, and do not need to write the completed access path on each method.

3. The method can process both get requests and post requests.

4. Directly @ RequestMapping (url ing) on the method ). In this way, the value is "url ing simultaneously processes get and post

5. Change the return value, no longer return ModeAndView, directly return a string specifying the name of the corresponding page, as to how to pass parameters, use HttpServletRequest

HttpServletRequest can be obtained by parameters in the method, a request can be provided, a response can be provided at the same time, it is really powerful and flexible.

Exp: add () method xxx add (HttpServletRquest request, HttpServletResponse response) {xxx} under UserController}

 

II. Specific Optimization

The code and code are provided to describe the optimization areas and features.

1, springAnnotation-servlet.xml:

 

2. OptimizedUserController:

 

 

Package com. chy. web. controller. annotation; import javax. servlet. http. httpServletRequest; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; @ Controller @ RequestMapping (/user2) // The Global url is extracted from the previous one. In this way, the original field in each method below does not need to be repeatedly written into the public class OptimizedUserController {/** Original: @ RequestMapping (value =/user/toUser, method = RequestMethod. GET) * New: @ RequestMapp Ing (/toUser) * 1. Put forward/user, * 2. Lack of method. Now many requests are sent using ajax. If so, we need to * determine whether it is get or post or yes. it is annoying. If there are no strict requirements, you can make it * both get and post. Simply put: Do not write the method attribute directly. * 3. The method is missing, and there is a value. Since each method has value =, not only does it not achieve * reusability, it is uncomfortable to look at, and is omitted directly. */@ RequestMapping (/toUser) public String toUser () {return/annotation;}/** Original: return new ModelAndView (/annotation, paramName, object); * new: return/annotation; * 1. Do not use the ModelAndView object * 2. Use the response page name String * 3. Use the original request to pass the parameter * 4 to the page. You can pass the parameter HttpServletRequest or HttpServletResponse to obtain the request, response, and * parameters, you can pass them to the method. You can do either or not. */@ RequestMapping (/addUser) public String addUser (HttpServletRequest request) {String result = this is addUser -------; request. setAttribute (result, result); return/annotation;} @ RequestMapping (/delUser) public String delUser (HttpServletRequest request) {String result = this is delUser -------; request. setAttribute (result, result); return/annotation ;}}

 

More: springMVC series directory -- 00

 

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.