requestMapping注释在控制器头中,并且找不到百日咳静态内容

时间:2017-05-25 08:54:46

标签: spring spring-mvc spring-boot thymeleaf

当我在控制器中添加requestmapping时,将无法找到百日咳静态内容,例如

@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使用百里香叶

  <!-- 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">

启动服务就像报告错误静态内容将无法找到 chrome console report error

我删除了控制器类头中的requestMapping,并且访问http://localhost:9090/list它正常工作,请告诉我原因?

1 个答案:

答案 0 :(得分:0)

问题是您是使用相对URL获取静态内容。不是来自绝对根路径(/)。因此,对于相对网址,它正在请求当前网址的相对根路径(例如/section/list它将请求/section/ + path_of_your_content),因为/section是此URL的根路径。但是,当您尝试/section/list之类的网址时,它会在/ + path_of_your_content中请求内容,因为/是此网址的根路径

链接URL是正确的,如果您从root创建URL,它应该可以工作。更改listSection.html,如下所示

<!-- 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">