SPRINGMVC Road Summary (iii)
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>
Then just like the beginning of learning XML configuration SPRINGMVC, new Springmvc-servlet.xml, because from now on to learn to use annotations in the form of SPRINGMVC, so remember several classes. In spring's package, there is a WEBMVC jar package, There is an annotated way to handle the adapter class Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter and a default annotation processing mapping class Org.springframe Work.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping. These two classes are necessary to know, because the loading of these two classes, or the two beans in the Springmvc-servlet.xml file, is because of them, the spring annotations are opened, one is based on the package to find the class, one is based on the class to find methods. While it's possible to use a single line of code <mvc:annotation-driven/> overrides when enabling annotations, I recommend that you 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 need to scan the controller, in the Spingmvc-servlet.xml scan statement 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:
Package Com.red.controller;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;@ Controllerpublic class Annocontroller { @RequestMapping (value = "/user/adduser", method = Requestmethod.get) Public Modelandview AddUser () { System.out.println ("Add User"); String result = "AddUser"; return new Modelandview ("/index", "result", result); @RequestMapping (value = "/user/deluser", method = requestmethod.get) public Modelandview Deluser () { System.out.println ("del user"); String result = "del user"; return new Modelandview ("/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 for method refers to the request methods, once this method is specified in the request mode, then the other is not available, so it is strictly to distinguish the post and get method Of course, in some cases, it is necessary to satisfy both the post and get methods, the setting of method is omitted directly, and the value of value = "/user/adduser" can be 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 be written over @requestmapping (), but each has the same part, so you can extract the same part of the path so you can see exactly what the module is, Add @requestmapping ("module name") to the front @controller of the class name, so you can clearly assign each part 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
SPRINGMVC Road Summary (