简体   繁体   English

将CSS文件链接到JSP

[英]Linking a CSS file into JSP

I have been trying to get my '.jsp' file to use a '.css' file that I have created. 我一直试图让我的'.jsp'文件使用我创建的'.css'文件。 I had originally stored the '.css' file in my 'WEB-INF' folder but through some searching found that the 'WEB-INF' folder is not public and can therefore not store the file so I moved it outside into the 'webapp' folder but I am still not getting anywhere. 我最初将'.css'文件存储在我的'WEB-INF'文件夹中,但是通过一些搜索发现'WEB-INF'文件夹不是公开的,因此无法存储该文件,因此我将其移到了'webapp '文件夹,但我仍然没走。

The files 'index.jsp' and 'index.css' files are in the same folder: 文件“ index.jsp”和“ index.css”文件位于同一文件夹中:

'HelloWord/src/main/webapp' 'HelloWord / src / main / webapp'

My '/jsp' file links the '.css' as shown 我的“ / jsp”文件链接“ .css”,如下所示

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="${pageContext.request.contextPath}/index.css" type="text/css" /> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>MM Vehicle Registration</title>
</head>

I get the following error when I try to run: 尝试运行时出现以下错误:

Nov 28, 2015 3:42:00 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/HelloWorld/index.css] in DispatcherServlet with name 'dispatcher'

The web page still displays ony without the css decoration. 网页仍然显示不带CSS修饰的字体。 The file is already declared in a '.jsp' that is being rendered fine so why would I receiving this error? 该文件已在呈现良好的'.jsp'中声明,为什么我会收到此错误?

try replace 尝试更换

'${pageContext.request.contextPath}/index.css' 

with

'/index.css'.

Are both index.css and index.jsp in the root folder of webapp? webapp的根文件夹中是否同时存在index.css和index.jsp? Ok,the strutre is right.Now I suggest you to check you web.xml. 好的,撑杆是正确的。现在,我建议您检查一下web.xml。 You might have something like this? 您可能会遇到这样的事情?

<servlet-mapping>
  <servlet>dispatcher</servlet>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

Try calling your controllers with a different extension (.do for example) and update the servlet-mapping to suit 尝试使用其他扩展名(例如,.do)调用控制器,并更新servlet映射以适合

 <servlet-mapping>
  <servlet>dispatcher</servlet>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>

then try to access to /helloworld/index.css.if ok,rollback '/index.css'. 然后尝试访问/helloworld/index.css。如果可以,请回滚“ /index.css”。 to '${pageContext.request.contextPath}/index.css' 到“ $ {pageContext.request.contextPath} /index.css”

Spring takes control over all URLs. Spring会控制所有URL。 You need to put <mvc:resources> into your configurtion and preferably put your client resources into WEB-INF/resources/ directory. 您需要将<mvc:resources>放入配置中,最好将客户端资源放入WEB-INF / resources /目录中。 Also check How to handle static content in Spring MVC? 还要检查如何在Spring MVC中处理静态内容?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM