繁体   English   中英

Spring mvc资源文件不能包含到jsp页面中(Tomcat v9.0,Spring Tool Suite,Maven)

[英]Spring mvc resources files can't include into jsp page (Tomcat v9.0, Spring Tool Suite, Maven)

我想将css文件包含到jsp页面中。 我尝试了stackoverflow,google的所有解决方案......但仍然没有,它无法识别css代码。

我试过这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" />

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

    <!-- 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;
    }

使用Spring Tool Suite创建的Mvc项目。 项目使用Maven,JRE_8,Tomcat v9.0

尝试这个:

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

说明

使用此配置:

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

您将通过/resources/分配/resources/css所有内容和spring映射。 当您尝试链接到/resources/style ,您将链接到/resources/style/style

最佳方案

将您的配置更改为:

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

并使用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM