【问题标题】:Using CSS with a JSP in Spring Web MVC Application在 Spring Web MVC 应用程序中使用 CSS 和 JSP
【发布时间】:2016-11-13 18:36:49
【问题描述】:

我试图弄清楚如何将存储在资源文件夹中的外部 css 文件用于我的 jsp 页面。到目前为止,我根据研究尝试了一些不同的方法,但尚未找到解决方案。

JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>ProjectX</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/homepage.css" />
</head>
<body>
    <h1>The message of the day is:</h1>
    <p>${titleHomepage}</p>
</body>
</html>

servletConfig.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.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-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <mvc:annotation-driven/>

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

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

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".jsp" />
</bean>

Folder Structure

我也相信将我的资源文件放在 src/man/resources 中是一种更好的结构,我也尝试过使用它,但得到的结果与上述问题相同。

找到的解决方案:

Full Folder Structure

【问题讨论】:

    标签: css jsp spring-mvc servlets


    【解决方案1】:

    你的资源文件夹在绞尽脑汁的地方。

    直接放入webapp文件夹中webapp/WEB-INF/resources或者 更改servletConfig.xml 中的路径。更好的方法是将文件夹移出WEB-INF

    【讨论】:

    • 所以我对你的评论有点困惑。您说将我的资源文件夹放在 webapp 文件夹中,而不是 webapp/resources 中?我已经重新定位了我的资源文件夹,所以它现在驻留在 webapp 而不是 WEB-INF 但仍然无法访问 CSS 文件。
    • 这只是打字时的一个错误......对不起。您是否重新部署并重新启动了服务器?您在浏览器控制台中遇到的错误是什么?
    • 我还注意到我有一个位于 src/main/webapp 的 WEB-INF 文件夹和位于 WebContent 文件夹内的另一个 WEB-INF 文件夹。我不确定我应该使用哪一个。
    • 如果您使用的是Maven,那么它可能应该只是webapp 文件夹。你能展示你的项目的完整结构吗
    • 这是我的结构。出于某种原因,我的项目将我的 WebContent 文件夹用于其资源、servlet 和页面。我将更新我上面的文件夹结构的屏幕截图。
    【解决方案2】:

    问题是我的 maven 项目正在从我的 WebContent 文件夹而不是 src/main/webapp 中提取我需要使用的文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-30
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多