gpt4 book ai didi

java - 使用 spring mvc 在使用 maven 构建的 JSP 中包含 CSS 文件的问题

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

在我的 JSP 文件中包含 css fie 时我遇到了问题。我在这里看到过类似的问题,但都没有解决。所以我想发布我的方法,这也是一种传统方法,但它仍然不起作用。

它的 maven,Spring 元素。

CSS文件目录:

src/main/resources/assets/css/

/src/main/webapp/WEB-INF/dispatcher-servlet.xml

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

<context:component-scan base-package="com.avlino.controller" />

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:annotation-driven></mvc:annotation-driven>

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

</beans>

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.avlino</groupId>
<artifactId>Dashboard</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Dashboard</name>
<url>http://maven.apache.org</url>

<properties>
<spring.version>4.0.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
<version>1.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

<build>
<finalName>Dashboard</finalName>
</build>
</project>

JSP 文件:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Free Bootstrap Admin Template : Dream</title>
<link rel="stylesheet" href="<c:url value="/resources/assets/css/bootstrap.css" />" />
<link rel="stylesheet" href="<c:url value="/resources/assets/css/font-awesome.css" />" />

我仍然无法访问 CSS。非常感谢任何帮助。

最佳答案

src/main/resources 仅用于 Java 资源而不是 Web 静态资源。将 CSS 和 JS 资源移动到 src/main/webapp/WEB-INF/resources/ 并将以下内容添加到 MVC 配置中:

<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>

关于java - 使用 spring mvc 在使用 maven 构建的 JSP 中包含 CSS 文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33587329/

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