Bu Ruo's Code Note Series - Web Series - MVC Framework - How to use @RequestMapping-3001

[email protected] to map requests in mvc, which can be processed by the controller Some URL requests. Equivalent to Servlet in web.xml The mapping of the configuration works consistently

as I wrote here <servlet>

    <servlet-name>test</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> 

</servlet> <servlet-mapping> <!-- Map servlet requests to a url --> <servlet-name>test</servlet-name> <url-pattern>*.test</url-pattern> </servlet-mapping>

I am actually creating a servlet here and then The mapping of url (with .test) action to this servlet

, then web.xml is actually a web server program Entrance, after starting here, it will be created according to you Servlet: <servlet>

    <servlet-name>test</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> 

</servlet> Will load a test-servlet.xml file and start

in this test-servlet.xml, we observe <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Xmlns:context="http://www.springframework.org/schema/context" Xmlns:mvc="http://www.springframework.org/schema/mvc" Xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

<!-- We need to define a mapper --> <bean class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name = "mappings"> <props> <prop key = "Start.test">startController</prop> <prop key = "Login.test">startLogin</prop> <prop key = "login_success.test">login_success</prop> </props> </property> </bean> <bean class = "org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value = "/WEB-INF/jsp/"/> <property name="suffix" value = ".jsp"/> </bean> <!-- We define the controller here --> <bean id = "startController" class = "test.testControoler"> </bean> <bean id = "startLogin" class = "org.springframework.web.servlet.mvc.ParameterizableViewController"> <property name="viewName" value = "login"></property> </bean> <bean id = "login_success" class = "test.CommondControllerTest"> </bean> </beans> In this way, the bean is injected into the controller. For example, here is created a <bean id = "startController" class = "test.testControoler"> </bean> Controller, then we can map some requests Put it all here <property name = "mappings"> <props> <prop key = "Start.test">startController</prop> <prop key = "Login.test">startLogin</prop> <prop key = "login_success.test">login_success</prop> </props> </property> Then, our controller actually implements a controller base class. There is one inside Public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { System.out.println("2333333333333"); Return new ModelAndView("main"); } This function directly implements the url request done using the mapping method. Ok, understand this, we have a need now, that is in a controller There are more than one request mapped inside, so we started to introduce @RequestMapping to map requests.

Next, I need to post the source code of @RequestMapping Public @interface RequestMapping { String name() default ""; String[] value() default {}; String[] path() default {}; RequestMethod[] method() default {}; String[] params() default {}; String[] headers() default {}; String[] consumes() default {}; String[] produces() default {}; }

Simple to see this, I can't understand it, so we still It’s good to repeat the whole process. First of all, when our html sends a request, How does it submit the request (a url)

tag in the action Like this Action="user/login"

<form action="user/login" method="post"> <input type="submit" value="Use Post to send request"/> </form> After this request is submitted to the server, it will be transferred to the controller. Note that we review the simplest serlvlet above We define a servlet in web.xml and will all The .test request is given in full to the servlet, inside the servlet. The specific controller is defined in detail.

and it seems that using the @RequestMapping method seems to be straightforward Skip this process and map the url request directly to java Going inside the code? ? No, it always feels wrong because you didn't instantiate your controller. Without an instance, you can't do anything! Oh, yes, I checked the information and respected everyone’s labor results as our programmers. The first code! Like me, I will post the information I check, when you quote the information. Also post it out wow!

https://my.oschina.net/kolbe/blog/509686

So, to sum up, you also need to define this servlet

<servlet> <servlet-name> mappingTest</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>mappingTest</servlet-name> <!-- can answer all requests, that is, hand all requests to the mappingTest servlet to handle --> <url-pattern>/</url-pattern> </servlet-mapping>

ok, we define a servlet, then we need Recall that you defined the servlet, you need a xx-servlet.xml To initialize your servlet is

so we need to create a

mappingTest-servlet.xml in the src root directory. document And initialize this servlet inside as a mapped servlet

... in research! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !

哈哈, after some research, I finally came out. First of all, you have to define the file path of this xx-servlet.xml. Write in the web <servlet> <servlet-name> mappingTest</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- classpath indicates the mappingTest-servlet.xml file in the path of the class (src root directory) --> <param-value>classpath:mappingTest-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>

This <init-param> tag can define the initialization path

. Next, you need to initialize the contents of the mappingTest-servlet.xml file

<?xml version="1.0" Encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Xmlns:context="http://www.springframework.org/schema/context" Xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- Automatically scan classes with Spring component annotations under cn.kolbe.spring.mvc --> <context:component-scan base-package="mapping.test.controller"/>

<!-- Configure View Parser: Parse the return value of the controller method to the actual physical view --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- Assuming the controller returns a success string, the view resolver will automatically assemble, and the specific view will be: Prefix + returned string + suffix = /WEB-INF/views/success.jsp --> <!-- prefix indicates prefix --> <property name="prefix" value="/WEB-INF/jsp/"></property> <!-- suffix means suffix --> <property name="suffix" value=".jsp"></property> </bean> </beans>

ok, let me explain <context:component-scan base-package="mapping.test.controller"/> This sentence configures the package where this controller is located, ok, so we solve the controller Instantiation problem.

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- Assuming the controller returns a success string, the view resolver will automatically assemble, and the specific view will be: Prefix + returned string + suffix = /WEB-INF/views/success.jsp --> <!-- prefix indicates prefix --> <property name="prefix" value="/WEB-INF/jsp/"></property> <!-- suffix means suffix --> <property name="suffix" value=".jsp"></property> </bean> Used to describe the subcontracting of your request, that is, after any request comes in, it will go to <context:component-scan base-package="mapping.test.controller"/> Defined controller inside the package These controllers (a class instance) will exist like this @RequestMapping(url) The url request map, through this map you can transfer the url request! Yes, super easy! ! ! ! (mmp? It took me 2 hours??)