Spring-MVC:无法访问ressource文件夹

时间:2015-04-29 15:03:35

标签: xml spring jsp spring-mvc spring-security

我在tomcat服务器上运行maven webapp,使用spring-mvc 4依赖项。我无法加载位于名为 css <的文件夹中的 login.css 文件/ strong>在应用程序的资源文件夹中。请求返回404文件未找到错误。

用于访问该文件的URL是:    http://localhost:8080/WFMConf/resources/css/login.css

当我直接访问JSP页面时,css不会加载。

这是相关代码:

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 the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /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 from utilisateur where login=?"
      authorities-by-username-query=
        "select login, role from role where 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>

修改:

生成的html上的输出链接是: <link href="/WFMConf/resources/css/login.css" rel="stylesheet">

目录结构如下:

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

1 个答案:

答案 0 :(得分:0)

我建议在servlet-context.xml添加以下一行。

<mvc:default-servlet-handler/>

并从css访问login.jsp,如下所示。

<link href="<c:url value="${pageContext.request.contextPath}/resources/css/login.css" />" rel="stylesheet"     type="text/css" />