Java 使用 Spring MVC 在 jsp 页面中包含样式表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2231792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:01:17  来源:igfitidea点击:

including style sheet in a jsp page with Spring MVC

javaspringjspspring-mvc

提问by Casey

I am having trouble linking to a style sheet from a jsp page. I believe it has something to do with my directory structure which is:

我在从 jsp 页面链接到样式表时遇到问题。我相信这与我的目录结构有关:

WEB-INF
  |-- css
  |    |-- main.css
  |
  |-- jsp
       |-- login.jsp

I have tried various forms of the standard html link tag such as:

我尝试了各种形式的标准 html 链接标签,例如:

<link href="css/main.css" rel="stylesheet" type="text/css" media="screen" />
<link href="main.css" rel="stylesheet" type="text/css" media="screen" />
<link href="WEB-INF/css/main.css" rel="stylesheet" type="text/css" media="screen" />

I have also tried including the css file in the jsp folder and linking to it directly. Nothing works. When I view the source after deployment, and try to access the CSS file directly, it's not there, but this is no surprise to me since it is in the WEB-INF directory.

我还尝试将 css 文件包含在 jsp 文件夹中并直接链接到它。什么都行不通。当我在部署后查看源代码,并尝试直接访问 CSS 文件时,它不存在,但这对我来说并不奇怪,因为它在 WEB-INF 目录中。

I have also verified that it is getting deployed with the rest of the application. The jsp source is:

我还验证了它正在与应用程序的其余部分一起部署。jsp源码为:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>

<link href="css/main.css" rel="stylesheet" type="text/css" media="screen" />

</head>

<body>
<div id="wrapper">
<div id="header">
<div id="logout">&nbsp;</div>
<h1>Login</h1>
</div>
<div id="content" class="content">
  <form action="" method="post" name="login-form">
    <fieldset>
      <legend>Login</legend>
      <table border="0" align="center">
        <tr>
          <td><label>User Name:</label></td>
        <td><input type="text" name="userName" /><br><br></td>
        </tr>
        <tr>
            <td><label>Password:</label></td>
        <td><input type="text" name="password" /><br><br></td>
         </tr>
        </table>
      </fieldset>
      <div class="buttons">
        <input type="submit" name="submit" value="Login" />&nbsp;&nbsp;&nbsp;
        <input type="button" name="cancel" value="Cancel" />
      </div>
    </form>
  </div>
</div>
</body>
</html>

Thanks!

谢谢!

采纳答案by BalusC

Files in /WEB-INFare not directly public accessible. Only an intermediating (controller) Servletcan access and stream them for you with help of ServletContext#getResourceAsStream(). That's exactly what Spring (as any other decent MVC framework) does with JSP files. You can't access JSP files directly by URL. That would potentially leak source code or break the application behaviour.

中的文件/WEB-INF不能直接公开访问。只有中介(控制器)Servlet才能在ServletContext#getResourceAsStream(). 这正是 Spring(与任何其他体面的 MVC 框架一样)对 JSP 文件所做的。您不能通过 URL 直接访问 JSP 文件。这可能会泄漏源代码或破坏应用程序行为。

So you have basically 2 options here:

所以你在这里基本上有两个选择:

  1. Put CSS files in public webcontent (just move one folder above WEB-INF, so that /cssis at same level as /WEB-INF).

  2. Create a Servlet which listens on an url-patternof /css/*, gets the requested CSS file by HttpServletRequest#getPathInfo()and basically gets an InputStreamfrom it using the aforementioned ServletContext#getResourceAsStream()and writes it to the OutputStreamof the response along a correct set of response headers with at least Content-Typeand Content-Length.

  1. 将 CSS 文件放在公共网页内容中(只需在 上方移动一个文件夹WEB-INF,使其/css与 处于同一级别/WEB-INF)。

  2. 创建一个侦听url-patternof的 Servlet,通过上述方法/css/*获取请求的 CSS 文件,HttpServletRequest#getPathInfo()并基本上从中获取一个InputStreamServletContext#getResourceAsStream()然后将其写入OutputStream响应的 ,并沿着一组正确的响应标头至少包含Content-TypeContent-Length

After all I think option 1 is easier and more suitable for your requirement ;)

毕竟我认为选项 1 更容易,更适合您的要求;)

回答by joseph

or try this

或者试试这个

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

" type="text/css"/>

" type="text/css"/>

it may work.

它可能工作。

回答by Slava Semushin

Spring-style implementation of 2nd approach from @BalusC is to using mvc:resources, like that:

@BalusC 第二种方法的 Spring 风格实现是使用mvc:resources,如下所示:

<mvc:resources mapping="/css/**" location="/WEB-INF/css/*" />

and after this your main.cssfile should be available at /css/main.css

在此之后,您的main.css文件应该可以在/css/main.css

(NOTE: if it does not work, check that DispatcherServletis mapped to /)

(注意:如果它不起作用,请检查DispatcherServlet映射到/