0

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>
4
  • If you put css files into /resources/css/ folder then you use location="/resources/" in servlet.xml and access it using <link href="<c:url value="/resources/css/test.css" />" >. If you want to use location="/resources/css/" then you can access it using <link href="<c:url value="/resources/test.css" />" > Commented Mar 6, 2017 at 17:23
  • I have tried as you said, but its not working :( Commented Mar 7, 2017 at 4:34
  • It might be cache problem. try to hit ctrl + F5 each time to clear cache if you work on Chrome. Commented Mar 7, 2017 at 6:42
  • @GurkanYesilyurt , Tried the same, but no luck :( Commented Mar 7, 2017 at 7:06

3 Answers 3

0

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

<link rel="stylesheet" href="<c:url value="/some/realtiv/pathToYour.css" />">
Sign up to request clarification or add additional context in comments.

1 Comment

Hi..Thanks for your quick reply. But its not working :(
0

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 Comments

<spring:url value="/resources/css/custom.css" var="custom" /> <link href="${custom}" rel="stylesheet" /> Try this
Hi Riyaz , thanks for your reply. Do i need to include any jar to use this?I am getting this error - Unknown tag (spring:url).
yes you need this in your jsp page <%@ taglib prefix="spring" uri="springframework.org/tags"%>
0

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

4 Comments

Sorry..I didn't get you. If i use <url-pattern>*.css</url-pattern> instead of <url-pattern>/</url-pattern> , then my jsp pages will not be accessed.
@Sarbani default is a reserved url and you do not need to create a servlet name called default
Hi lucumt , Thanks for your explanation. I tried the same, but its not working :(
@Sarbani if it's still not work,I would suggest you to use absolute path to access your static files,I have updated my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.