致谢@how2j
之前所做的内容的跳转都是服务端跳转,本节学习客户端跳转的SpringMVC的客户端跳转实现
1.编写IndexController
核心代码如下
ModelAndView mav = new ModelAndView("redirect:/index");
完整代码如下:
package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class IndexController {
@RequestMapping("/index")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("index");
mav.addObject("message", "Hello Spring MVC");
return mav;
}
@RequestMapping("/jump")
public ModelAndView jump() {
ModelAndView mav = new ModelAndView("redirect:/index");
return mav;
}
}
其他配置文件如以前所述
项目结构如下:
2.测试结果
Hello Spring MVC
其中,最初的访问路径是:
跳转之后的路径是:
由此就可以看出差别了