2016-11-13 68 views
0

我想弄清楚如何使用存储在我的jsp页面的资源文件夹中的外部css文件。到目前为止,我基于研究尝试了一些不同的东西,但还没有找到解决方案。在Spring Web MVC应用程序中使用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 /人/资源在我的资源文件我也尝试过,但得到了相同的结果作为上面的问题。

发现的解决方案:

Full Folder Structure

回答

0

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

0

您具有拧的地方你的资源文件夹。

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

+0

所以我有点困惑你的评论。你说要将我的资源文件夹放在webapp文件夹中,但不是webapp/resources?我已经重新定位了我的资源文件夹,因此它现在驻留在webapp中,而不是WEB-INF中,但仍然无法访问CSS文件。 – Brenton

+0

这只是一个错误,而输入.....抱歉。您是否重新部署并重新启动了服务器?您在浏览器控制台中遇到的错误是什么? –

+0

我也注意到我有一个位于src/main/webapp的WEB-INF文件夹和位于WebContent文件夹内的另一个WEB-INF文件夹。我不确定不同的和我应该使用哪一个。 – Brenton

相关问题