gpt4 book ai didi

java - Spring mvc资源文件无法包含到jsp页面中(Tomcat v9.0、Spring Tool Suite、Maven)

转载 作者:太空宇宙 更新时间:2023-11-04 12:05:13 25 4
gpt4 key购买 nike

我想将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/ 使用 Spring 映射分配 /resources/css 中的所有内容。当您尝试实际链接到 /resources/style 时,您将链接到 /resources/style/style

最佳解决方案

将您的配置更改为:

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

并使用:

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

关于java - Spring mvc资源文件无法包含到jsp页面中(Tomcat v9.0、Spring Tool Suite、Maven),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40442540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com