简体   繁体   English

如何将CSS和JS添加到Spring MVC项目

[英]How to add CSS and JS to Spring mvc project

I'm working on a project in Spring MVC. 我正在Spring MVC中的一个项目上工作。 I would like to connect CSS and js under it. 我想在其下连接CSS和js。 However, for some reason, spring can not see these files. 但是,由于某种原因,spring无法看到这些文件。 In the previous project which was configured using XML and everything worked fine. 在上一个使用XML配置的项目中,一切正常。 Any suggestion would be helpful. 任何建议都会有所帮助。

Project Structure 项目结构

    <script type="text/javascript" src='<spring:url value="/resources/js/script.js"/>'></script>
    <link href='<spring:url value="/resources/css/style.css"/>' rel="stylesheet" />

Configuration: 组态:

public void addResourceHandlers(final ResourceHandlerRegistry registry){
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

Move the resource folder under webapp folder. resource文件夹webapp文件夹下。 And with this line you can connect your css file. 通过此行,您可以连接CSS文件。

<link rel="stylesheet" href="/css/styles.css"> 

Similarly write code to connect script like 类似地,编写代码以连接脚本,例如

<script type="text/javascript" src="/js/script.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM