gpt4 book ai didi

java - 检查jsp是否包含两次

转载 作者:行者123 更新时间:2023-12-01 22:23:09 25 4
gpt4 key购买 nike

有谁知道如何检查一个jsp是否已经包含另一个jsp两次?

mainJSP.jsp[
include myJSP.jsp[

<jsp:include page="foo.jsp"/>
]
include myJSP1.jsp[

<jsp:include page="foo.jsp"/>//This should never happend
]
]

有什么想法吗?

最佳答案

玩了一段时间后,我能够得到你想要的结果,但我只能使用 scriplet 来解决它。假设:

您的 Foo.jsp 是:(将包含在 myJsp1 和 myJsp2 中的页面)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.HashSet" %>

<%String hasInclude = "itHas";
request.setAttribute("hasInclude", hasInclude); %>
<%HashSet<String> hasIncludes = new HashSet<String>();
request.setAttribute("hasIncludes", hasIncludes);%>
<h2>Inside include</h2>

你的myJsp1:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="java.util.HashSet" %>
<h1>Inside jsp1</h1>
<%
String hasInclude = (String)request.getAttribute("hasInclude");
HashSet<String> hasIncludes = (HashSet<String>)request.getAttribute("hasIncludes"); %>
<%if (hasIncludes==null||!hasIncludes.contains(hasInclude)) { %>
<jsp:include page="include.jsp" />
<%if (hasIncludes==null) {
hasIncludes = new HashSet<String>();
hasIncludes.add((String)request.getSession().getAttribute("hasInclude"));
} else {
hasIncludes.add(hasInclude);
}
request.setAttribute("hasIncludes", hasIncludes);%>
<%}%>

您的 myJsp2:(包含前与 myJsp1 的代码相同)

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="java.util.HashSet" %>
<h1>Inside jsp2</h1>
<%
String hasInclude = (String)request.getAttribute("hasInclude");
HashSet<String> hasIncludes = (HashSet<String>)request.getAttribute("hasIncludes"); %>
<%if (hasIncludes==null||!hasIncludes.contains(hasInclude)) { %>
<jsp:include page="include.jsp" />
<%if (hasIncludes==null) {
hasIncludes = new HashSet<String>();
hasIncludes.add((String)request.getSession().getAttribute("hasInclude"));
} else {
hasIncludes.add(hasInclude);
}
request.setAttribute("hasIncludes", hasIncludes);%>

<%}%>

最后是 main.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:include page="myJsp1.jsp" />
<jsp:include page="myJsp2.jsp" />

</body>
</html>

main 的输出将是:

jsp1内部

内部包含

jsp2内部

关于java - 检查jsp是否包含两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29296234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com