thymeleaf中相对路径的两种方式

先看代码:

@Controller
@RequestMapping("/I18n/aaa")
public class I18nController {

    @Autowired
    private LocaleResolver localeResolver;

    @RequestMapping("use")
    public String useI18n(HttpServletRequest request, HttpServletResponse response, Model model){
        //model.addAttribute("msg","信息");
        request.getSession().setAttribute("msg","提示信息");
        localeResolver.setLocale(request,response, Locale.CHINA);
        return "I18n";
    }
}
<img src="../static/images/1.png" th:src="@{/images/1.png}"/><!--http://localhost:9090/learn-thymeleaf116/images/1.png-->
<img src="../static/images/1.png" th:src="@{images/1.png}"/><!--http://localhost:9090/learn-thymeleaf116/I18n/aaa/images/1.png-->

这里主要images前面带/和不带/的区别:

前面加"/":访问的路径是从服务器的根路径而言的,就是application.yml里面配置的context-path,上我我配置的是/learn-thymeleaf116,所以访问路径为http://localhost:9090/learn-thymeleaf116/images/1.png。

前面不加"/":访问路径是相对于当前的路径而言的,比如上面的第二个,这个请求的的路径为http://localhost:9090/learn-thymeleaf116/I18n/aaa/use,相对于他的当前路径就是去掉use,所以最终的访问路径就是http://localhost:9090/learn-thymeleaf116/I18n/aaa/images/1.png

posted @ 2020-01-16 14:59  WL忽然之间  阅读(4452)  评论(0编辑  收藏  举报