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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM