springmvc中RequestMapping注解和redirect路径加不加斜杠有何区别

RequestMapping注解和redirect路径加不加斜杠有何区别

1.redirect 重定向时是否需要加"/"斜杠

项目的根路径为:http://localhost:8080/vueblog

页面请求/user/login之后重定向到 /index.jsp

@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/login")
    public String testPath(){
        return "redirect:/index.jsp";
    }
}
如果 redirect之后没有添加斜杠,就会使用当前controller的相对路径
    访问的是:http://localhost:8080/vueblog/user/index.jsp

springmvc中RequestMapping注解和redirect路径加不加斜杠有何区别_第1张图片

如果 redirect之后添加了斜杠,就会拼接项目的根路径 
    访问的是:http://localhost:8080/vueblog/index.jsp

springmvc中RequestMapping注解和redirect路径加不加斜杠有何区别_第2张图片

2.关于RequestMapping注解中的value是否加斜杠

可加可不加,因为在springmvc底层会对其进行处理,如果没有斜杠会自动加上

源码:
org.springframework.web.servlet.mvc.condition.PatternsRequestCondition prependLeadingSlash
springmvc中RequestMapping注解和redirect路径加不加斜杠有何区别_第3张图片

你可能感兴趣的:(SSM,java,intellij-idea,spring,springmvc,ssm)