Write a simple demo of SpringMvc

1. The introduction of frame packet associated springmvc

 

 

 2. Configure DispatcherServlet in web.xml

 1   <servlet>
 2       <servlet-name>springMVC</servlet-name>
 3       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 4       <init-param>
 5           <param-name>contextConfigLocation</param-name>
 6           <param-value>classpath:springMVC-annotation.xml</param-value>
 7       </init-param>
 8   </servlet>
 9   
10   <servlet-mapping>
11       <servlet-name>springMVC</servlet-name>
12       <url-pattern>*.do</url-pattern>
13   </servlet-mapping>

3. Configure springMvc profile

. 1 <XML Version = "1.0" encoding = "UTF-. 8"??>
 2 <Beans xmlns = "http://www.springframework.org/schema/beans"
 . 3      xmlns: the xsi = "HTTP: // WWW. w3.org/2001/XMLSchema-instance "
 4      xsi: schemaLocation =" http://www.springframework.org/schema/beans
 5          HTTP: // www.springframework.org/schema/beans/spring-beans.xsd " > 
. 6          
. 7          
. 8      <! - If no configuration handMappering a system will have a default HandleMapping ->
 . 9      
10      < the bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> </ the bean> 
 . 11      <!
- Register with the controller class itself -> 12     <bean name="/my.do" class="com.zhiyou100.mcl.controller.MyController"></bean>   
13     <!-- 配置视图解析器 -->
14     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
15         <property name="prefix" value="/WEB-INF/view/"/>
16         <property name="suffix" value=".jsp"/>
17     </bean>
18 </beans>

4. Common HandlerMapping

. 1 <XML Version = "1.0" encoding = "UTF-. 8"??>
 2 <Beans xmlns = "http://www.springframework.org/schema/beans"
 . 3      xmlns: the xsi = "HTTP: // WWW. w3.org/2001/XMLSchema-instance "
 4      xsi: schemaLocation =" http://www.springframework.org/schema/beans
 5          HTTP: // www.springframework.org/schema/beans/spring-beans.xsd " > 
. 6          
. 7          
. 8      <! - If no configuration handMappering a system will have a default HandleMapping ->
 . 9      <! - 1. The bean name lookup ->
 10      <-! <bean   class = "org.springframework .web.servlet.handler.BeanNameUrlHandlerMapping "> </ bean> -
 >11      <-! Registered themselves with the controller class ->
12 is      <-! <The bean name = "/ my.do" class = "com.zhiyou100.mcl.controller.MyController"> </ the bean> ->        
 13 is      <-! 2 . The corresponding controller lookup id class
 14            here contained a mapping id and url addresses
 15       ->
 16      <-! <the bean class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
 . 17          <Property name = "mappings">
 18 is              < The props>
 . 19                  <prop Key = "/ a.do"> MY1 </ prop>
 20 is                  <prop Key = "/ b.do"> MY2 </ prop>
21             </props>
22         </property>
23     </ the bean>       
 24      registered with its own controller class
 25      <the bean ID = "MY1" class = "com.zhiyou100.mcl.controller.MyController"> </ the bean>
 26 is      <the bean ID = "MY2" class = "COM. zhiyou100.mcl.controller.MyController2 "> </ bean> ->
 27      
28      <-! 3 . Find the corresponding Contorller class under the name of the controller class
 29      class in addition to the controller capitalized, others are lowercase ->
 30      <the bean class = "org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"> </ the bean>
 31 is      <the bean class = "com.zhiyou100.mcl.controller.
MyController"></bean>
32     <bean class="com.zhiyou100.mcl.controller.SecondController"></bean>
33     <!-- 配置视图解析器 -->
34     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
35         <property name="prefix" value="/WEB-INF/view/"/>
36         <property name="suffix" value=".jsp"/>
37     </bean>
38 </beans>

4.1 BeanNameUrlHandlerMapping according bean to find the corresponding name tags of Controller class

4.2 SimpleUrlHandlerMapping The bean 's id to find the corresponding Controller class.

4.3 ControllerClassNameHandlerMapping According controller to find the corresponding name of the class of the Controller .

5. Use annotations instead of the configuration information. (More commonly)

5.1 Additional shelf package

 

 

5.2   In springmvc add the following code profile.

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context
 9         http://www.springframework.org/schema/context/spring-context.xsd 
10          HTTP: // www.springframework.org/schema/mvc 
11          HTTP: // www.springframework.org/schema/mvc/spring-mvc.xsd 
12          ">
 13 is          
14          <- pack scan:! scanning packets of the packet type annotation where the controller is located ->
 15          <context: scan-base- Component package =" com.zhiyou100.mcl.controller.annotation "> </ context : Component-Scan>
 16          <- turn annotations drive AnnotationHandlerMapping ->!
 . 17          <MVC: annotation-driven />
 18 is      
. 19      ! <- configuration view resolver ->
 20      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
21         <property name="prefix" value="/WEB-INF/view/"/>
22         <property name="suffix" value=".jsp"/>
23     </bean>
24 
25 </beans>

5.2 Creating Controller class

. 1  Package com.zhiyou100.mcl.controller.annotation;
 2  
. 3  Import org.springframework.stereotype.Controller;
 . 4  Import org.springframework.web.bind.annotation.RequestMapping;
 . 5  
. 6  @Controller
 . 7  public  class MyController1 {
 . 8      @RequestMapping ( "/list.do" ) // represents the access address on the web address bar
 9      public String List () {
 10          System.out.println ( "Search" );
 11          return "the Login" ;        
 12      }
 13      
14      @ RequestMapping ( "/ delete.do")
 15      public String Delete () {
 16          System.out.println ( "Delete" );
 . 17          return "Login" ;        
 18 is      }
 . 19      
20 is      @RequestMapping ( "/ save.do" )
 21 is      public String Save () {
 22 is          the System. out.println ( "save" );
 23 is          return "Login" ;        
 24      }
 25 } 

each request corresponds to a method of Controller

 

Guess you like

Origin www.cnblogs.com/mcl2238973568/p/11455081.html