The results jump method
ModelAndView
Set ModelAndView object, jump to a specific page based on the name of the view, and the view resolver.
Page: {prefix view resolver} + viewName + {} suffix view resolver
<!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean>
Corresponding controller class
public class ControllerTest1 implements Controller { public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { //返回一个模型视图对象 ModelAndView mv = new ModelAndView(); mv.addObject("msg","ControllerTest1"); mv.setViewName("test"); return mv; } }
ServletAPI
By providing ServletAPI, you need not view resolver.
- Output via HttpServletResponse
- Redirection by HttpServletResponse
- HttpServletResponse achieved by forwarding
@Controller public class ResultGo { @RequestMapping("/result/t1") public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException { rsp.getWriter().println("Hello,Spring BY servlet API"); } @RequestMapping("/result/t2") public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException { rsp.sendRedirect("/index.jsp"); } @RequestMapping("/result/t3") public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception { //转发 req.setAttribute("msg","/result/t3"); req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp); } }
SpringMVC
Forwarding and redirection is achieved by SpringMVC - no need to view resolver;
Before the test, you need to comment out the view resolver
@Controller public class ResultSpringMVC { @RequestMapping("/rsm/t1") public String test1(){ //转发 return "/index.jsp"; } @RequestMapping("/rsm/t2") public String test2(){ //转发二 return "forward:/index.jsp"; } @RequestMapping("/rsm/t3") public String test3(){ //重定向 return "redirect:/index.jsp"; } }
Forwarding and redirection is achieved by SpringMVC - have view resolver;
Redirect, no view resolver, essentially re-request a new place Well, so pay attention to the path problem.
A request can be redirected to another implementation.
@Controller public class ResultSpringMVC2 { @RequestMapping ( "/ Rsm2 / T1" ) public String test1 () { // forward return "Test" ; } @RequestMapping ( "/ Rsm2 / T2" ) public String test2 () { // weight oriented return "the redirect: /index.jsp" ; // return "the redirect: hello.do"; // hello.do another request / } }