先看代碼:
@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