我无法在Maven中配置资源路径

时间:2016-12-29 08:21:33

标签: java eclipse maven java-ee

我无法获得正确的css路径

我在index.jsp头上有这段代码

<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="javascript/jquery-3.1.1.min.js"></script>
<script src="javascript/bootstrap.min.js"></script>
<script src="javascript/angular.min.js"></script>

请注意,如果我使用它:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

css显示正确。

这是我的project buildpath

这是similar question not answered

4 个答案:

答案 0 :(得分:0)

您可以将此cdn用于bootstrap

   if((($current_year - $start_year) % 2) == 0){
    //perform task
    }

答案 1 :(得分:0)

使用

`<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">`

一切正常

测试并验证

下面是一个检查,这里我添加了容器类(这是一个bootstrap类) =&gt;左右两侧都添加了15px的填充。

#test{
  background:red;
  height:200px;
  }
<head>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  </head>
<body>
<ul class="list-group">this is  ul tag
  <li class="list-group-item">i didn't applied list-style:none</li>
  <li class="list-group-item">i didn't applied any style to ul,li tags</li>
  <li class="list-group-item">but it's working</li>
  <li class="list-group-item">which meants <strong>bootstrap</strong> is working</li>
</ul>
<div class="container">
  <div id="test"></div>
</div>
  </body>

答案 2 :(得分:0)

在链接标记中使用type属性:

<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">

答案 3 :(得分:0)

这是一个重复的问题:我在这里找到了解决方案。

Where do CSS and JavaScript files go in a Maven web app project?

,而是在@Viriato上用xml做,

  

只是为了澄清这是我必须做的:   确保在servlet-context.xml中具有以下内容:   <resources mapping="/resources/**" location="/resources/" />,   创建一个文件夹,如果在名为resources的webapps下尚不存在,   把你的css文件夹和css文件放在那里,   引用我的css文件如下:   <link rel="stylesheet" href="<%=request.getContextPath()%>/resources/css/960.css"/>

我决定做一个基于java的配置,因为我已经有了Config.java课程。 所以,我将我的Config.java类扩展到抽象类WebMvcConfigurerAdapter,以便使用方法 addResourceHandlers (如果你想使用它,你必须覆盖它)

这就是我必须做的事情:

public class Config extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
...
}

然后我必须将contextpath添加到我的路由中:

<script type="text/javascript" src="http://localhost:8585/electronicaDonPepe/resources/js/jquery-3.1.1.min.js"></script>

<script type="text/javascript" src="http://localhost:8585/electronicaDonPepe/resources/js/bootstrap.min.js"></script> 

<script type="text/javascript" src="http://localhost:8585/electronicaDonPepe/resources/js/angular.min.js"></script> 

<link href="http://localhost:8585/electronicaDonPepe/resources/css/bootstrap.min.css" rel="stylesheet" />

如果您不了解上下文路径,可以使用Scriptlets,如下所示:

<%= request.getContextPath() %>

例如,bootstrap.min.css的代码为:

   <link href="<%= request.getContextPath() %>/resources/css/bootstrap.min.css" rel="stylesheet" />

如果您想了解有关scriptlet的更多信息,可以阅读:http://docs.oracle.com/javaee/5/tutorial/doc/bnaou.html

我计划与Angular合作,所以硬编码路线现在适用于我,我不鼓励这样做,因为这是一种不好的做法。用户不应该能够看到您的资源路线并随之乱七八糟。

感谢您的时间。