Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SpringMVC-包括静态文件/javascript、css_Spring_Model View Controller - Fatal编程技术网

SpringMVC-包括静态文件/javascript、css

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:

我已经创建了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://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'/>">