重定向和转发
通过springmvc来实现转发和重定向—无需视图解析器
测试前,需要将视图解析器注释掉
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">-->
<!-- <property name="prefix" value="/WEB-INF/jsp/"/>-->
<!-- <property name="suffix" value=".jsp" />-->
<!--</bean>-->
默认的就是转发
转发
@Controller
public class ModelTest1 {
@RequestMapping("/m1/t1")
public String test(Model model){
model.addAttribute("msg","ModelTest1");
// return "/WEB-INF/jsp/test.jsp"; //转发
return "forward:/WEB-INF/jsp/test.jsp"; //转发
}
}
重定向
不需要视图解析器,本质就是重新请求一个新地方嘛,所以注意路径问题.
可以重定向到另外一个请求实现﹒
@Controller
public class ModelTest1 {
@RequestMapping("/m1/t1")
public String test(Model model){
model.addAttribute("msg","ModelTest1");
return "redirect:/index.jsp"; //重定向
}
}
通过springmvc来实现转发和重定向—有视图解析器
转发
@Controller
public class ModelTest1 {
@RequestMapping("/m1/t1")
public String test(Model model){
model.addAttribute("msg","ModelTest1");
return "/test"; //转发
}
}
重定向
@Controller
public class ModelTest1 {
@RequestMapping("/m1/t1")
public String test(Model model){
model.addAttribute("msg","ModelTest1");
return "/redirect:/index.jsp"; //重定向
// return "/redirect:hello.do"; //hello.do为另一个请求
}
}