如何通过 MVC portlet 中的 JavaScript 重定向到另一个 JSP 页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13006323/
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 redirect to another JSP page via JavaScript in MVC portlet?
提问by User 1531343
I am trying to open my form with filling out every text box on click of edit button on the HTML table where all my data is stored in list and each row has edit button. When I click on edit it will redirect to edit_restaurant.jsp page from restaurant.jsp page.
我试图打开我的表单,点击 HTML 表上的编辑按钮填写每个文本框,我的所有数据都存储在列表中,每行都有编辑按钮。当我单击编辑时,它将从 restaurant.jsp 页面重定向到 edit_restaurant.jsp 页面。
My restuarant.jsp page have one HTML table list in which it has edit button on each row and I want that on click of that edit button I will redirect to edit_restaurant.jsp page where all data can fill up with the id key which is bind to my edit button.
我的 restuarant.jsp 页面有一个 HTML 表格列表,其中每一行都有编辑按钮,我希望单击该编辑按钮时我将重定向到 edit_restaurant.jsp 页面,在该页面中所有数据都可以用绑定的 id 键填充到我的编辑按钮。
What I want is to get the id of button in another jsp button..am using liferay custom mvc portlet so can anyone guide me that how to make it possible.
我想要的是在另一个 jsp 按钮中获取按钮的 id..我正在使用 liferay 自定义 mvc portlet,所以任何人都可以指导我如何使它成为可能。
Here is the snippet of code that I have tried.
这是我尝试过的代码片段。
Following is my column of edit button:
以下是我的编辑按钮栏:
<td><input type="submit" id=<%=temprest.getPrimaryKey()s%> onclick="return getbuttonId('<%=temprest.getPrimaryKey() %>')" value="edit" /></td>
...and on click of that button THE following Javascript will be invoked:
...并单击该按钮将调用以下 Javascript:
<script type="text/javascript">
function getbuttonId(sid){
// document.editform.action=url;
// document.editform.submit();
var url="<%= editURL.toString()%>";
window.location.href = url+"?sid="+sid;
return false ;
}
</script>
My portlet action url is as follows:
我的 portlet 操作 url 如下:
<portlet:actionURL name="editRestaurant" var="editURL">
</portlet:actionURL>
...and the method I am calling via portlet action url is here in my action class:
...我通过 portlet 操作 url 调用的方法在我的操作类中:
public void editRestaurant(ActionRequest reqeust,ActionResponse response) throws Exception
{
String str= reqeust.getParameter("sid");
long l=Long.valueOf(str);
//Retriev table data using Primary key
restaurant rest=restaurantLocalServiceUtil.getrestaurant(l);
//Set Attribute which has all values of specified pk.
reqeust.setAttribute("edit",rest );
// Redirect to Jsp page which has Update form.
response.setRenderParameter("jspPage","/jsps/edit_restaurant.jsp");
}
I got error like this when I click on edit function
单击编辑功能时出现这样的错误
java.lang.NoSuchMethodException: emenu.advertise.portlet.RestaurantPortlet.editRestaurant?sid=5(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
Can somebody please guide me to fix this?
有人可以指导我解决这个问题吗?
@
@
here i want that the sid value assigned to my button id should be transfer to this string value before page is redirects
在这里我希望分配给我的按钮 id 的 sid 值应该在页面重定向之前转移到这个字符串值
<%
String restId=request.getParameter("sid");
//can i call sid value of javascript function getbuttonId() here
%>
<portlet:actionURL name="editRestaurant" var="editURL">
<portlet:param name="key" value="<%=restId %>"/>
</portlet:actionURL>
采纳答案by User 1531343
Anyways had solved my problem with the following code:
无论如何已经用以下代码解决了我的问题:
function getbuttonId(sid){
函数 getbuttonId(sid){
alert("Are You Sure You want to Edit");
// document.editform.action=url;
// document.editform.submit();
var textbox = document.getElementById('hiddenkey');
textbox.value=sid;
document.editform.action = "<%=editURL.toString() %>";
document.editform.submit();
return false ;
}
}
and onclick of button I am calling the above javascript method. So it works good.
和 onclick 按钮我正在调用上面的 javascript 方法。所以效果很好。
Actually what I want was the id of particular button which is clicked so I am storing that id in hidden key and after form submit action in java script I am redirecting to page I want and then I can get the value of hidden key field from the request.getattribute(hiddenkey field name).
实际上我想要的是被点击的特定按钮的 id,所以我将该 id 存储在隐藏键中,在 java 脚本中的表单提交操作之后,我重定向到我想要的页面,然后我可以从隐藏键字段中获取值request.getattribute(隐藏键字段名称)。
回答by brightboy2004
Please read this:
请阅读这个:
How to redirect to another webpage in JavaScript/jQuery?
如何在 JavaScript/jQuery 中重定向到另一个网页?
Query is not necessary, and window.location.replace(...) will best simulate an HTTP redirect.
查询不是必需的,并且 window.location.replace(...) 将最好地模拟 HTTP 重定向。
It is better than using window.location.href =, because replace() does not put the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco. If you want to simulate someone clicking on a link, use location.href. If you want to simulate an HTTP redirect, use location.replace.
它比使用 window.location.href = 更好,因为 replace() 不会将原始页面放入会话历史记录中,这意味着用户不会陷入永无止境的后退按钮惨败中。如果您想模拟某人单击链接,请使用 location.href。如果要模拟 HTTP 重定向,请使用 location.replace。
For example:
例如:
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";