SpringMVC 4:自定义适配器

※ .自定义适配器

    一般情况下,springMVCSimpleControllerHandlerAdapter会是我们常用的适配器,也是SpringMVC中默认的适配器,该适配器中的主要代码如下:
    

     public class SimpleControllerHandlerAdapter implements HandlerAdapter {
        public boolean supports(Object handler) {
            return (handler instanceof Controller);
        }
        public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {

            return ((Controller) handler).handleRequest(request, response);
        }
    }

    return ((Controller) handler).handleRequest(request, response);
    从代码中可以看出,它首先会判断我们的handler是否实现了Controller接口,如果实现了,那么会调用Controller接口中的handleRequest方法。
    那么根据这种方式能看出,我们也可以有自己的适配器的实现,那么就可以让任意类成为SpringMVC中的handler了,无论我们的类是否实现了Controller接口(首先要自定义控制器Controller)。
    
    

例如:
        自己的接口:
        public interface MyHandler {
            public ModelAndView handler_test(HttpServletRequest request, HttpServletResponse response)throws Exception;
        }
        自己的适配器:
        public class MyHandlerAdapter implements HandlerAdapter{
            @Override
            public boolean supports(Object handler) {
                return (handler instanceof MyHandler);
            }

            @Override
            public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
                    throws Exception {
                return ((MyHandler)handler).handler_test(request, response);
            }

            @Override
            public long getLastModified(HttpServletRequest request, Object handler) {
                return -1L;
            }

        }
        
        自己的hander:(就是我们之前写的Controller)
        public class TestController implements MyController{
            @Override
            public ModelAndView handler_test(HttpServletRequest request, HttpServletResponse response) throws Exception {
                String name = request.getParameter("name");
                ModelAndView mv = new ModelAndView("hello");
                mv.addObject("name", name);
                return mv;
            }
        }


   最后在spring的配置中把我们的适配器进行配置即可正常使用.

    

例:

1.自定义控制器Controller

package com.briup.web.adapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
/*
 * 自定义控制器接口
 */
public interface MyController {
	ModelAndView 
		handler_test(HttpServletRequest req,
				HttpServletResponse res);
}



2.自定义适配器


package com.briup.web.adapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ModelAndView;

public class DefineAdapter 
	implements HandlerAdapter{

	@Override
	public boolean supports(Object handler) {
		// TODO Auto-generated method stub
                //判断是否是自己定义的Controller
		return (handler instanceof MyController);
	}

	@Override
	public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		return ((MyController)handler)
				.handler_test(request, response);
	}

	@Override
	public long getLastModified(HttpServletRequest request, Object handler) {
		// TODO Auto-generated method stub
		return 0;
	}

}




3.实现自定义控制器(因为


package com.briup.web.adapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

public class FourController 
	implements MyController{

	@Override
	public ModelAndView handler_test(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mv=new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("name", "four_test");
		return mv;
	}

}




4.spring.xml 配置


	

                    

你可能感兴趣的:(SSM(SpringMVC))