Java 如何根据按下的表单的提交按钮将用户重定向到不同的页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22703079/
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 user to different pages based on the submit button of the form that is pressed
提问by Hyman
First, please note I know the following code is not in JavaScript or jQuery but I just added them as tags of this question in hope of finding a solution for it using Javascript or jQuery.
首先,请注意我知道以下代码不在 JavaScript 或 jQuery 中,但我只是将它们添加为这个问题的标签,希望能使用 Javascript 或 jQuery 找到解决方案。
I have a form with three submit buttons. The problem is that, when I submit the form with either of the buttons the result page will be sent to the same address. I need it to be in a different address.
我有一个带有三个提交按钮的表单。问题是,当我使用任一按钮提交表单时,结果页面将发送到同一地址。我需要它在不同的地址。
Purpose: The code is supposed to show a list of items for a specific category in a page called view. User selects few items and submit the form. Server needs to show the details of the selected items in a page called update (separate address with view page). Then user is able to edit the details of items and submit the form to update the details of those items.
目的:该代码应该在称为视图的页面中显示特定类别的项目列表。用户选择几个项目并提交表单。服务器需要在一个名为更新的页面中显示所选项目的详细信息(与查看页面分开的地址)。然后用户可以编辑项目的详细信息并提交表单以更新这些项目的详细信息。
Requirement
要求
lets say address is
让我们说地址是
myexample.com/Product/view //shows view page
after clicking on edit, it should redirect to following address.
点击编辑后,它应该重定向到以下地址。
myexample.com/Product/update //shows update page ( show details of selected products)
Potential Solution
潜在解决方案
Based on Roman C's answer, after receiving the request, I pass the products list to the other action to show update page, but it shows it as following on the address; therefore, update page does not recognize products parameter as a list.
根据Roman C的回答,收到请求后,我将产品列表传递给另一个动作以显示更新页面,但它在地址上显示如下;因此,更新页面不会将产品参数识别为列表。
...products=[1%2C+2]&id=1
Java
爪哇
List <Integer> products = new ArrayList();
Struts redirect
Struts 重定向
<result name="updateProducts" type="redirectAction">
<param name="actionName">update</param>
<param name="namespace">/products/Update</param>
<param name="products">${products}</param>
<param name="id">${id}</param>
</result>
Even if I manually send the following request, the action class does not recognize products parameter as a list. It shows it's size is Zero.
即使我手动发送以下请求,操作类也不会将 products 参数识别为列表。它显示它的大小为零。
...products=[1,2]&id=1
采纳答案by Roman C
You should add a redirectAction
result to the action config of the action that processes a submit request. In your case it has an edit
name, that is confusing, should name like save
or update
because when you name it edit
many programmers think like you are making a request to populate some textfields. But it's not true, your edit
action doesn't populate, rather than submits to the server. After submit you should follow the Post-Redirect-Get patternand return a redirectAction
or redirect
result to the other action or URL not nessesary to view
or edit
actions if you don't want to stay on the page after redirecting. Remove the edit action config as it has covered by the wildcard mapping. Add the save button
您应该将redirectAction
结果添加到处理提交请求的操作的操作配置中。在您的情况下,它有一个edit
名称,令人困惑,应该命名为 likesave
或者update
因为当您命名它时,edit
许多程序员认为您正在请求填充某些文本字段。但事实并非如此,您的edit
操作不会填充,而不是提交给服务器。提交后,您应该遵循邮政重定向消息获取模式,并返回一个redirectAction
或redirect
导致其他动作或URL不向所必要的view
或edit
行动,如果你不想重定向后,留在页面上。删除编辑操作配置,因为它已被通配符映射覆盖。添加保存按钮
<s:submit id="saveBtn" action="save" value="Save"/>
add and return redirect action result from the save
action
从save
动作添加和返回重定向动作结果
<action name="save" method="save" class="myexample.Product">
<result type="redirectAction" name="list" />
</action>
回答by jmail
How to redirect user to different pages based on the submit button of the form that is pressed:
如何根据按下的表单的提交按钮将用户重定向到不同的页面:
URL redirection is a World Wide Web technique for making a web page available under more than one URL address
. When a web browser attempts to open a URL that already has been redirected, a page with a different URL is opened.
URL 重定向是一种万维网技术,用于使一个网页在多个URL address
. 当 Web 浏览器尝试打开已重定向的 URL 时,会打开具有不同 URL 的页面。
For example, www.example.com is redirected to example.iana.org.
Similarly, Domain redirection
or domain forwarding
is when all pages in a URL domain are redirected to a different domain, as when wikipedia.com
and wikipedia.net
are automatically redirected to wikipedia.org
.
类似地,Domain redirection
或者domain forwarding
是当 URL 域中的所有页面都重定向到不同的域时,就像当wikipedia.com
和wikipedia.net
自动重定向到wikipedia.org
.
URL redirection can be used for URL shortening, to prevent broken links when web pages are moved, to allow multiple domain
names belonging to the same owner to refer to a single web site, to guide navigation into and out of a website, for privacy protection, and for less innocuous purposes such as phishing attacks.
URL 重定向可用于缩短 URL,防止在移动网页时断开链接,允许multiple domain
属于同一所有者的名称引用单个网站,引导进出网站的导航,保护隐私,以及用于不太无害的目的,例如网络钓鱼攻击。
<form action="http://example.org/ jmail1.java">
<input type="submit" value="Go To jmail1.java">
</form>
<form action="http://example.org/jmail2.java">
<input type="submit" value="Go To jmail2.java">
</form>
Techniques:
技巧:
Several different kinds of response to the browser will result in a redirection.
These vary in whether they affect HTTP headers or HTML content. The techniques used typically depend on the role of the person implementing it and their access to different parts of the system.
Several different kinds of response to the browser will result in a redirection.
These vary in whether they affect HTTP headers or HTML content. The techniques used typically depend on the role of the person implementing it and their access to different parts of the system.
For example, a web author with no control over the headers might use a Refresh meta tag whereas a web server administrator redirecting all pages on a site is more likely to use server configuration.
例如,无法控制标题的 Web 作者可能使用 Refresh 元标记,而重定向站点上所有页面的 Web 服务器管理员更有可能使用服务器配置。
<action name="foo">
<result type="redirectAction">
<param name="actionName">bar</param>
<param name="namespace">/</param>
</result>
</action>
Although Info path gives you the ability to create a custom message after submission, lots of users have requested more than that. They haven't been happy with the way the form just leaves you sitting there in the List, it can be confusing. So we have a way in which you can make an InfoPath form function like a traditional web form wherein you fill out the form and upon clicking the ‘Submit' button, the user is taken to a Thank You page
尽管信息路径使您能够在提交后创建自定义消息,但许多用户的要求不止于此。他们对表格让您坐在列表中的方式并不满意,这可能会令人困惑。因此,我们提供了一种方法,您可以使 InfoPath 表单功能类似于传统的 Web 表单,您可以在其中填写表单,然后单击“提交”按钮,用户将进入感谢页面
Resolution:
解析度:
Create a Thank You page and save it somewhere on your site. We recommend using any editor, like Notepad or SharePoint Designer to create an html page with a message of your choosing such as “Thank You for your submission”
Click on that Thank You page and record the URL I.E., it will look something like this: http://mydomain.com/sites/YourSite/YourDocLibrary/ThankYou.html
Now you need to figure out the URL to your InfoPath form. Either click on it and record the URL or if you have attached the InfoPath form to a SharePoint List, then you will need to alter the URL like this: http://mydomain.com/sites/YourSite/YourList/NewForm.aspx, as you can see we just added the NewForm.aspx to the end of it.
Take the URL you recorded in step 3 and add the following:?
Source=http://mydomain.com/sites/YourSite/YourDocLibrary/ThankYou.html, So the URL should look like this: http://mydomain.com/sites/YourSite/YourList/NewForm.aspx?Source=http://mydomain.com/sites/YourSite/YourDocLibrary/ThankYou.html<< this is the link you will send to your users in order to fill out your form. When your users click on this new link, it will tell SharePoint to automatically redirect them to your Thank You page after they submit.On that Thank you page you can add a link to go to another page or you can get real fancy and add a Meta refresh tag to automatically take redirect your users, for example, to the home page of your site. How do you do that? Simple. Add this line:
<meta http-equiv="refresh" content="2;url=http://mydomain.com/sites/yourSite/">
in between the tags of your HTML page. Just to explain a little more about that line, the number 2 in this case represents 2 seconds. So you can modify that to keep the user on that page longer or shorter by replacing that number with a different one. The url would be the url of your home page or any other site you want to redirect the user to.
创建一个感谢页面并将其保存在您网站上的某个位置。我们建议使用任何编辑器(如记事本或 SharePoint Designer)创建带有您选择的消息的 html 页面,例如“感谢您提交”
单击该谢谢页面并记录 URL IE,它将如下所示:http: //mydomain.com/sites/YourSite/YourDocLibrary/ThankYou.html
现在您需要找出您的 InfoPath 表单的 URL。单击它并记录 URL,或者如果您已将 InfoPath 表单附加到 SharePoint 列表,则您需要像这样更改 URL:http://mydomain.com/sites/YourSite/YourList/NewForm.aspx,如您所见,我们刚刚将 NewForm.aspx 添加到它的末尾。
Take the URL you recorded in step 3 and add the following:?
Source= http://mydomain.com/sites/YourSite/YourDocLibrary/ThankYou.html,所以 URL 应该是这样的:http://mydomain.com/sites/YourSite/YourList/NewForm.aspx?Source=http: //mydomain.com/sites/YourSite/YourDocLibrary/ThankYou.html<< 这是您将发送给用户以填写表单的链接。当您的用户单击此新链接时,它会告诉 SharePoint 在他们提交后自动将他们重定向到您的感谢页面。在那个谢谢页面上,您可以添加一个链接到另一个页面,或者您可以真正花哨并添加一个元刷新标签来自动将您的用户重定向到您网站的主页。你是怎样做的?简单的。添加这一行:
<meta http-equiv="refresh" content="2;url=http://mydomain.com/sites/yourSite/">
在 HTML 页面的标签之间。只是为了进一步解释该行,本例中的数字 2 表示 2 秒。因此,您可以修改它,通过用不同的数字替换该数字,使用户在该页面上的时间更长或更短。该 url 将是您的主页或您希望将用户重定向到的任何其他站点的 url。
<table><tr>
<td><s:submit name="update" value="Update" action="update" ></s:submit></td>
<td><s:submit name="delete" value="Delete" action="delete" ></s:submit></td>
</tr></table>
<table><tr>
<td><s:submit name="update" value="Update" action="update" ></s:submit></td>
<td><s:submit name="delete" value="Delete" action="delete" ></s:submit></td>
</tr></table>
<action name="update"
class="net.viralpatel.struts2.action.MemberCrudOpertaions"
method="updateUser">
<result>members.jsp</result>
</action>
<action name="delete"
class="net.viralpatel.struts2.action.MemberCrudOpertaions"
method="deleteUser">
<result>members.jsp</result>
</action>`
Manual redirect:
手动重定向:
The simplest technique is to ask the visitor to follow a link to the new page, usually using an HTML anchor like:
最简单的技术是让访问者点击新页面的链接,通常使用 HTML 锚点,如:
Please follow <a href="http://www.example.com/">this link</a>.
This method is often used as a fall-back — if the browser does not support the automatic redirect, the visitor can still reach the target document by following the link.
这种方法通常用作后备——如果浏览器不支持自动重定向,访问者仍然可以通过点击链接到达目标文档。
Default redirect:
默认重定向:
The Default redirect technique is to ask the visitor to follow a link to the new page, usually using an HTML with JSP like:
默认重定向技术是要求访问者点击新页面的链接,通常使用带有 JSP 的 HTML,例如:
<html>
<%
String redirectURL = "http://stackoverflow.com/";
response.sendRedirect(redirectURL);
%>
</html>
http://struts.apache.org/release/2.2.x/docs/redirect-action-result.html
http://struts.apache.org/release/2.2.x/docs/redirect-action-result.html
http://struts.apache.org/development/2.x/docs/multiple-submit-buttons.html
http://struts.apache.org/development/2.x/docs/multiple-submit-buttons.html
http://struts.apache.org/development/2.x/docs/submit.html
http://struts.apache.org/development/2.x/docs/submit.html
http://struts.apache.org/release/2.2.x/docs/interceptors.html
http://struts.apache.org/release/2.2.x/docs/interceptors.html
related source : http://en.wikipedia.org/
相关来源:http: //en.wikipedia.org/