1、实现Controller接口的处理器适配器
1 package com.xiaostudy; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.web.servlet.ModelAndView; 7 import org.springframework.web.servlet.mvc.Controller; 8 9 public class MyController implements Controller{ 10 11 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { 12 //创建视图 13 ModelAndView modelAndView = new ModelAndView(); 14 15 //设置页面回显数据 16 modelAndView.addObject("hello", "xiaostudy"); 17 18 //返回物理视图,指定跳转的视图 19 // mv.setViewName("/WEB-INF/index.jsp"); 20 21 //返回逻辑视图 22 modelAndView.setViewName("index"); 23 24 return modelAndView; 25 } 26 27 }
2、实现HttpRequestHandler接口的处理器适配器
1 package com.xiaostudy; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 import org.springframework.web.HttpRequestHandler; 10 11 public class HttpController implements HttpRequestHandler{ 12 13 @Override 14 public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 15 request.setAttribute("hello", "xiaostudy.xiaostudy"); 16 request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response); 17 } 18 19 }