2

I can see my bootstrap template using thymeleaf with spring boot.

When I request http://localhost:8080/student bootstrap resources work good, but when I request including some path variable like http://localhost:8080/student/3 bootstrap can't be uploaded. I'm getting this error from the browser Request Method:GET Status Code:400. It tries to request bootstrap file like that http://localhost:8080/student/bootstrap.css.

I don't think I have any problems with declaring bootstrap files' path because other pages work fine. And my paths are from guide as resources/static/bootstrap.css I'm not using WebMvcConfig and I'm using default spring security.

@RequestMapping( value = "/student/{id}", method = RequestMethod.GET)
public String izinEkle(Model model, @PathVariable int id) {


    studentService.getStudentlById(id);
    Izin izin = new Izin();
    model.addAttribute("izin",izin);
    return "izinGiris";
}

--

<head lang="en">

<title>HolyDayTracker</title>
<link href="../static/bootstrap.css" th:href="@{bootstrap.css}" rel="stylesheet" media="screen">
<link href="../static/style.css" th:href="@{style.css}" rel="stylesheet">
<script src="http://cdn.jsdelivr.net/webjars/jquery/2.1.4/jquery.min.js   th:src="@{../webjars/jquery/2.1.4/jquery.min.js}"></script>

memoricab
  • 425
  • 1
  • 7
  • 28
  • It seems like program is trying get path variable id as bootstrap.css. I think I have some logic failure on the request paths with variables. I'm pretty confused. Any help would be appreciated. Thanks. – memoricab Feb 22 '17 at 21:46
  • 1
    Possible duplicate of [Absolute vs relative URLs](http://stackoverflow.com/questions/2005079/absolute-vs-relative-urls) – g00glen00b Feb 23 '17 at 07:57
  • The issue here is that `bootstrap.css` will be a relative URL, and `/student` and `/student/3` are on different levels, so they will relatively point to a different location for `bootstrap.css`. There are many solutions for this, most of them can be found in the linked question. The most common solutions are to use an absolute URL (as shown by @shi) or by setting a `` tag in you `` section so the base URL is the same for all paths. – g00glen00b Feb 23 '17 at 07:58
  • Thank you for description. It really helped. – memoricab Feb 27 '17 at 09:59

1 Answers1

4

Try adding a leading slash / in your thymeleaf href attr value. And you do not need additional href attributes, only th:href will do.

<link th:href="@{/bootstrap.css}" rel="stylesheet" media="screen">
<link th:href="@{/style.css}" rel="stylesheet">
Monzurul Shimul
  • 8,132
  • 2
  • 28
  • 42
  • Thank you very much. I think I missed an important point about thymeleaf references. Didn't expect anything like this solution, anyway saved me out. I I appreciate. But a point that if I remove html href I can't auto-complete bootstrap classes. Probably it's reference for the Ide. – memoricab Feb 27 '17 at 10:01