Java 提交表单后重定向到另一个jsp页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16414999/
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
Redirect to another jsp page after submitting a form
提问by JohnRambo
I have 2 jsp pages "/page1.jsp","/page2.jsp" and "file.java", after submitting a form from "/page1" i want to go to action method take some records from the database and go to the "/page2" with the records i have and list it. All is done but for some reason i cannot go to the "/page2" page it takes me somewhere else(another .jsp page).
我有 2 个 jsp 页面“/page1.jsp”、“/page2.jsp”和“file.java”,从“/page1”提交表单后,我想转到操作方法,从数据库中获取一些记录并转到“/page2”与我拥有的记录并列出。一切都完成了,但由于某种原因,我无法进入“/page2”页面,它会将我带到其他地方(另一个 .jsp 页面)。
I am using liferay and extending MVCPortlet class
我正在使用 liferay 并扩展 MVCPortlet 类
Thanks in advance!!!.
提前致谢!!!。
public void AddCustomer(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {
...
pCustomer.setCustomerId(customerId);
// set UI fields
pCustomer.setName(cusName);
pCustomer.setAddress(address);
pCustomer.setComments(comments);
try {
PCustomerLocalServiceUtil.addPCustomer(pCustomer);
} catch (com.liferay.portal.kernel.exception.SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
...
}
addCustomer.jsp:
添加Customer.jsp:
<portlet:actionURL var="addCustomersAct" name="AddCustomer">
<portlet:param name="jspPage" value="/allCustomers.jsp"/>
</portlet:actionURL>
<form method="post" action="<%= addCustomersAct %>">
...
</form>
allCustomers.jsp
所有客户.jsp
<%for (PCustomer allCustomer : customers) { %>
<tr>
...//List of all customers
</tr>
<% } %>
On top of the addCustomers.jsp i have some more portlet parameters because I have a sidemenu and i need them for the render requests.
在 addCustomers.jsp 之上,我有更多的 portlet 参数,因为我有一个侧菜单,我需要它们来处理呈现请求。
采纳答案by Rohan
After catchblock you can add following code statement.
在catch块之后,您可以添加以下代码语句。
actionResponse.sendRedirect("page2.jsp");
回答by M Sach
Well would like to see more code snippet.Here it is to forward the request to another jsp
好吧,想看更多的代码片段。这里是将请求转发到另一个jsp
String destination = "page2.jsp"; // using relative path here
RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
rd.forward(request, response);
make sure you are using right path for destination jsp
确保您为目标jsp使用正确的路径
回答by karthik
In place of mul change the object according to your requirement whatever value you want which is redirected to jsp
actionRequest.setAttribute("mul",mul);
actionResponse.setRenderParameter("jspPage", "/jsp/b.jsp");
In jsp:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<h6>Mul</h6>
<%= renderRequest.getAttribute("mul") %>
回答by chandan sharma
In your addCustomer.jsp create one renderURL and provide this as hidden parameter as redirect url
在您的 addCustomer.jsp 中创建一个 renderURL 并将其作为隐藏参数提供为重定向 url
...
<portlet:renderURL var="redirectURL">
<portlet:param name="jspPage" value="/html/yourhtmlpath/confirm.jsp"/>
</portlet:renderURL>
...
and
和
<form>
...
<aui:input name="redirectURL" type="hidden" value="${redirectURL}"></aui:input>
...
</form>
In Your processAction method after doing your task:
在您完成任务后的 processAction 方法中:
...
actionResponse.sendRedirect(ParamUtil.getString(actionRequest, "redirectURL"));
...