What the hell is @Controller and @RequestMapping in SpringMVC?

1494897625777093133.jpg

1.1 What is @Controller Let's
look at an example first:

@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, this class is loaded into Spring's Bean factory as a controller. If it is not added, it is an ordinary class, which has nothing to do with Spring.

Here are two common configurations:

<!-- 开启注解模式驱动 -->

<mvc:annotation-driven></mvc:annotation-driven> <!-- 扫包 --> <context:component-scan base-package=*"com.blogMgr.*"*></context:component-scan>

 

Among them, base-package means that it will scan all the packages in the com.blogMgr directory. Once a class is found to have an annotation similar to @Controller , the system will load it into Spring's Bean factory when the container starts, and instantiate it.

 

This is why, we just wrote the Controller, but never new the Controller in one place, because when the Web container starts, the Controller has been loaded into its own Bean factory by Spring.

 

This is the so-called Spring package scanning mechanism. @Controller is an annotation, when tomcat starts, we will see some JAVA classes waving flags with @Controller and shouting : "Hey, SpringMVC, I'm here, please take me to your bean factory!"

 

1.2 What is @RequestMapping

In the Controller, you will always see the RequestMapping annotation, which looks like a path jump. The following is a metaphor that is convenient for us to remember.

 

For example, one day, I found that a movie was very good, so I created a folder on the D drive called "movie". There are two movies inside, each with a folder to store.

1494897692559029057.png 

 

In the above picture, we can see that its path is "D:\Movies", and there is another folder [Zootopia 2016] under this path, which is like this

1494897702152088854.png 

Then, the specific path of the file is "D:\Movie\Zootopia 2016", and now I want to access this resource, in addition to double-clicking, do I just need to enter in the address bar: "D:\Movie\Crazy Animals" City 2016\Crazy Zoo 2016.mp4" can also be?

1494897725949001346.png

 

Yes, of course.

1494897735746062558.png 

It was successful, we got the resource file we want by url!

 

Now we make a copy of this file and copy it to the same path

1494897745480004347.png 

If I try to also change the name of the first MP4 file to "Zootopia 2016.mp4", a prompt will pop up as follows

1494897751918040594.png 

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

 

Similarly, if I set two identical RequestMappings in the same Controller

1494897766027073622.png 

 

Compilation is ok, but when I start tomcat,

I access it through the url " http://localhost:8088/BlogMgr/blog/index", and an error will be reported :

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 indicates that there is an ambiguous path, that is, the path has the same name, and the system cannot perform mapping, so an error is reported. This is the same as the inability to create two files with the same name under the same folder.

 

So what if I have to create another "Zootopia 2016.mp4" file

1494897778730063720.png 

1494897786777045482.png 

 

Then, I put this file inside another folder and it works.

 

In the same way, I can also build another Controller. The RequestMapping on the Controller class is called "/blog2", and there is definitely no problem in writing an identical RequestMapping in it.

 

summary

A web project itself is a system, and like an operating system, the project can be regarded as a system, an application. Why do people use computers, because computers can

 

1. Give us the resources we want (such as .avi)

2. Help us do things.

 

In a system, if there is no graphical interface, if we want to access a resource, it must be accessed through a black window, that is, through a path. A web project with B/S architecture is an application similar to the command line. We can only get the resources and services we want through the url, that is, the path.

 

Let's look at RequestMapping, the final corresponding must be a method , the function of the method is nothing more than to perform some business operations, or return something.

 

for example

1494897796684010043.png 

 

We get the desired jsp page through this method. The function of RequestMapping is to provide a handle so that we can access the corresponding method and finally get what we want. In summary, RequestMapping is a mapping path.

  

1.3 The role of @ResponseBody is
in the Controller. We can often see the annotation @ResponseBody . Its meaning is very simple, which means that what this method returns will be written to the browser through the IO stream.

For example, we write a method:

@RequestMapping("/testResponseBody")
@ResponseBody
public String testResponseBody(HttpServletRequest request){ return "<h1 style='color:lightGreen'>Hello Web!</h1>"; }

The final result obtained in the browser is as follows:

1494897809137032549.png

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325219298&siteId=291194637