1、实现Controller接口的处理器适配器
package com.xiaostudy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class MyController implements Controller{
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//创建视图
ModelAndView modelAndView = new ModelAndView();
//设置页面回显数据
modelAndView.addObject("hello", "xiaostudy");
//返回物理视图,指定跳转的视图
// mv.setViewName("/WEB-INF/index.jsp");
//返回逻辑视图
modelAndView.setViewName("index");
return modelAndView;
}
}
2、实现HttpRequestHandler接口的处理器适配器
package com.xiaostudy;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.HttpRequestHandler;
public class HttpController implements HttpRequestHandler{
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("hello", "xiaostudy.xiaostudy");
request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response);
}
}