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

1.非注解的处理器映射器(HandlerMapping)

1.访问:http://localhost:8080/queryItems.action





SpringMVC框架--非注解的处理器映射器和适配器_第1张图片

2.简单URL映射
访问:http://localhost:8080/huan.action
http://localhost:8080/ma.action

 
    
    
        
            
                itemsController
                itemsController
            
        
    
SpringMVC框架--非注解的处理器映射器和适配器_第2张图片
SpringMVC框架--非注解的处理器映射器和适配器_第3张图片
具体配置内容见:SpringMVC框架入门--(学习笔记)

2.非注解的适配器(HandlerAdapter)

1.适配器SimpleControllerHandlerAdapter

  • 要求所有的Handler实现Controller接口


package spring.ssm.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import spring.ssm.pojo.Items;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ubuntu on 17-7-6.
 */
//实现Controller接口的处理器
public class ItemsController implements Controller{

    public ModelAndView handleRequest(HttpServletRequest Request, HttpServletResponse Response) throws Exception {
        List list = new ArrayList();

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

        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();
        modelAndView.addObject("itemsList",list);
        modelAndView.setViewName("WEB-INF/items/itemsList.jsp");
        return modelAndView;
    }
}

2.适配器HttpRequestHandlerAdapter

  • 要求编写的Handler实现HttpRequestHandler接口

package spring.ssm.controller;

import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import spring.ssm.pojo.Items;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ubuntu on 17-7-6.
 */
//实现Controller接口的处理器
public class ItemsController2 implements HttpRequestHandler {
    public void handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        List list = new ArrayList();

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

        Items items_2 = new Items();
        items_2.setName("小米手机");
        items_2.setPrice(5000f);
        items_2.setDetail("mix小米手机!");
        list.add(items_1);
        list.add(items_2);
        httpServletRequest.setAttribute("itemsList",list);
        httpServletRequest.getRequestDispatcher("WEB-INF/items/itemsList.jsp").forward(httpServletRequest,httpServletResponse);

    }
}

总结:映射器就是通过URL找Handler(Controller)
适配器就是按照要求的规则执行Handler(Controller)

.

上一篇文章:SpringMVC框架入门--(学习笔记)

文集:SpringMVC框架学习

你可能感兴趣的:(SpringMVC框架--非注解的处理器映射器和适配器)