2017-05-30 63 views
2

我没有找到我正在处理的特定场景的答案。我有一个Maven Web项目,我想在Tomcat服务器上运行(目前工作正常)。我的问题是,我需要使用jQuery和Bootstrap,但它不起作用,官方资源不提供一种方法来使用它没有任何额外的框架。如何在没有任何框架的情况下使用带有Maven的webjar

这是我迄今所做的:

  • 我的pom.xml看起来是这样的,并拥有所有必要的依赖:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    
         <modelVersion>4.0.0</modelVersion> 
         <groupId>de.ls5.wt2</groupId> 
         <artifactId>micro</artifactId> 
         <packaging>war</packaging> 
         <version>0.0.1-SNAPSHOT</version> 
         <name>micro Maven Webapp</name> 
         <url>http://maven.apache.org</url> 
         <dependencies> 
         <dependency> 
          <groupId>junit</groupId> 
          <artifactId>junit</artifactId> 
          <version>3.8.1</version> 
          <scope>test</scope> 
         </dependency> 
          <dependency> 
           <groupId>org.webjars</groupId> 
           <artifactId>webjars-locator</artifactId> 
           <version>0.1</version> 
          </dependency> 
          <dependency> 
           <groupId>org.webjars</groupId> 
           <artifactId>webjars-locator</artifactId> 
           <version>0.32-1</version> 
          </dependency> 
          <dependency> 
           <groupId>org.webjars</groupId> 
           <artifactId>jquery</artifactId> 
           <version>3.2.0</version> 
          </dependency> 
          <dependency> 
           <groupId>org.webjars</groupId> 
           <artifactId>bootstrap</artifactId> 
           <version>3.3.7</version> 
          </dependency> 
         <dependency> 
          <groupId>org.hibernate.javax.persistence</groupId> 
          <artifactId>hibernate-jpa-2.1-api</artifactId> 
          <version>1.0.0.Final</version> 
         </dependency> 
         <dependency> 
          <groupId>javax</groupId> 
          <artifactId>javaee-api</artifactId> 
          <version>7.0</version> 
         </dependency> 
         <dependency> 
          <groupId>javax</groupId> 
          <artifactId>javaee-web-api</artifactId> 
          <version>7.0</version> 
         </dependency> 
         </dependencies> 
         <build> 
         <finalName>micro</finalName> 
         </build> 
        </project> 
    
  • 目标/ APPNAME/WEB-INF/lib文件夹包含jQuery,Bootstrap和所有其他相关库的jar文件

  • 当我想现在在index.html或index.jsp中使用它时,它不起作用,控制台sa是的,资源无法找到。中的index.html看起来像这样(来自教程为例):

<html> 
 
    <head> 
 
     <script src="/webjars/jquery/3.1.1/jquery.min.js"></script> 
 
     <script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script> 
 
     <title>WebJars Demo</title> 
 
     <link rel="stylesheet" 
 
      href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" /> 
 
    </head> 
 
    <body> 
 
     <div class="container"><br/> 
 
      <div class="alert alert-success"> 
 
       <a href="#" class="close" data-dismiss="alert" 
 
        aria-label="close">×</a> 
 
       <strong>Success!</strong> It is working as we expected. 
 
      </div> 
 
     </div> 
 
    </body> 
 
</html>

  • index.html是我target/appName/文件夹内,我知道"target/appName/webjars/jquery/3.1.1/jquery.min.js"下的引用文件,其被引用从index.html不存在,但我需要知道什么?我以为maven会这么做。

回答

1

首先,你有重复的webjars-locator依赖 - 修复。现在


,对您的问题: 通过删除前面的 “/” 修复您的JS和CSS进口: 更改<script src="/webjars/.../><script src="webjars/.../>

检查full example,如果你想

希望这会工作。

相关问题