SPRINGMVC Road Summary (iii)

Source: Internet
Author: User
Tags map class

In the blog "Springmvc Road Summary (a)" and "Springmvc Road Summary (ii)", the framework of application cases are based on the form of XML implementation. However, for large projects, this kind of XML configuration increases the coupling between project modules and makes configuration files more difficult to configure, so, in terms of efficiency, this is not an optimistic approach.

Although this form of XML configuration is relatively small in the project, this form of configuration can clearly understand some of the basics of getting started with SPRINGMVC, and it can be easy to learn. It will be easier to see the annotation implementation after we have learned the XML-based implementation SPRINGMVC. It is because of the cumbersome configuration, increase the coupling between the modules in the program, so the main task today is to use the form of annotations to achieve a very simple springmvc, so that the introductory note sample, understand the basic annotation form of SPRINGMVC, after the case to be implemented, It will be obvious that SPRINGMVC is more convenient and more fit for practical application than the XML form.

For SPRINGMVC in the Note keyword is not the focus of this article, we use the process will summarize some, continuous application, continuous summary. If you want to understand the annotation keywords in springmvc at once, you can view the relevant APIs. This blog post for my study summary, please respect the fruits of labor. Welcome reprint, please keep the source of the blog:http://www.cnblogs.com/itred ; e-mail: [email protected] .

Case

Create a new Web project and add the relevant jar packages;

With the configuration in Web. XML, the Spingmvc-servlet.xml configuration file in this example is placed in the SRC directory, so the relevant configuration needs to be made in Web. Xml. The configuration is as follows:

<servlet>       <Servlet-name>Springmvc</Servlet-name>       <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>       <Init-param>           <Param-name>Contextconfiglocation</Param-name>           <Param-value>Classpath*:springmvc-servlet.xml</Param-value>       </Init-param> </servlet> <servlet-mapping>       <Servlet-name>Springmvc</Servlet-name>       <Url-pattern>/</Url-pattern> </servlet-mapping> 

      Then just like the first learning XML configuration SPRINGMVC, create a new springmvc-servlet.xml , because from now on we should learn to apply SPRINGMVC in the form of annotations, so keep a few classes in mind. In spring's package, there is a WEBMVC jar package under which there is an annotated way to handle the adapter class org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter and a default annotation processing map class org.springframework.web.servlet.mvc.annotation.defaultannotationhandlermapping . These two classes are required to be known because of the loading of these two classes, or in file, it is because of them, will open spring annotations, one is based on the package to find the class, one is based on the class to find methods. Although it is said that the annotation can be enabled by a line of code <mvc:annotation-driven/> overrides, but I still recommend that you need to know these two basic classes.

There is also a need to note that the controller belongs to the package, because here is no longer a one to be configured with XML, so you need to scan the controller, the scan statement in Spingmvc-servlet.xml is:<context: Component-scan base-package= "Com.red.controller"/> . Here, the configuration is basically done, right! As you can see, the configuration you used to make with XML was a lot more than it is now. Even if you create a new controller later, you do not need to configure it again.

Speaking of the controller, of course, no less! Create a new controller under the development package, as this package must be available under the package directory where annotations can be scanned.

The source code is as follows:

 PackageCom.red.controller;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.servlet.ModelAndView; @Controller Public classAnnocontroller {@RequestMapping (value= "/user/adduser", method =requestmethod.get) PublicModelandview AddUser () {System.out.println ("Add User"); String result= "AddUser"; return NewModelandview ("/index", "result", result); } @RequestMapping (Value= "/user/deluser", method =requestmethod.get) PublicModelandview Deluser () {System.out.println ("Del user"); String result= "Del user"; return NewModelandview ("/index", "result", result); }}

The following is a summary of the annotations used in the above controller:

    @Controller is identified above the class name, indicating that the class is a controller, in fact, in the process of use, as long as the controller is added, and when the content of the program is more than a few errors;

    @RequestMapping (value = "/user/adduser", method = Requestmethod. GET) method, the value is the URL that the browser can access, and the method refers to the request method, once it is specified in this way, then the other is not available, so It is strictly a distinction between post and get method, of course, in some cases, need to satisfy both the post and get method, here directly omit the method of the setting, directly ignore this part, also can be value = "/user/adduser" The value of this section is directly modified to:"/user/adduser". Just write the path to the access in parentheses, and you can do the same thing.

Here, you'll see that each method has to go through @requestmapping (), but each one has the same part, so you can extract the same part of the path so you can see exactly what the module is, and @controller it in front of the class name. @RequestMapping ("module name"), it is clear that each section can be assigned to team development.

The implementation effect is as follows: (Here is the Get method, Post method please self-test)

Method One:

  

Method Two:

  

Console output:

  

Summary

For the study of technology, it has to be on its own. Don't know the simple application on the line, this will never understand the reason why people design. The light is pompous on its surface, can not sink the heart, quietly experience its brilliant place, it is inevitable can not really learn this technology.

Case DOWNLOAD Link

Thanks to the struggle of their own!  

  Email:[email protected]  Personal Blog: http://itred.cnblogs.com  Copyright Notice: This article is copyrighted by the author and the blog Park, Welcome to reprint, but please mark the article in a conspicuous position. I reserve all rights to be held accountable for his use without my written consent. 

SPRINGMVC Road Summary (iii)

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.