【问题标题】:I can't configure the resources path in Maven我无法在 Maven 中配置资源路径
【发布时间】:2017-05-13 13:02:29
【问题描述】:

我无法获得正确的 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

【问题讨论】:

  • 你的 javascripts 工作了吗?
  • 是的,Javascript 工作正常。
  • 您的视图文件放在哪里?在 webapp 本身中?
  • 是的,我将项目构建路径留在了主要问题上方的图片中。又来了:i.stack.imgur.com/xcUto.png
  • 你的html页面放在哪里?

标签: java eclipse maven jakarta-ee


【解决方案1】:

你可以使用这个cdn来引导

<!-- You can use this link for bootstrap cdn  -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->

【讨论】:

  • 是的,我知道。我正在尝试使用下载的 bootstrap.min.css 文件。想象一下,我必须在没有互联网连接的情况下展示这个项目。
【解决方案2】:

在你的链接标签中使用 type 属性:

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

【讨论】:

  • 我已经试过了,但还是一样的反应。它不起作用。
【解决方案3】:

使用

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

效果很好

测试和验证

这里是下面的检查,这里我添加了容器类(这是一个引导类) => 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>

【讨论】:

  • 嗨,链接到服务器工作正常,但这不是我要找的。我正在尝试从我的计算机链接 css 文件。
【解决方案4】:

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

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

而是像@Viriato所说的那样在xml上做,

只是为了澄清这是我必须做的: 确保在您的 servlet-context.xml 中有如下内容: &lt;resources mapping="/resources/**" location="/resources/" /&gt;, 如果在称为资源的 webapps 下尚不存在文件夹,则创建一个文件夹, 将您的 css 文件夹与 css 文件一起放在那里, 参考我的css文件如下: &lt;link rel="stylesheet" href="&lt;%=request.getContextPath()%&gt;/resources/css/960.css"/&gt;

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

这是我必须做的:

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

然后我必须将上下文路径添加到我的路线中:

<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,所以硬编码的路线现在对我有用,我不鼓励这样做,因为这是一种不好的做法。用户不应该能够看到您的资源路由并乱用它们。

感谢您的宝贵时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2013-02-09
    • 1970-01-01
    相关资源
    最近更新 更多