First, here is the code:
首先,这是代码:
<form action="FirstServlet" method="Post">
Last Name: <input type="text" name="lastName" size="20">
<br><br>
<input type="submit" value="FirstServlet">
<input type="submit"value="SecondServlet">
</form>
I'd like to understand how to send information in case the FirstServlet button
was pressed to FirstServlet
and in case the SecondServlet button
was pressed to SecondServlet
.
我想了解如何在FirstServlet按钮被按下FirstServlet的情况下发送信息,以及如果将SecondServlet按钮按下到SecondServlet。
important:
I want to do it in the same form so the same information would be transered to both the servlets. (of course, in the servlets I'll use the info accordingly)
重要的是:我想以相同的形式进行,因此相同的信息将被转换为两个servlet。 (当然,在servlet中我将相应地使用信息)
5 个解决方案
#1
84
There are several ways to achieve this.
有几种方法可以实现这一目标。
Probably the easiest would be to use JavaScript to change the form's action.
可能最简单的方法是使用JavaScript来改变表单的动作。
<input type="submit" value="SecondServlet" onclick="form.action='SecondServlet';">
But this of course won't work when the enduser has JS disabled (mobile browsers, screenreaders, etc).
但是当最终用户禁用JS(移动浏览器,屏幕阅读器等)时,这当然不起作用。
Another way is to put the second button in a different form, which may or may not be what you need, depending on the concrete functional requirement, which is not clear from the question at all.
另一种方法是将第二个按钮放在不同的形式,根据具体的功能要求,这可能是你需要的,也可能不是你需要的,这根本不是问题所在。
<form action="FirstServlet" method="Post">
Last Name: <input type="text" name="lastName" size="20">
<br><br>
<input type="submit" value="FirstServlet">
</form>
<form action="SecondServlet" method="Post">
<input type="submit"value="SecondServlet">
</form>
Note that a form would on submit only send the input data contained in the very same form, not in the other form.
请注意,表单将在提交时仅发送包含在同一表单中的输入数据,而不是以其他形式发送。
Again another way is to just create another single entry point servlet which delegates further to the right servlets (or preferably, the right business actions) depending on the button pressed (which is by itself available as a request parameter by its name
):
另一种方法是创建另一个单一入口点servlet,它根据按下的按钮进一步委托给正确的servlet(或者最好是正确的业务操作)(它本身可以通过其名称作为请求参数):
<form action="MainServlet" method="Post">
Last Name: <input type="text" name="lastName" size="20">
<br><br>
<input type="submit" name="action" value="FirstServlet">
<input type="submit" name="action" value="SecondServlet">
</form>
with the following in MainServlet
在MainServlet中具有以下内容
String action = request.getParameter("action");
if ("FirstServlet".equals(action)) {
// Invoke FirstServlet's job here.
} else if ("SecondServlet".equals(action)) {
// Invoke SecondServlet's job here.
}
This is only not very i18n/maintenance friendly. What if you need to show buttons in a different language or change the button values while forgetting to take the servlet code into account?
这只是i18n /维护友好。如果您需要在忘记考虑servlet代码的同时显示不同语言的按钮或更改按钮值,该怎么办?
A slight change is to give the buttons its own fixed and unique name, so that its presence as request parameter could be checked instead of its value which would be sensitive to i18n/maintenance:
稍微改变是为按钮提供固定且唯一的名称,以便可以检查其作为请求参数的存在而不是其对i18n / maintenance敏感的值:
<form action="MainServlet" method="Post">
Last Name: <input type="text" name="lastName" size="20">
<br><br>
<input type="submit" name="first" value="FirstServlet">
<input type="submit" name="second" value="SecondServlet">
</form>
with the following in MainServlet
在MainServlet中具有以下内容
if (request.getParameter("first") != null) {
// Invoke FirstServlet's job here.
} else if (request.getParameter("second") != null) {
// Invoke SecondServlet's job here.
}
Last way would be to just use a MVC framework like JSF so that you can directly bind javabean methods to buttons, but that would require drastic changes to your existing code.
最后一种方法是使用像JSF这样的MVC框架,以便您可以直接将javabean方法绑定到按钮,但这需要对现有代码进行大幅更改。
<h:form>
Last Name: <h:inputText value="#{bean.lastName}" size="20" />
<br/><br/>
<h:commandButton value="First" action="#{bean.first}" />
<h:commandButton value="Second" action="#{bean.Second}" />
</h:form>
with just the following javabean instead of a servlet
只使用以下javabean而不是servlet
@ManagedBean
@RequestScoped
public class Bean {
private String lastName; // +getter+setter
public void first() {
// Invoke original FirstServlet's job here.
}
public void second() {
// Invoke original SecondServlet's job here.
}
}
#2
4
In addition to the previous response, the best option to submit a form with different buttons without language problems is actually using a button tag.
除了之前的响应之外,提交具有不同语言问题的不同按钮的表单的最佳选择实际上是使用按钮标记。
<form>
...
<button type="submit" name="submit" value="servlet1">Go to 1st Servlet</button>
<button type="submit" name="submit" value="servlet2">Go to 2nd Servlet</button>
</form>
#3
3
If you use jQuery, u can do it like this:
如果你使用jQuery,你可以这样做:
<form action="example" method="post" id="loginform">
...
<input id="btnin" type="button" value="login"/>
<input id="btnreg" type="button" value="regist"/>
</form>
And js will be:
并且js将是:
$("#btnin").click(function(){
$("#loginform").attr("action", "user_login");
$("#loginform").submit();
}
$("#btnreg").click(function(){
$("#loginform").attr("action", "user_regist");
$("#loginform").submit();
}
#4
0
You may need to write a javascript for each button submit. Instead of defining action in form definition, set those values in javascript. Something like below.
您可能需要为每个按钮提交编写一个javascript。不是在表单定义中定义操作,而是在javascript中设置这些值。像下面的东西。
function callButton1(form, yourServ)
{
form.action = yourServ;
form.submit();
});
#5
0
If you use spring this a better way
如果你使用弹簧这是一个更好的方法
Is there any way to create form with multiple submit buttons on Spring MVC using annotations?
有没有办法使用注释在Spring MVC上创建具有多个提交按钮的表单?
#1
84
There are several ways to achieve this.
有几种方法可以实现这一目标。
Probably the easiest would be to use JavaScript to change the form's action.
可能最简单的方法是使用JavaScript来改变表单的动作。
<input type="submit" value="SecondServlet" onclick="form.action='SecondServlet';">
But this of course won't work when the enduser has JS disabled (mobile browsers, screenreaders, etc).
但是当最终用户禁用JS(移动浏览器,屏幕阅读器等)时,这当然不起作用。
Another way is to put the second button in a different form, which may or may not be what you need, depending on the concrete functional requirement, which is not clear from the question at all.
另一种方法是将第二个按钮放在不同的形式,根据具体的功能要求,这可能是你需要的,也可能不是你需要的,这根本不是问题所在。
<form action="FirstServlet" method="Post">
Last Name: <input type="text" name="lastName" size="20">
<br><br>
<input type="submit" value="FirstServlet">
</form>
<form action="SecondServlet" method="Post">
<input type="submit"value="SecondServlet">
</form>
Note that a form would on submit only send the input data contained in the very same form, not in the other form.
请注意,表单将在提交时仅发送包含在同一表单中的输入数据,而不是以其他形式发送。
Again another way is to just create another single entry point servlet which delegates further to the right servlets (or preferably, the right business actions) depending on the button pressed (which is by itself available as a request parameter by its name
):
另一种方法是创建另一个单一入口点servlet,它根据按下的按钮进一步委托给正确的servlet(或者最好是正确的业务操作)(它本身可以通过其名称作为请求参数):
<form action="MainServlet" method="Post">
Last Name: <input type="text" name="lastName" size="20">
<br><br>
<input type="submit" name="action" value="FirstServlet">
<input type="submit" name="action" value="SecondServlet">
</form>
with the following in MainServlet
在MainServlet中具有以下内容
String action = request.getParameter("action");
if ("FirstServlet".equals(action)) {
// Invoke FirstServlet's job here.
} else if ("SecondServlet".equals(action)) {
// Invoke SecondServlet's job here.
}
This is only not very i18n/maintenance friendly. What if you need to show buttons in a different language or change the button values while forgetting to take the servlet code into account?
这只是i18n /维护友好。如果您需要在忘记考虑servlet代码的同时显示不同语言的按钮或更改按钮值,该怎么办?
A slight change is to give the buttons its own fixed and unique name, so that its presence as request parameter could be checked instead of its value which would be sensitive to i18n/maintenance:
稍微改变是为按钮提供固定且唯一的名称,以便可以检查其作为请求参数的存在而不是其对i18n / maintenance敏感的值:
<form action="MainServlet" method="Post">
Last Name: <input type="text" name="lastName" size="20">
<br><br>
<input type="submit" name="first" value="FirstServlet">
<input type="submit" name="second" value="SecondServlet">
</form>
with the following in MainServlet
在MainServlet中具有以下内容
if (request.getParameter("first") != null) {
// Invoke FirstServlet's job here.
} else if (request.getParameter("second") != null) {
// Invoke SecondServlet's job here.
}
Last way would be to just use a MVC framework like JSF so that you can directly bind javabean methods to buttons, but that would require drastic changes to your existing code.
最后一种方法是使用像JSF这样的MVC框架,以便您可以直接将javabean方法绑定到按钮,但这需要对现有代码进行大幅更改。
<h:form>
Last Name: <h:inputText value="#{bean.lastName}" size="20" />
<br/><br/>
<h:commandButton value="First" action="#{bean.first}" />
<h:commandButton value="Second" action="#{bean.Second}" />
</h:form>
with just the following javabean instead of a servlet
只使用以下javabean而不是servlet
@ManagedBean
@RequestScoped
public class Bean {
private String lastName; // +getter+setter
public void first() {
// Invoke original FirstServlet's job here.
}
public void second() {
// Invoke original SecondServlet's job here.
}
}
#2
4
In addition to the previous response, the best option to submit a form with different buttons without language problems is actually using a button tag.
除了之前的响应之外,提交具有不同语言问题的不同按钮的表单的最佳选择实际上是使用按钮标记。
<form>
...
<button type="submit" name="submit" value="servlet1">Go to 1st Servlet</button>
<button type="submit" name="submit" value="servlet2">Go to 2nd Servlet</button>
</form>
#3
3
If you use jQuery, u can do it like this:
如果你使用jQuery,你可以这样做:
<form action="example" method="post" id="loginform">
...
<input id="btnin" type="button" value="login"/>
<input id="btnreg" type="button" value="regist"/>
</form>
And js will be:
并且js将是:
$("#btnin").click(function(){
$("#loginform").attr("action", "user_login");
$("#loginform").submit();
}
$("#btnreg").click(function(){
$("#loginform").attr("action", "user_regist");
$("#loginform").submit();
}
#4
0
You may need to write a javascript for each button submit. Instead of defining action in form definition, set those values in javascript. Something like below.
您可能需要为每个按钮提交编写一个javascript。不是在表单定义中定义操作,而是在javascript中设置这些值。像下面的东西。
function callButton1(form, yourServ)
{
form.action = yourServ;
form.submit();
});
#5
0
If you use spring this a better way
如果你使用弹簧这是一个更好的方法
Is there any way to create form with multiple submit buttons on Spring MVC using annotations?
有没有办法使用注释在Spring MVC上创建具有多个提交按钮的表单?