SpringMVC框架--注解的处理器映射器和适配器

1.注解的处理器映射器

在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping注解映射器。
在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping注解映射器。

<!--================注解的处理器映射器==============================-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

2.适配器

在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
注解适配器。
在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
注解适配器

<!--================注解的适配器==============================-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

注意:

使用mvc:annotation-driven代替上面两个注解映射器和注解适配的配置
mvc:annotation-driven默认加载很多的参数绑定方法,
比如json转换解析器默认加载了,如果使用mvc:annotation-driven则不用配置上面的RequestMappingHandlerMappingRequestMappingHandlerAdapter实际开发时使用mvc:annotation-driven

 <mvc:annotation-driven></mvc:annotation-driven>

3.开发Handler(Controller)

使用注解的映射器和注解的适配器。(使用注解的映射器和注解的适配器必须配对使用)

package spring.ssm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import spring.ssm.pojo.Items;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ubuntu on 17-7-7.
 */
//使用Controller标记 他是一个控制器
@Controller
public class ItemsController3 {
    @RequestMapping("/queryItems3")
    //实现 对queryItems方法和url进行映射,一个方法对应一个url
    //一般建议将url和方法写成一样
    public ModelAndView queryItems()throws Exception{
        List<Items> list = new ArrayList<Items>();

        Items items_1 = new Items();
        items_1.setName("小米笔记本");
        items_1.setPrice(6000f);
        items_1.setDetail(" 小米笔记本电脑!");

        Items items_2 = new Items();
        items_2.setName("小米手机");
        items_2.setPrice(5000f);
        items_2.setDetail("mix小米手机!");
        list.add(items_1);
        list.add(items_2);
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(1212);
        modelAndView.addObject("itemsList",list);
        modelAndView.setViewName("WEB-INF/items/itemsList.jsp");
        return modelAndView;
    }
    @RequestMapping("/selectIteam.action")
    public void  selectIteam(){
        System.out.println("查找方法");

    }

}


1.Handler 单个配置

 <!-- 配置Handler -->
    <bean    class="spring.ssm.controller.ItemsController3"/>

2.Handler 使用组件扫描配置

<!-- 对于注解的Handler 可以单个配置
    实际开发中加你使用组件扫描
    -->
    <!--  <bean  class="com.iot.ssm.controller.ItemsController3"/> -->
    <!-- 可以扫描controller、service、...
    这里让扫描controller,指定controller的包
     -->
    <context:component-scan base-package="com.iot.ssm.controller"></context:component-scan>

运行配置:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等)
      若不配置,默认加载WEB-INF/servlet名称-servlet(springmvc-servlet.xml)
    -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc1.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>


</web-app>

springmvc1.xml:

<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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置Handler -->
    <!--<bean    class="spring.ssm.controller.ItemsController3"/>-->

    <context:component-scan base-package="spring.ssm.controller"></context:component-scan>
<!--================注解的处理器映射器==============================-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!--================注解的适配器==============================-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>


    <!-- 视图解析器
    解析jsp,默认使用jstl,classpath下要有jstl的包
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置jsp路径的前缀 -->
        <property name="prefix" value="WEB-INF/"/>
        <!-- 配置jsp路径的后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

访问:http://localhost:8080/queryItems3.action



作者:Mr_欢先生
链接:https://www.jianshu.com/p/d7d67ba89b82
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

猜你喜欢

转载自blog.csdn.net/bingguang1993/article/details/88061219
今日推荐