Весна MVC.Почему ресурсы не видны со страницы JSP? - PullRequest
0 голосов
/ 11 октября 2018

У меня есть простое весеннее приложение MVC.Я пытаюсь добавить CSS как статический ресурс в мое приложение, но моя страница jsp не может его найти.

Моя страница jsp выглядит так:

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><fmt:message key="app.title"/></title>
        <link rel="stylesheet" href="/resources/css/style.css">
    </head>
    <body>
         ..................................................................
    </body>
    </html>

Также мой контекст Spring-mvc выглядитэто:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns="http://www.springframework.org/schema/beans"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<mvc:annotation-driven/>

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

<context:component-scan base-package="ru.javawebinar.**.web"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:viewClass="org.springframework.web.servlet.view.JstlView"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp"/>

И мои ресурсы расположены так:

enter image description here

Ответы [ 2 ]

0 голосов
/ 11 октября 2018

Попробуйте следующие изменения:

В spring-web-config.xml:

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

В вашем файле jsp:

<link href="<c:url value="/resources/css/style.css" />" rel="stylesheet">

ОБНОВЛЕНИЕ:

Для Spring вы можете попробовать в своем файле * .jsp:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title><fmt:message key="app.title"/></title>
    <spring:url value="/resources/css/style.css" var="mainCss" />
    <link href="${mainCss}" rel="stylesheet" />
  </head>
  <body>
    ..................................................................
  </body>
</html>
0 голосов
/ 11 октября 2018

Вы используете JSTL?Если это так, вы можете включить, как это.

 <link href="<c:url value="/resources/css/style.css" />" rel="stylesheet">

другой способ:

<link href="${pageContext.request.contextPath}/resources/css/style.css" rel="stylesheet" >
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...