java - Spring mvc resources files can't include into jsp page (Tomcat v9.0, Spring Tool Suite, Maven) -
i want include css file jsp page. tried solutions stackoverflow, google... still nothing, doesn't recognize css code.
i tried 3 solutions:
<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" />
servlet-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"> <!-- definition of root spring container shared servlets , filters --> <context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/spring/root-context.xml</param-value> </context-param> <!-- creates spring container shared servlets , 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 project created spring tool suite. project use maven, jre_8, tomcat v9.0
try this:
<link href="<c:url value="/resources/main.css"/>" rel="stylesheet">
explanation
with configuration:
<resources mapping="/resources/**" location="/resources/css/" />
you assign content in /resources/css
spring mapping /resources/
. when try link /resources/style
in real link /resources/style/style
.
best solution
change configuration to:
<resources mapping="/css/**" location="/resources/css/" />
and use:
<link href="<c:url value="/css/main.css"/>" rel="stylesheet">
Comments
Post a Comment