简体   繁体   English

JSP - 包含资源文件夹中的文件

[英]JSP - Include a file from the resources folder

How do I include a file into my jsp file from the resources path?如何从资源路径将文件包含到我的 jsp 文件中? What should the path be specifically.路径具体应该是什么。 Using either @include or jsp:include .使用@includejsp:include

My directory structure:我的目录结构:

在此处输入图片说明

I want to include resources/static/template/index.html what should be the include path in home.jsp .我想包含resources/static/template/index.html应该是home.jsp的包含路径。

You shouldn't have .jsp files in any subdirectory of WEB-INF , and if webapp is the context root of your web application, there's no way you can include anything outside it, since both the include directive and standard tag only work on files under the context root.您不应该在WEB-INF任何子目录中包含 .jsp 文件,并且如果webapp是您的 Web 应用程序的上下文根,则您无法在其之外包含任何内容,因为 include 指令和标准标记都仅适用于文件在上下文根下。

If you were to move the whole resources directory under webapp , then you'll be able to use one of the following:如果您要将整个resources目录移动到webapp下,那么您将能够使用以下方法之一:

<%@include file="/resources/static/template/index.html"%>

Or或者

<jsp:include page="/resources/static/template/index.html"/>

I have solved it like this我已经这样解决了

  • Move the static files like .html from src/main/resources/static/ to src/main/webapp/includes/.将 .html 等静态文件从 src/main/resources/static/ 移动到 src/main/webapp/includes/。 eg I have my header.html and footer.html here.例如,我在这里有我的 header.html 和 footer.html。
  • To include .html file, use <%@include file="????.html" %>要包含 .html 文件,请使用 <%@include file="????.html" %>
  • To include .jsp file, use <jsp:include page="????.jsp"/>要包含 .jsp 文件,请使用 <jsp:include page="????.jsp"/>
  • Replace ????替换??? with the relative path of the file与文件的相对路径

尝试使用:

<%@include file="../../resources/static/template/index.html" %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM