What the hell is @controller and @requestmapping in SPRINGMVC?

Source: Internet
Author: User

What is 1.1 @Controller?
First look at an example:

  @Controller  @RequestMapping ( "/blog") public  class blogcontroller {@ Requestmapping ( "/index")  Public Modelandview index (HttpServletRequest request) { Modelandview Mav = new Modelandview ( "/index"); String CTX = Request.getcontextpath (); System.out.println (CTX); Mav.addobject ( "CTX", CTX); return Mav;} } 

@Controller means that when Tomcat starts, it loads the class as a controller into the spring Bean factory, which, if not added, is an ordinary class, and spring does not have a half-dime relationship.

The following are two common configurations:

<!-- 开启注解模式驱动 --><mvc:annotation-driven></mvc:annotation-driven><!-- 扫包 --><context:component-scan base-package=*"com.blogMgr.*"*></context:component-scan>

Where Base-package says it scans all packages in the Com.blogmgr directory, and once a class is found with a @controller-like annotation, the system loads it into the spring bean factory and instantiates it when the container is started.

That's why we just wrote the controller, but there's never been a new controller in a place because the controller was loaded into the bean factory by spring when the Web container started.

This is the so-called Spring sweep package mechanism. @Controller is an annotation, and when Tomcat launches, we'll see some Java classes waving flags with @controller and shouting: "Hey,springmvc,i ' m here,please take me to your bean factory! "

What is 1.2 @RequestMapping?

In the controller, will always see requestmapping this annotation, looks like the path of the jump, the following list is a convenient metaphor for our memory.

For example, one day, I found a movie pretty good, in D set up a folder, called "movie". There are two movies in it, each of which is stored in a folder.

In, we can see that the path to it is "D:\ movie", and there is a folder under the path "Crazy Animal City 2016", inside is like this

So, the specific path to the file is "D:\ movie \ Crazy Animal City 2016", now I want to access this resource, in addition to double-click, is not only need to enter in the Address bar:"d:\ movie Crazy Animal city 2016\ Crazy Animal City 2016.mp4" can also?

Yes, of course I can.

Succeeded, we get the resource file we want through the URL way!

Now let's copy this file and copy it to the same path.

If I try to change the name of the first MP4 file to "Mad Animal City 2016.mp4", you'll get a hint like this

It can be seen that under the same path, there cannot be two files with duplicate names.

Similarly, if I set two identical requestmapping in the same controller

Compilation is possible, but when I start Tomcat,

I access it via the URL "http://localhost:8088/BlogMgr/blog/index" and I get an error:

Org.springframework.beans.factory.BeanCreationException:Error creating bean with Name ' Org.springframework.web.servlet.mvc.method.annotation.requestmappinghandlermapping#0 ': Invocation of Init method Failed Nested exception is java.lang.IllegalStateException:Ambiguous mapping found. Cannot map ' BlogController ' Bean method

Ambiguous mapping found said there is a path ambiguous, that is, the name of the path, the system can not be mapped, so the error. This and cannot create two files of the same name under the same folder.

So what if I had to create another "crazy Animal city 2016.mp4" file?

Then, I'll put this file in another folder.

Similarly, I can also build a Controller,controller class above the requestmapping called "/blog2", and then write an identical requestmapping in it is certainly no problem.

Summary

A Web project is itself a system, like an operating system, that can treat a project as a system, an application. Why do people use computers, because computers can

1. Give us the resources we want (e.g.. avi)

2. Help us do things.

In a system, if there is no graphical interface, we have to access a resource, must be accessed through a black window, that is, through the path to access. a b/s architecture of the Web project, is a similar to the command line of the application, we only through the URL, that is, the path to get the resources and services we want.

To see Requestmapping, the final correspondence must be a method , the function of the method is to do some business operations, or return a thing.

Like what

That's how we get the JSP page we want, and Requestmapping's role is to provide a handle that allows us to access the corresponding method and ultimately get what we want. In summary, requestmapping is a mapping path.

The role of 1.3 @ResponseBody
In the controller, we can often see @responsebody this annotation, it is very simple, it means that the method returned by the way the IO stream is written to the browser.

For example, let's write a method:

@RequestMapping("/testResponseBody")@ResponseBodypublic String testResponseBody(HttpServletRequest request){ return "

Finally, the effect in the browser is this:

What the hell is @controller and @requestmapping in SPRINGMVC?

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.