写一个简单的SpringMvc的demo

1.引入springmvc相关的架包

 

 

 2.在web.xml中配置DispatcherServlet

 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.配置springMvc的配置文件

 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     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6         
 7         
 8     <!-- 配置一个handMappering  如果不配置系统会默认有一个HandleMapping -->
 9     
10     <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> 
11     <!-- 注册自己用的controller类 -->
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.常见的HandlerMapping

 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     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6         
 7         
 8     <!-- 配置一个handMappering  如果不配置系统会默认有一个HandleMapping -->
 9     <!-- 1.根据bean名查找 -->
10     <!-- <bean  class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> -->
11     <!-- 注册自己用的controller类 -->
12     <!-- <bean name="/my.do" class="com.zhiyou100.mcl.controller.MyController"></bean> -->        
13     <!-- 2.根据id查找相对应的controller类
14           这里包含了id与url地址的映射关系
15      -->
16     <!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
17         <property name="mappings">
18             <props>
19                 <prop key="/a.do">my1</prop>
20                 <prop key="/b.do">my2</prop>
21             </props>
22         </property>
23     </bean>       
24     注册自己用的controller类
25     <bean id="my1" class="com.zhiyou100.mcl.controller.MyController"></bean>
26     <bean id="my2" class="com.zhiyou100.mcl.controller.MyController2"></bean> -->
27     
28     <!-- 3.根据controller类的名字查找对应的Contorller类
29     类中除了Controller首字母大写,其他都小写 -->
30     <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>
31     <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 根据bean标签的名称找到对应的Controller

4.2  SimpleUrlHandlerMapping 根据beanid查找对应的Controller类。

4.3  ControllerClassNameHandlerMapping 根据controller类的名字找到对应的Controller

5.  使用注解来替代配置信息。(比较常用)

5.1  加入额外的架包

 

 

5.2  springmvc配置文件中加入如下代码。

 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         
14         <!-- 包扫描:扫描注解所在的包controller类所在的包 -->
15         <context:component-scan base-package="com.zhiyou100.mcl.controller.annotation"></context:component-scan>
16         <!-- 开启注解驱动AnnotationHandlerMapping -->
17         <mvc:annotation-driven/>
18     
19     <!-- 配置视图解析器 -->
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  创建Controller类

 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")  //表示的是网页地址栏上的访问地址
 9     public String list() {
10         System.out.println("查询所有");
11         return "login";        
12     }
13     
14     @RequestMapping("/delete.do")
15     public String delete() {
16         System.out.println("删除");
17         return "login";        
18     }
19     
20     @RequestMapping("/save.do")
21     public String save() {
22         System.out.println("保存");
23         return "login";        
24     }
25 }

每一个请求对应着Controller中的一个方法

 

posted @ 2019-09-03 19:38  猪头太笨  阅读(1762)  评论(0编辑  收藏  举报