Я хочу включить файл css в страницу jsp. Я пробовал все решения из stackoverflow, google ... но все равно ничего, он не распознает код css.Файлы ресурсов Spring mvc не могут быть включены в страницу jsp (Tomcat v9.0, Spring Tool Suite, Maven)
Я пытался с этим 3 решения:
<link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">
<spring:url value="/resources/css/main.css" var="mainCss" />
<link type="text/css" href="${pageContext.request.contextPath}/css/main.css" rel="stylesheet" />
сервлет-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
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-4.0.xsd">
<context:component-scan base-package="com.projekt.springmvc" />
<annotation-driven />
<resources mapping="/resources/**" location="/resources/css/" />
<default-servlet-handler />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<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>
</web-app>
home.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ page session="false" %>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Home</title>
<!-- First solution -->
<link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">
<!-- Second solution -->
<spring:url value="/resources/css/main.css" var="mainCss" />
<!-- Third solution -->
<link type="text/css" href="${pageContext.request.contextPath}/css/main.css" rel="stylesheet" />
</head>
<body>
<h1>
Hello world!
</h1>
</body>
</html>
main.css
body{
padding-top:70px;
font-size:30px;
}
Mvc проект, созданный с Spring Tool Suite. Проект использования Maven, JRE_8, Tomcat v9.0
Работает в Затмении, но не в Spring Tool Suite, интересно .. Спасибо за решение!!! :) – Ivan