在Maven项目中导入Bootstrap模板遇到问题
Hey there, let's work through the issues you're having getting a full Bootstrap template set up in your Maven project! Looking at the dependency snippets you shared, there are a couple of key problems that are likely causing your trouble:
You've set <scope>test</scope> on your Bootstrap dependency, and your jQuery dependency's scope is incomplete (I'm guessing it's also set to test). The test scope means these dependencies are only available during test compilation and execution—your main web application code won't be able to access them at runtime, which is why your Bootstrap template isn't loading properly.
Corrected Dependency Configuration
Remove the <scope> tags entirely (since compile is the default scope, which makes dependencies available to both main and test code) or explicitly set them to <scope>compile</scope>:
<dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>3.3.6</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.0.0</version> </dependency>
Once your dependencies are fixed, you need to properly link Bootstrap's CSS and JS files (and jQuery, which Bootstrap requires) in your HTML templates. WebJars follow a consistent path structure: /webjars/[artifactId]/[version]/[resource-file-path]
Here's how to add the references:
<!-- Bootstrap CSS --> <link rel="stylesheet" href="/webjars/bootstrap/3.3.6/css/bootstrap.min.css"> <!-- jQuery (required for Bootstrap's JavaScript components) --> <script src="/webjars/jquery/3.0.0/jquery.min.js"></script> <!-- Bootstrap JavaScript --> <script src="/webjars/bootstrap/3.3.6/js/bootstrap.min.js"></script>
Note for Non-Spring Boot Projects
If you're using a plain Java Web project (not Spring Boot), you'll need to add a servlet mapping in your web.xml to serve WebJars resources:
<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/webjars/*</url-pattern> </servlet-mapping>
Run mvn clean install to ensure Maven pulls the dependencies into your local repository. Check your .m2/repository/org/webjars directory—you should see folders for bootstrap/3.3.6 and jquery/3.0.0. If they're missing, double-check your network connection or try refreshing Maven dependencies in your IDE.
Bootstrap 3.3.6 is a pretty old version. If you don't have a specific reason to stick with it, consider upgrading to the latest stable release of Bootstrap 3 (3.4.1) to get security patches and bug fixes.
内容的提问来源于stack exchange,提问作者Iliqn Tachev

