我是Spring MVC中的新手,我在Spring MVC项目中遇到以下问题,该项目使用FreeMarker作为我的观点。
我发现在尝试导入一些资源时遇到了一些问题(CSS文件和定义到我项目的myProject\src\main\webapp\resources\
目录中的图像)。
所以我有这个header.ftl
文件代表我所有页面的标题:
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js is-ie8"><![endif]-->
<!--[if gt IE 8]><!--><html class="no-js"><!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MY WEBSITE</title>
<meta name="robots" content="noindex,nofollow">
<link rel="stylesheet" href="resources/css/webfont.css">
<link rel="stylesheet" href="resources/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="resources/bootstrap/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="resources/plugins/bs-select/bootstrap-select.min.css">
<link rel="stylesheet" href="resources/plugins/bs-dialog/bootstrap-dialog.min.css">
<link rel="stylesheet" href="resources/css/style.css" />
</head>
<body id="main">
<div class="container page-header">
<h1>My WebSite</h1>
</div>
因此,正如您所看到的,我将一些定义到myProject\src\main\webapp\resources\
目录的css资源导入到我的项目中。
问题是我的项目中没有导入这些资源(所以我没有CSS设置)。
这是以前查看FirFox生成的HTML代码的CSS资源的渲染输出:
<link rel="stylesheet" href="resources/css/webfont.css">
<link rel="stylesheet" href="resources/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="resources/bootstrap/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="resources/plugins/bs-select/bootstrap-select.min.css">
<link rel="stylesheet" href="resources/plugins/bs-dialog/bootstrap-dialog.min.css">
<link rel="stylesheet" href="resources/css/style.css" />
此项目中唯一的Spring xml配置文件名为 web-context.xml (这是 FreeMarker 的配置位置,其中指定了我的控制器的组件扫描),这一个:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="
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.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:annotation-config />
<!-- Attivazione dei Proxy tramite aspetto -->
<aop:aspectj-autoproxy />
<context:property-placeholder location="classpath*:*.properties"
ignore-resource-not-found="true" />
<!-- Controller -->
<context:component-scan base-package="it.istruzione.iam.ssum.registrazione">
</context:component-scan>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Configurazione del resolver FreeMarker -->
<!-- freemarker config -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="suffix" value=".ftl" />
</bean>
<!-- Configurazione MVC -->
<mvc:annotation-driven />
</beans>
如您所见,我已添加此行以尝试处理对/resources/
目录内容的请求:
<mvc:resources mapping="/resources/**" location="/resources/" />
但它仍然无法正常工作,我无法访问位于/resources/
子目录中的CSS资源。
当我将上一页显示到浏览器中时,在Eclipse控制台中,我获得了以下与这些资源相关的错误消息:
16:55:40,249 ERROR [org.jboss.as] (Controller Boot Thread) JBAS015875: JBoss EAP 6.2.0.GA (AS 7.3.0.Final-redhat-14) started (with errors) in 13261ms - Started 176 of 240 services (3 services failed or missing dependencies, 59 services are passive or on-demand)
16:55:42,623 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-1) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/] in DispatcherServlet with name 'dispatcher'
16:57:14,624 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-1) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/iscrizione/resources/css/webfont.css] in DispatcherServlet with name 'dispatcher'
16:57:14,640 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-1) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/iscrizione/resources/css/style.css] in DispatcherServlet with name 'dispatcher'
16:57:14,658 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-6) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/iscrizione/assets/js/modernizr-custom.js] in DispatcherServlet with name 'dispatcher'
16:57:14,659 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-3) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/iscrizione/resources/bootstrap/css/bootstrap-theme.min.css] in DispatcherServlet with name 'dispatcher'
16:57:14,661 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-4) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/iscrizione/resources/plugins/bs-select/bootstrap-select.min.css] in DispatcherServlet with name 'dispatcher'
16:57:14,664 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-5) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/iscrizione/resources/plugins/bs-dialog/bootstrap-dialog.min.css] in DispatcherServlet with name 'dispatcher'
16:57:14,664 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-2) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/iscrizione/resources/bootstrap/css/bootstrap.min.css] in DispatcherServlet with name 'dispatcher'
为什么呢?怎么了?我错过了什么?我该如何解决这个问题?
答案 0 :(得分:0)
您可以使用JSTL
`来确保您的网址正确无误:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<link href="<c:url value="/resources/css/webfont.css" />" rel="stylesheet">
修改抱歉,我没有注意到它是一个freemarker页面:
<#import "/spring.ftl" as spring />
<link href="<@spring.url '/resources/css/webfont.css'/>" rel="stylesheet">