Accessing CSS from Spring MVC

2020-08-04 04:14发布

问题:

I need to access CSS from spring mvc. I spend a lot time searching google and stack overflow but didn't find why my css is not being accessed.Please help.... I have included mvc tag in servlet.xml and kept the css in webapps/resources folder

CSS file:

@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 file:

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

回答1:

Try to access your .css via the c.url tag. Like this:

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


回答2:

Put .css file in resource folder inside WEB-INF and then put this in mvc-dispatcher-servlet.xml

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


回答3:

Since your jsp file are within the WEB-INF folder,how about add the follow snippet to your web.xml and redeploy your applicationb?The default will exclude the css access request from the springMVC request filter.

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

Below code is used to add server path to each of your href link

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