springmvc实现前端控制器的三种实现方法:
1、使用注解@Controller
2、实现HttpRequestHandler接口
这种方式需要配置适配器HttpRequestHandlerAdapter,交给适配来执行
springmvc.xml
前端控制器编写:
public class LoginController implements HttpRequestHandler{
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("HttpRequestHandler.........");
//为返回页面设置数据
request.setAttribute("name", "akld");
//设置返回页面
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
HttpRequestHandler接口
public interface HttpRequestHandler {
/**
* Process the given request, generating a response.
* @param request current HTTP request
* @param response current HTTP response
* @throws ServletException in case of general errors
* @throws IOException in case of I/O errors
*/
void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException;
}
HttpRequestHandler实现类HttpRequestHandlerAdapter适配器
public class HttpRequestHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object handler) {
//supports方法本质上就是判断handler是否是Servlet
return (handler instanceof HttpRequestHandler);
}
//handle本质上是执行HttpRequestHandler.handleRequest方法
@Override
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
((HttpRequestHandler) handler).handleRequest(request, response);
return null;
}
@Override
public long getLastModified(HttpServletRequest request, Object handler) {
if (handler instanceof LastModified) {
return ((LastModified) handler).getLastModified(request);
}
return -1L;
}
}
3、实现Controller接口
public class HttpController implements Controller{
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("Controller.........");
return new ModelAndView("index");
}
}
Controller接口:
public interface Controller {
/**
* Process the request and return a ModelAndView object which the DispatcherServlet
* will render. A {@code null} return value is not an error: it indicates that
* this object completed request processing itself and that there is therefore no
* ModelAndView to render.
* @param request current HTTP request
* @param response current HTTP response
* @return a ModelAndView to render, or {@code null} if handled directly
* @throws Exception in case of errors
*/
ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;
}
SimpleControllerHandlerAdapter适配器:
public class SimpleControllerHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object handler) {
return (handler instanceof Controller);
}
@Override
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
return ((Controller) handler).handleRequest(request, response);
}
@Override
public long getLastModified(HttpServletRequest request, Object handler) {
if (handler instanceof LastModified) {
return ((LastModified) handler).getLastModified(request);
}
return -1L;
}
}
supports方法就是判断handler是否是Servlet
getLastModified直接返回modified值
handle方法本质是执行HttpRequestHandler.handleRequest方法。
开发过程中,用的比较多还是直接用注解的方式,简单快速开发。非注解方式的处理器映射器和处理器适配器可以同时存在多个。非注解方式的处理器映射器和处理器适配器之间可以混用。