SpringMVC-包括静态文件/javascript、css
我已经创建了MVC应用程序 我想在jsp中包含js或css文件 我的静态文件位于以下位置: - webapp -js/jquery.js -WEB-INF| | - jsp/*.jsp 这意味着,MVC试图将url映射到js文件 我认为我的配置有些问题,但我不知道是什么 我的web.xml是:SpringMVC-包括静态文件/javascript、css,spring,model-view-controller,Spring,Model View Controller,我已经创建了MVC应用程序 我想在jsp中包含js或css文件 我的静态文件位于以下位置: - webapp -js/jquery.js -WEB-INF| | - jsp/*.jsp 这意味着,MVC试图将url映射到js文件 我认为我的配置有些问题,但我不知道是什么 我的web.xml是: <?xml version="1.0" encoding="UTF-8"?> http:
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd“>
上下文配置位置
/WEB-INF/spring/root-context.xml
org.springframework.web.context.ContextLoaderListener
appServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/appServlet/servlet-context.xml
1.
appServlet
/*
冬眠过滤器
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
冬眠过滤器
/*
将您的
DispatcherServlet
映射更改为,例如:
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
appServlet
*.jsp
或者其他一些不冲突的url模式
,如*.htm
或/controllers/*
。请记住,从现在起,所有控制器都将仅通过此模式可用
现在它正在拦截web应用程序中的所有内容,包括.js
文件、图像等
与
hibernateFilter
一样,在获取.js
文件时,您实际上不需要打开Hibernate会话,不是吗?将此添加到您的配置中,并根据需要修改位置
<mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>
请参见此使用spring JSTL标记包括外部脚本文件或样式表。 首先,您应该在JSP中包含标记库,如下所示
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
然后您可以使用
<script type="text/javascript" src="<spring:url value="/js/jquery.js"/>"></script>
我同意你的答案。但是在style.css文件中声明与图像路径相关的url --style.css-- 如何在style.css文件中使用标记
在浏览器IE/Firefox中查看
--jsp文件---
为什么不使用简单的jsp核心
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<link rel="stylesheet" type="text/css" href="<c:url value='/resources/css/bootstrap.css'/>" />
在您的配置文件(*-servlet.xml)中添加mvc:resources,您可以发现它是有效的我刚刚按照它放置css、js、jquery和图像文件。它对我有效 在servlet-context.xml中
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/assets/" />
在JSP中,导入标记库
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
加上
<link rel="stylesheet" href="<c:url value='/resources/css/custom.css'/>">
但是,当我这样做时,我的实际映射将被分发。在更改我的url:Not Workne chenge之后-我添加了另一个带有*.js的servlet映射请参见:
<link href="<spring:url value="/resources/style.css"/>" rel="stylesheet" type="text/css" media="screen">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<link rel="stylesheet" type="text/css" href="<c:url value='/resources/css/bootstrap.css'/>" />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/assets/" />
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<link rel="stylesheet" href="<c:url value='/resources/css/custom.css'/>">