SpringMVC架构--注解的控制器映射器和连接器版权声明

原创
小哥 3年前 (2022-12-26) 阅读数 10 #大杂烩

1.带注释的处理器映射器

在spring3.1之前使用 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping 注释映射器。
在spring3.1之后使用 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
注释适配器


注意:

使用 mvc:annotation-driven 替换上述两个注释映射器和注释调整的配置。
mvc:annotation-driven 默认情况下,加载了许多参数绑定方法,
比如 json 如果使用 mvc:annotation-driven 您不需要配置上述内容 RequestMappingHandlerMappingRequestMappingHandlerAdapter 用于实际开发 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 list = new ArrayList();

        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 单个配置

 
    

2.Handler 使用组件扫描配置


    
    
    

运行配置:

web.xml



  Archetype Created Web Application

  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:springmvc1.xml
    
  
  
    springmvc
    *.action
  

springmvc1.xml:



    
    

    

    

    

    
    
        
        
        
        
    

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

作者:Mr_欢先生
链接:https://www.jianshu.com/p/d7d67ba89b82
资料来源:简介
这本简介书的版权归作者所有。请联系作者以获得授权,并注明任何形式的转载来源。

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除