在带有Spring MVC的jsp页面中包含样式表 [英] including style sheet in a jsp page with Spring MVC

查看:85
本文介绍了在带有Spring MVC的jsp页面中包含样式表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从jsp页面链接到样式表.我相信这与我的目录结构有关:

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:

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

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

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

<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" />

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

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.

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

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

<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>

谢谢!

推荐答案

/WEB-INF中的文件不能直接公开访问.在

Files in /WEB-INF are not directly public accessible. Only an intermediating (controller) Servlet can 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.

因此,您在这里基本上有2个选择:

So you have basically 2 options here:

  1. 将CSS文件放入公共Web内容中(只需将一个文件夹移到WEB-INF上方,以便/css/WEB-INF处于同一级别).

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

创建一个Servlet,该Servlet侦听/css/*url-pattern,并通过

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

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

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

这篇关于在带有Spring MVC的jsp页面中包含样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆