我如何调用特定的Java方法上点击/ JSP中提交特定按钮的事件? [英] How do I call a specific Java method on click/submit event of specific button in JSP?
问题描述
我的Java文件是:
public class MyClass {
public void method1() {
// some code
}
public void method2() {
//some code
}
public void method3() {
//some code
}
}
在我的JSP页面中我有三个HTML按钮。
In my JSP page I have three HTML buttons.
如果我点击按钮1
,那么只有方法1
将被调用,如果我点击按钮2
则仅方法2
将执行,如果按钮3
,那么只有 method3
,等等。
If I click on button1
, then only method1
will be called, if I click on button2
then only method2
will execute, and if button3
, then only method3
, and so on.
我怎样才能做到这一点?
How can I achieve this?
推荐答案
只要给各个按钮元素的唯一名称。当pressed,按钮的名称可以作为一个请求参数一样,与输入元素通常的方式。
Just give the individual button elements an unique name. When pressed, the button's name is available as a request parameter the usual way like as with input elements.
您只需要确保该按钮输入有键入=提交
在<输入类型=提交>
和<按钮式=提交>
,从而不可以 TYPE =按钮
只呈现一个死按钮,纯粹是为了的onclick
的东西,所有的。
You only need to make sure that the button inputs have type="submit"
as in <input type="submit">
and <button type="submit">
and thus not type="button"
which only renders a "dead" button purely for onclick
stuff and all.
例如
<form action="${pageContext.request.contextPath}/myservlet" method="post">
<input type="submit" name="button1" value="Button 1" />
<input type="submit" name="button2" value="Button 2" />
<input type="submit" name="button3" value="Button 3" />
</form>
与
@WebServlet("/myservlet")
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
MyClass myClass = new MyClass();
if (request.getParameter("button1") != null) {
myClass.method1();
} else if (request.getParameter("button2") != null) {
myClass.method2();
} else if (request.getParameter("button3") != null) {
myClass.method3();
} else {
// ???
}
request.getRequestDispatcher("/WEB-INF/some-result.jsp").forward(request, response);
}
}
另外,使用&LT;按钮式=提交&GT;
而不是&LT;输入类型=提交&GT;
,那么你可以给他们所有相同的名称,而是一种独特的价值。的值&LT;按钮方式&gt;
将不能作为标签,你可以指定自己为孩子
Alternatively, use <button type="submit">
instead of <input type="submit">
, then you can give them all the same name, but an unique value. The value of the <button>
won't be used as label, you can just specify that yourself as child.
例如
<form action="${pageContext.request.contextPath}/myservlet" method="post">
<button type="submit" name="button" value="button1">Button 1</button>
<button type="submit" name="button" value="button1">Button 2</button>
<button type="submit" name="button" value="button1">Button 3</button>
</form>
与
@WebServlet("/myservlet")
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
MyClass myClass = new MyClass();
String button = request.getParameter("button");
if ("button1".equals(button)) {
myClass.method1();
} else if ("button2".equals(button)) {
myClass.method2();
} else if ("button3".equals(button)) {
myClass.method3();
} else {
// ???
}
request.getRequestDispatcher("/WEB-INF/some-result.jsp").forward(request, response);
}
}
参见:
- Servlet回报&QUOT; HTTP状态404所请求的资源(/ servlet的)不可用&QUOT;
- How我通过点击JSP页面的超链接或按钮通过当前项目的Java方法?
- Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
- How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
See also:
这篇关于我如何调用特定的Java方法上点击/ JSP中提交特定按钮的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!