thymeleaf中相对路径的两种⽅式
先看代码:
@Controller
@RequestMapping("/I18n/aaa")
public class I18nController {
@Autowired
thymeprivate LocaleResolver localeResolver;
@RequestMapping("use")
public String useI18n(HttpServletRequest request, HttpServletResponse response, Model model){
//model.addAttribute("msg","信息");
localeResolver.setLocale(request,response, Locale.CHINA);
return "I18n";
}
}
<img src="../static/images/1.png" th:src="@{/images/1.png}"/><!--localhost:9090/learn-thymeleaf116/images/1.png-->
<img src="../static/images/1.png" th:src="@{images/1.png}"/><!--localhost:9090/learn-thymeleaf116/I18n/aaa/images/1.png-->
这⾥主要images前⾯带/和不带/的区别:
前⾯加"/":访问的路径是从服务器的根路径⽽⾔的,就是l⾥⾯配置的context-path,上我我配置的是/learn-thymeleaf116,所以访问路径为localhost:9090/learn-thymeleaf116/images/1.png。
前⾯不加"/":访问路径是相对于当前的路径⽽⾔的,⽐如上⾯的第⼆个,这个请求的的路径为localhost:9090/learn-thymeleaf116/I18n/aaa/use,相对于他的当前路径就是去掉use,所以最终的访问路径就是localhost:9090/learn-thymeleaf116/I18n/aaa/images/1.png