Javascript 在Javascript中打开另一个jsp页面的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11694911/
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
Method to open another jsp page in Javascript
提问by user1506919
I am trying to use buttons in an html/javascript program to redirect someone to another one of my pages. Basically, I created a .jsp page that gives the user some buttons to click. If the user clicks a button, it calls a method that is supposed to open a new .jsp page that I created under the same project. However, I have no clue how to do this as I am brand new to Javascript and HTML. I provided an example below:
我正在尝试使用 html/javascript 程序中的按钮将某人重定向到我的另一个页面。基本上,我创建了一个 .jsp 页面,它为用户提供了一些可单击的按钮。如果用户单击一个按钮,它会调用一个方法,该方法应该打开我在同一项目下创建的新 .jsp 页面。但是,我不知道如何做到这一点,因为我是 Javascript 和 HTML 的新手。我在下面提供了一个例子:
Sample Page
示例页面
<html>
<title>Web Page</title>
<body>
<p>Please click a button to redirect you to the page you wish to go to.</p>
<br>
<form>
<input type="button" id="idname" value = "General Info " onclick="goToInfo()"/><br>
</form>
<br>
<form>
<input type="button" id="idname" value = "Other Information" onclick="goToOther()"/><br>
</form>
<script type="text/javascript">
function goToInfo(){
}
function goToOther(){
}
</script>
</body>
</html>
*For example, I have another page in my NetBeans project that is called "GeneralInfo.jsp" and I want goToInfo()
to be able to redirect the person to that page. Thank you for your help.
*例如,我的 NetBeans 项目中有另一个名为“GeneralInfo.jsp”的页面,我希望goToInfo()
能够将该人重定向到该页面。感谢您的帮助。
回答by jmort253
You could use window.locationto redirect the user to the corresponding JSP pages; however, you'll need to make sure your paths in the code below match the actual paths to your JSP page as mapped by your servlet or based on the absolute path relative to the application.
您可以使用window.location将用户重定向到相应的 JSP 页面;但是,您需要确保以下代码中的路径与您的 servlet 映射的 JSP 页面的实际路径或基于相对于应用程序的绝对路径相匹配。
function goToInfo(){
window.location = '/GeneralInfo.jsp';
}
function goToOther(){
window.location = '/someOtherJSPPage.jsp';
}
If you get 404 errors when trying to redirect to your JSP page, try turning up your logging level to ALL or DEBUG so that you can see the logs from the framework or Java container, these will hopefully show you the real file paths so that you can then adjust the URL to match the actual target location.
如果在尝试重定向到 JSP 页面时遇到 404 错误,请尝试将日志记录级别调高为 ALL 或 DEBUG,以便您可以查看来自框架或 Java 容器的日志,这些有望向您显示真实的文件路径,以便您然后可以调整 URL 以匹配实际目标位置。
回答by user1558223
this should open new tab with GeneralInfo.jsp when you click on the General Info button..
当您单击“常规信息”按钮时,这应该会打开带有 GeneralInfo.jsp 的新选项卡。
function goToInfo(){
window.location = "GeneralInfo.jsp";
}
回答by cmb28
you can use this method,
你可以用这个方法
<input type="button" value="General Info" name="INfoPage"
onclick="document.forms[0].action = 'infoPage.jsp';" />
or the easy way is,
或者简单的方法是,
<a href="InfoPage.jsp">General Info</a>