简体   繁体   English

JSP包含动态页面

[英]JSP include dynamic page

I've got some of my include pages stored in the Servlet context. 我已经将一些包含页面存储在Servlet上下文中。 I want to include some pages inside a JSP dynamically, but I can't. 我想在JSP中动态包含一些页面,但是我不能。 What I do is: 我要做的是:

<jsp:include page="<%=(String)application.getAttribute("headURL")%>"/>

But it gives me an error related to the quotes. 但这给我一个与引号有关的错误。 I've tried to change it to: 我试图将其更改为:

<jsp:include page="<%=(String)application.getAttribute(\"headURL\")%>"/>

But it neither doesn't works. 但这都不行。

How could I do this? 我该怎么办?

Thanks! 谢谢!

You can use Expression language to access the URL. 您可以使用表达式语言来访问URL。 So you dont need to escape quotes. 因此,您不需要转义报价。

Here is an example you can do this: 这是您可以执行此操作的示例:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <c:set value="/WEB-INF/jspf/head.jspf" var="headURL" scope="application" />
    <jsp:include page="${headURL}"></jsp:include>
</body>
</html>

The file located in /Project/WebContent/WEB-INF/jspf/head.jspf will be included. 将包含/Project/WebContent/WEB-INF/jspf/head.jspf的文件。

EDIT: 编辑:
This line is only for the test. 该行仅用于测试。 I set a context attribute headURL : 我设置了一个上下文属性headURL

<c:set value="/WEB-INF/jspf/head.jspf" var="headURL" scope="application" />

You need only: 您只需要:

<jsp:include page="${headURL}"></jsp:include>

And a Path relative to the WebContent. 以及相对于WebContent的路径。

Do like this: 这样做:

<jsp:include page="${headURL}"></jsp:include>

First, Servlet container will lookup headURL in Page scope, then Request scope, then Session scope, and then Application scope (your case) 首先,Servlet容器将在Page范围中查找headURL,然后是Request范围,然后是Session范围,然后是Application范围(您的情况)

headURL must be set at somewhere into Application scope before the JSP invoked. 必须在调用JSP之前将headURL设置在Application范围的某个位置。

BUT I think It must be /WEB-INF/jspf/head.jspf ( app name need to be removed ) 但是我认为它必须是/WEB-INF/jspf/head.jspf (需要删除应用名称)

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

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