重定向是两次请求
重定向一般在身份校验、权限管理等地方配合aop一起使用
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/test")
public class SpringBootTest {
@RequestMapping("/Beijin")
public ModelAndView test(String name){
ModelAndView modelAndView=new ModelAndView();
if(!name.equalsIgnoreCase("Beijin")){
/**
* 重定向的是请求路径不是模板文件地址
* 重定向后还能继续加上请求参数
*/
modelAndView.setViewName("redirect:/test/Other?name="+name);
}else {
modelAndView.setViewName("/my/Beijin");
}
return modelAndView;
}
@RequestMapping("/Other")
public ModelAndView test1(){
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("/my/Other");
return modelAndView;
}
}
/my/Beijin.ftl模板文件写
练习重定向
本次请求的参数是Beijin
/my/Other.ftl模板文件写
练习重定向
本次请求的参数不是Beijin,是其他字符串
请求地址是:http://127.0.0.1:8080/test/Beijin 参数是name参数的值是Beijin的时候找到的模板文件是/my/Beijin
请求地址是:http://127.0.0.1:8080/test/Beijin 参数是name参数的值不是Beijin的时候重定向到http://127.0.0.1:8080/test/Other这个请求路径并且加上请求参数,返回的是/my/Other的模板文件