1.默认的BeanNameUrlHandlerMapping(通过name查找Controller)用Controller的name来访问

2.ControllerClassNameHandlerMapping(通过Controller类的名字查找Controller。在网页访问除了Controller类首字母大写,其他的(Controller1)都小写)

<?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.xsd">

 

       <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>

      

    <!-- 配置Controller -->

    <bean id="m1" class="com.zhiyou100.kfs.controller.MyController1"></bean>

    <bean id="m2" class="com.zhiyou100.kfs.controller.MyController2"></bean>

 

       <!-- 配置视图解析器:ViewResolver -->

       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

              <property name="prefix" value="/"/>

              <property name="suffix" value=".jsp"/>

       </bean>

</beans>

 

3.SimpleUrlHandlerMapping(通过SimpleUrlHandlerMapping的mappings属性(property)和Controller的id映射关系来访问)

<?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.xsd">

 

       <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

              <property name="mappings">

                     <props>

                            <prop key="a.do">m1</prop>

                            <prop key="b.do">m2</prop>

                     </props>

              </property>

       </bean>

      

    <!-- 配置Controller -->

    <bean id="m1" class="com.zhiyou100.kfs.controller.MyController1"></bean>

    <bean id="m2" class="com.zhiyou100.kfs.controller.MyController2"></bean>

 

       <!-- 配置视图解析器:ViewResolver -->

       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

              <property name="prefix" value="/"/>

              <property name="suffix" value=".jsp"/>

       </bean>

</beans>

 

4.使用注解代替配置信息(annotation),需要添加头部xmlnx:信息(可以找文档)还需要添加spring-aop jar包

  1.包扫描:扫描注解所在的包(扫描带注解的Controller类)

用<context::component-scan>标签

  2.开启注解驱动:会开启AnnotationHandlerMapping

用<mvc:annotation-driver/>标签

<?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.xsd

        http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

 

       <!-- 1.包扫描:扫描注解所在的包(带注解的Controller类所在的包) -->

       <context:component-scan base-package="com.zhiyou100.kfs.controller"></context:component-scan>

       <!-- 2.开启注解驱动:会开启AnnotationHandlerMapping -->

       <mvc:annotation-driven/>

       <!-- 配置视图解析器:ViewResolver -->

       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

              <property name="prefix" value="/"/>

              <property name="suffix" value=".jsp"/>

       </bean>

</beans>

 

对应的Controller

package com.zhiyou100.kfs.controller;

 

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

 

@Controller

@RequestMapping("/myController1")

public class MyControler3 {

      

       @RequestMapping("/list")

       public ModelAndView list() {

              ModelAndView mv=new ModelAndView("login");

              mv.addObject("name","login3<br/>hello!");

              return mv;

       }

}

posted on 2019-09-03 21:59  kfsrex  阅读(468)  评论(0编辑  收藏  举报