当JSP位于WEB-INF文件夹下时,如何使用Jquery.load()函数加载JSP? [英] How do I load JSP with Jquery.load() function when the JSP is under WEB-INF folder?
问题描述
这是我项目中的目录结构
Here's the directory structure that I have in my project
WebContent
--resources
--WEB-INF
--classes
--jsp
dashboard.jsp
home.jsp
--lib
我想使用Jquery方法将home.jsp加载到dashboard.jsp中
I want to load home.jsp inside dashboard.jsp using Jquery method
$(#result").load("home.jsp");
$("#result").load("home.jsp");
但是它不起作用.我相信,我引用home.jsp页面的方式在load方法中是错误的.如何调用驻留在WEB-INF/jsp文件夹下的home.jsp(从dashboard.jsp)?
But it is not working. I believe, the way I am referencing my home.jsp page is incorrect in the load method. How do I make a call to home.jsp (from dashboard.jsp) which resides under WEB-INF/jsp folder?
推荐答案
如注释中所述,无法通过url访问WEB-INF文件夹中的任何文件.但是您可能可以通过jsp:include
As mentioned in the comments it is not possible to access any file within the WEB-INF folder by url. But you may be able to access it via jsp:include
<jsp:include page="WEB-INF/home.jsp" />
(我不知道那是否可行,但值得一试)
URL总是相对于组合" URL(因为缺少更好的词).因此,如果您通过以下方式通过servlet访问.jsp文件:
The url is always relative to the 'combined' URL (for the lack of a better word). So if you access a .jsp file via a servlet with:
request.getRequestDispatcher("/WEB-INF/dashboard.jsp").forward(request, response);
您引用的形式的url实际上是您的servlet的URL.
the url you reference form is actually the URL of your servlet.
考虑到这一点,将使用URL $("#result").load("/WEB-INF/home.jsp")
,但是由于除了Servlet之外,其他任何地方都无法访问整个WEB-INF文件夹(然后只能分派给它们),因此您无法访问home jQuery中的.jsp.
Taking this into account, the URL would be $("#result").load("/WEB-INF/home.jsp")
but since the entire WEB-INF folder is inaccessable from everywhere but Servlets (and then only to dispatch to them), you cannot access home.jsp from jquery.
因此,为您的问题提供解决方案:
So to provide a solution to your problem:
任何一个
- 将home.jsp放到WEB-INF文件夹中,
- 创建一个分派给它的Servlet,
- 尝试jsp:include
这篇关于当JSP位于WEB-INF文件夹下时,如何使用Jquery.load()函数加载JSP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!