Spring Boot没有加载css文件:请求方法' GET'不支持

时间:2017-06-22 19:52:32

标签: css gradle spring-boot materialize webjars

Spring启动应用程序未加载使用Gradle导入的materializecss库的css文件

在我的控制器中,我的方法看起来像这样

@RequestMapping(value = "/")
public String home(){
    return "user/index";
}

的index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="layout/default">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="viewport" content="width=device-width, initialscale= 1,
            maximum-scale=1.0, user-scalable=no"/>
    <title>Dodaj produkt</title>
</head>
<body>
<div class="row" layout:fragment="content">
</div>
</body>
</html>

和default.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="viewport" content="width=device-width, initialscale= 1,
            maximum-scale=1.0, user-scalable=no"/>
    <title>Witaj w Thymeleaf</title>
    <link href="/webjars/materializecss/0.98.0/css/materialize.css"
          type="text/css" rel="stylesheet" media="screen,projection"/>
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"/>
</head>
<body>
<nav class="nav-extended">
    <div class="nav-wrapper">
        <a href="/" class="brand-logo">Logo</a>
        <a href="#" data-activates="mobile-demo" class="button-collapse"><i class="material-icons">menu</i></a>
        <ul id="nav-mobile" class="right hide-on-med-and-down">
            <li><a th:href="@{/products}">Produkty</a></li>
            <li><a th:href="@{/categories}">Kategorie</a></li>
            <li><a th:href="@{/cart}">Koszyk</a></li>
        </ul>
        <ul class="side-nav" id="mobile-demo">
            <li><a th:href="@{/products}">Produkty</a></li>
            <li><a th:href="@{/categories}">Kategorie</a></li>
            <li><a th:href="@{/cart}">Koszyk</a></li>
        </ul>
    </div>
</nav>
<div layout:fragment="content"></div>
<script src="/webjars/jquery/2.1.4/jquery.js"></script>
<script src="/webjars/materializecss/0.98.0/js/materialize.js"></script>
<script type="text/javascript">
    $(".dropdown-button").dropdown();
    $(".button-collapse").sideNav();
</script>
<script type="text/javascript" layout:fragment="script"></script>
</body>
</html>

我收到错误

WARN 4520 --- [nio-8080-exec-2] o.s.web.servlet.PageNotFound             : Request method 'GET' not supported

并且未加载css样式

css files not loading

我试图找到解决方案,但我无法找到符合我问题的任何内容

[编辑] 除了创建另一个简单的应用程序以确定这些webjars是否可以在其中工作之外,我没有做任何事情。它正在工作,所以我重新启动主应用程序,问题几乎消失了。现在css文件正在加载正确,但我仍然收到请求方法的警告&#39; GET&#39;不支持,虽然现在一切都很好 [编辑] 还没有工作,在另一个app css文件正在加载,在我的主应用程序中,当我尝试访问它时,我得到405

我的整个MainController看起来像这样,没有其他方法映射到&#34; /&#34;

@Controller
public class MainController {
    private ProductServiceImpl productsService;
    private CategoryService categoryService;
    private CartSession cartSession;

    @Autowired
    public MainController(ProductServiceImpl productService, CategoryService categoryService,
                          CartSession cartSession){
        this.productsService = productService;
        this.categoryService = categoryService;
        this.cartSession = cartSession;
    }

    @ModelAttribute
    public List<CartItem> getCart(){
        return cartSession.toList();
    }

    @RequestMapping(value = "/")
    public String home(){
        return "user/index";
    }

    @RequestMapping("products")
    public String products(Model model, @PageableDefault(size = 20) Pageable pageable, SearchForm searchForm,
                           SortingForm sortingForm){
        Page<Product> products = productsService.findAll(pageable);
        PageWrapper<Product> page = new PageWrapper<>(products, "/products");
        model.addAttribute("products", products);
        model.addAttribute("page", page);
        return "user/products";
    }


    @RequestMapping(value = "products", params = {"search"}, method = RequestMethod.POST)
    public String searchedProducts(Model model, @PageableDefault(size = 20) Pageable pageable, SearchForm searchForm,
                                   SortingForm sortingForm){
        Page<Product> products = productsService.searchByQuery(searchForm.getToFind(), pageable);
        PageWrapper<Product> page = new PageWrapper<>(products, "/products");
        model.addAttribute("products", products);
        model.addAttribute("page", page);
        return "user/products";
    }

    @RequestMapping(value = "products", params = {"sort"}, method = RequestMethod.POST)
    public String searchedProducts(Model model, @PageableDefault(size = 20) Pageable pageable,
                                   SortingForm sortingForm, SearchForm searchForm){
        Page<Product> products = productsService.getProductsOrdered(pageable, sortingForm.getOrderBy());
        PageWrapper<Product> page = new PageWrapper<>(products, "/products");
        model.addAttribute("products", products);
        model.addAttribute("page", page);
        return "user/products";
    }

    @RequestMapping("productPicture/{id}")
    public void getProductPicture(HttpServletResponse response, @PathVariable("id")Long id) throws IOException {
        String image = productsService.findById(id).getImagePath();
        response.setHeader("Content-Type",
                URLConnection.guessContentTypeFromName(image));
        InputStream in = new FileSystemResource(image).getInputStream();
        OutputStream out = response.getOutputStream();
        IOUtils.copy(in, out);
        in.close();
        out.close();
    }

    @RequestMapping("product/{id}")
    public String productInfo(@PathVariable(value = "id")Long id, Model model){
        model.addAttribute("product", productsService.findById(id));
        return "user/product";
    }

}

1 个答案:

答案 0 :(得分:1)

问题解决了

我的任何webjars文件甚至资源目录中的文件未正确加载的原因是我在此注释中出错的另一个控制器

.facebook, .instagram, .dribble {
  transform: scale(0.65);
  -webkit-transform: scale(0.65);
  transition: all .2s ease-in-out;
}

我已将其更改为此,并且它现在正在为我工​​作

@RequestMapping(name = "/cart/updateProductQuantity", params="update", method = RequestMethod.POST)

如果有人知道为什么会出现问题,请与我分享