javascript 如何从控制器返回一个 jsp 作为弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15641592/
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
How to return a jsp from controller as a popup
提问by user2161881
I have a Spring MVC controller that gets called as a redirect based on certain condition. Everything seems to be working fine besides that I would like the returned JSP as modal window. Any suggestions on how to achieve this?
I want myModalWindow.jsp to be returned as modal window or popup.
我有一个 Spring MVC 控制器,它根据某些条件被称为重定向。除了我希望返回的 JSP 作为模式窗口之外,一切似乎都运行良好。关于如何实现这一目标的任何建议?
我希望 myModalWindow.jsp 作为模式窗口或弹出窗口返回。
@RequestMapping( value ="/abc/xyz.html")
public String getMyModal()
{
//Do something here and return modal
return "myModalWindow";
}
Edit 1: This call is made through another controller which in turn is called via an Ajax call.
编辑 1:此调用是通过另一个控制器进行的,而该控制器又通过 Ajax 调用进行调用。
回答by Michael Técourt
Modal window = javascript... you cannot ask the server the open a pop-up in the client's browser. Unless maybe your previous page opens the "/abc/xyz/html" in a new browser window, but it will happen no matter what view your controller returns... and that's not a modal window anyway.
Modal window = javascript...你不能要求服务器在客户端的浏览器中打开一个弹出窗口。除非您的上一页在新的浏览器窗口中打开“/abc/xyz/html”,但无论您的控制器返回什么视图都会发生这种情况……而且无论如何这都不是模态窗口。
For a real pop-up, all you can do is return a page that will open your modal window (jsp) in the onLoad function :
对于真正的弹出窗口,您所能做的就是返回一个页面,该页面将在 onLoad 函数中打开您的模式窗口 (jsp):
@RequestMapping( value ="/abc/xyz.html")
public String getMyModal()
{
//Do something here and return modal
return "myPageThatOpensAModalWindow";
}
... and then in myPageThatOpensAModalWindow.jsp :
...然后在 myPageThatOpensAModalWindow.jsp 中:
<html>
<head>
<script>
function loadModalWindow() {
// open your window here
window.open("/myModalWindowURL", ...);
}
</script>
</head>
<body onload="loadModalWindow()">
<h1>Hello World!</h1>
</body>
</html>
回答by frisco
Use a Javascript library to load the modal from "/abc/xyz.html". Maybe you need to change the template of that jsp to include only the content of the modal (avoiding, header, footer and main html estructure).
使用 Javascript 库从“/abc/xyz.html”加载模态。也许您需要更改该 jsp 的模板以仅包含模式的内容(避免、页眉、页脚和主 html 结构)。
There is no html header, call or whatever that allows you to show a modal on its own, it is usually a div with absolute positioning and a semi opaque layer over the page that blocks all clicks what it is usually used as a modal in web design
没有 html 标题、调用或任何允许您自己显示模态的东西,它通常是一个具有绝对定位的 div 和页面上的半透明层,可以阻止所有点击它通常用作网络中的模态设计