Search code examples
springspring-mvcspring-bootthymeleaf

requestMapping annotation in controller head ,and thymeleaf static content will be not find


when I add requestmapping in controller,thymeleaf static content will be not find, such as

@Controller
@RequestMapping(value = "/section")
public class SectionController {

@Autowired
private SectionService sectionService;

@RequestMapping(value = "/list")
public String listSection(Model model){
    model.addAttribute("result",sectionService.listSection());
    return "views/listSection";
}

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveSection(Section section){
    sectionService.saveSection(section);
    return "redirect:/section/list";
}}

listSection.html using thymeleaf

  <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script th:src="@{jquery/jquery-3.2.1.js}" ></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script th:src="@{bootstrap/3.3.7v/js/bootstrap.js}"></script>

<!-- Bootstrap -->
<link th:href="@{bootstrap/3.3.7v/css/bootstrap.css}" rel="stylesheet">

and start service like will report error static content will be not find chrome console report error

and I remove the requestMapping in controller class head, and access http://localhost:9090/listit worked as normal, please tell me why?


Solution

  • Problem is you are fetching static content with relative URL. Not from the absolute root path(/). So that for relative url it is requesting to the current URL's relative root path (Like for /section/list it will request to /section/+path_of_your_content) as /section is the root path of this URL. But when you try the URL like /section or /list it is requesting for content in / + path_of_your_content as / is the root path of this URL

    It the link URL is right, it should work if you make the URL from root. Change the listSection.html as following

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script th:src="@{/jquery/jquery-3.2.1.js}" ></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script th:src="@{/bootstrap/3.3.7v/js/bootstrap.js}"></script>
    
    <!-- Bootstrap -->
    <link th:href="@{/bootstrap/3.3.7v/css/bootstrap.css}" rel="stylesheet">