Accessing CSS from Spring MVC



我需要从Spring MVC访问CSS。我花了很多时间搜索Google和Stack溢出,但没有找到为什么我的CSS无法访问的原因。请帮助...我在servlet.xml中包括了MVC标签,并将CSS保存在WebApps/Resources文件夹中

CSS文件:

@CHARSET "ISO-8859-1";
html, body {
    height: 100%;
    margin: 0;
    font-size: 20px;
}
#header {
    height: 20%;
    background-color:#c4e8a2;
    padding: 1rem;
}
#left {
    width: 20%;
    height: 80%;
    position: fixed;
    outline: 1px solid;
    background:#42b0f4;
}
#right {
    width: 80%;
   /* height: auto;*/
    height: 80%;
    outline: 1px solid;
   /* position: absolute; */
    position:fixed;
    right: 0;
    background:#42b0f4;
}

/*#footer
{
    width:100%;
    height:20%;
    position: absolute;
    bottom : 0;
    height : 50 px ;
    left :0;
    background:#42b0f4;
}*/

#footer {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 1rem;
  background-color:#c4e8a2;
  text-align: center;
}

servlet.xml:

[...]
<context:component-scan base-package="com.controller"></context:component-scan>
<context:component-scan base-package="com.dao"></context:component-scan>
<context:component-scan base-package="com.model"></context:component-scan> 
<mvc:resources mapping="/resources/**" location="/resources/css/"
    cache-period="31556926"/>
<mvc:annotation-driven />   
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
<property name="prefix" value="/WEB-INF/jsp/"></property>  
<property name="suffix" value=".jsp"></property>  
</bean>  
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>  
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>  
<property name="username" value="system"></property>  
<property name="password" value="*****"></property>  
</bean>  
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">  
<property name="dataSource" ref="ds"></property>  
</bean>  

<bean id="dao" class="com.dao.UserDao">  
<property name="template" ref="jt"></property>  
</bean>  

<bean id="loginservice" class="com.service.LoginService">
</bean>  
</beans>  

JSP文件:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login page</title>
<link href="/resources/css/test.css" rel="stylesheet" type="text/css">
  </head>
  <body>
 <div id = "header"> 
<jsp:include page="headerlogin.jsp"/>
</div>
        <h1>Login Here</h1>  
       <form:form method="POST" action="/Project/onSubmit">    
        <table >    
         <tr>    
          <td>Username : </td>   
          <td><form:input path="username"  /></td>  
         </tr> 
          <tr>    
          <td>Password :</td>    
          <td><form:input path="password" type="password" name="password" /></td>  
         </tr>   
         <tr>    
          <td> </td>    
          <td><input type="submit" value="Login" /></td>    
         </tr>    
        </table>    
       </form:form>   
 <div id="footer">
 <jsp:include page="Footer.jsp"/>
</div> 
   </body> 
</body>
</html>

尝试通过C.URL标签访问您的.css。这样:

<link rel="stylesheet" href="<c:url value="/some/realtiv/pathToYour.css" />">

.css文件放入WEB-INF内的资源文件夹中,然后将其放入mvc-dispatcher-servlet.xml

<mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>

由于您的JSP文件在 web-Inf 文件夹中,如何将" lost snippet"添加到您的 web.xml 和Redeploy您的应用程序?默认将从SpringMVC请求过滤器中排除CSS访问请求。

<servlet-mapping>
   <servlet-name>default</servlet-name>
   <url-pattern>*.css</url-pattern>
</servlet-mapping>

以下代码用于将服务器路径添加到您的每个HREF链接

<%
String path = request.getContextPath();
String serverPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
String basePath = serverPath + path+"/";
%>
<base href="<%=basePath%>"/>

最新更新