xml - Spring-MVC : Unable to access static files in the ressource folder -
i have maven webapp running in tomcat server, using spring-mvc 4 dependency.and can't load login.css file located in folder named css inside resource folder of app. request returns 404 file not found error.
the url being used access file : http://localhost:8080/wfmconf/resources/css/login.css
the css doesn't load when access jsp page directly either.
here relevant code :
web.xml :
<web-app> <display-name>archetype created web application</display-name> <context-param> <param-name>contextconfiglocation</param-name> <param-value> /web-inf/spring/spring-security.xml /web-inf/spring/spring-database.xml </param-value> </context-param> <!-- spring security --> <filter> <filter-name>springsecurityfilterchain</filter-name> <filter-class>org.springframework.web.filter.delegatingfilterproxy </filter-class> </filter> <filter-mapping> <filter-name>springsecurityfilterchain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class> org.springframework.web.context.contextloaderlistener </listener-class> </listener> <servlet> <servlet-name>appservlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/spring/appservlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appservlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
servlet-context.xml :
<!-- enables spring mvc @controller programming model --> <annotation-driven /> <!-- handles http requests /resources/** efficiently serving static resources in ${webapproot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- resolves views selected rendering @controllers .jsp resources in /web-inf/views directory --> <beans:bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <beans:property name="prefix" value="/web-inf/vues/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="com.myapp.controllers" /> </beans:beans>
spring-security.xml :
<http pattern="/resources/**" security="none" /> <http auto-config="true" use-expressions="true"> <access-denied-handler error-page="/403" /> <!-- <intercept-url pattern="/admin**" access="hasrole('admin')" /> --> <form-login login-page="/login" default-target-url="/index" authentication-failure-url="/login?error" username-parameter="username" password-parameter="password" /> <logout logout-success-url="/login?logout" invalidate-session="true"/> <!-- enable csrf protection --> <csrf/> </http> <authentication-manager> <authentication-provider> <jdbc-user-service data-source-ref="datasource" users-by-username-query= "select login,pwd, enabled utilisateur login=?" authorities-by-username-query= "select login, role role login =?" /> </authentication-provider> </authentication-manager>
login.jsp :
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1" iselignored="false"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>login page</title> <link href="<c:url value="/resources/css/login.css" />" rel="stylesheet" type="text/css" /> </head>
edit :
the output link on generated html : <link href="/wfmconf/resources/css/login.css" rel="stylesheet">
the directory structure following :
->src ->main ->java ->com.myapp.controllers.maincontroller ->resources ->js ->img ->css ->login.css ->webapp ->web-inf ->spring ->appservlet ->servlet-context.xml ->spring-database.xml ->spring-security.xml ->vues ->login.jsp ->web.xml
i suggest add below line in servlet-context.xml
.
<mvc:default-servlet-handler/>
and access css
login.jsp
below.
<link href="<c:url value="${pagecontext.request.contextpath}/resources/css/login.css" />" rel="stylesheet" type="text/css" />
Comments
Post a Comment