Spring MVC:在视图中包含一个 JSP(JSP 安装在视图旁边,因此无法从外部控制器访问)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9465010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:58:10  来源:igfitidea点击:

Spring MVC: Including a JSP inside a view (JSP is installed next to view so is inaccesible from outside controller)?

springjspspring-mvc

提问by Martin

I have created a view and its working well but i now need to Include another JSP inside the page.

我已经创建了一个视图并且它运行良好,但我现在需要在页面中包含另一个 JSP。

Considering my views are installed in the protected area of WEB-INF/Views (hence its not available in my resources directory where my imgs, css are)

考虑到我的视图安装在 WEB-INF/Views 的受保护区域中(因此它在我的 imgs、css 所在的资源目录中不可用)

I have tried using

我试过使用

   <%@ include file="/views/shared/items/NewItem.jsp" %>

And it always gives me FileNotFound, taking into consideration that my NewItem.jsp is installed with my other views (i.e. NOT accesible via the normal routes but controlled by controllers) how can i include a JSP files that is installed next to my view

它总是给我 FileNotFound,考虑到我的 NewItem.jsp 与我的其他视图一起安装(即不能通过正常路由访问,但由控制器控制)我如何包含安装在我的视图旁边的 JSP 文件

If i take out the "include file" my view renders without issues.

如果我取出“包含文件”,我的视图呈现没有问题。

I am sure i am missing something here?

我确定我在这里遗漏了什么?

Thanks in advance

提前致谢

回答by JB Nizet

If NewItem.jspis in /WEB-INF/views/shared/items/NewItem.jsp, then you have to use this path when incuding it:

如果NewItem.jsp是 in /WEB-INF/views/shared/items/NewItem.jsp,则在包含它时必须使用此路径:

<%@ include file="/WEB-INF/views/shared/items/NewItem.jsp" %>

回答by Dexterous

Better to user

对用户更好

<jsp:include />

instead of

代替

<%@ include />

and send request to controller and controller will handle view

并向控制器发送请求,控制器将处理视图

Sending request to contoller

向控制器发送请求

    <jsp:include page="${request.contextPath}/newItem"></jsp:include>

Controller

控制器

@RequestMapping(method = RequestMethod.GET, value = "newItem")
public String newItem(Model model) {
    return "shared/items/NewItem";
}