My Java file is:
public class MyClass {
public void method1() {
// some code
}
public void method2() {
//some code
}
public void method3() {
//some code
}
}
In my JSP page I have three HTML buttons.
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?
If you have web.xml then
HTML/JSP
web.xml
Java SomeController.java
You can try adding action="#{yourBean.function1}" on each button (changing of course the method function2, function3, or whatever you need). If that does not work, you can try the same with the onclick event.
Anyway, it would be easier to help you if you tell us what kind of buttons are you trying to use, a4j:commandButton or whatever you are using.
Just give the individual button elements a unique name. When pressed, the button's name is available as a request parameter the usual way like as with input elements.
You only need to make sure that the button inputs have
type="submit"
as in<input type="submit">
and<button type="submit">
and nottype="button"
, which only renders a "dead" button purely foronclick
stuff and all.E.g.
with
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.E.g.
with
See also:
on pressing it request will go to servlet on the servlet page check which button is pressed and then accordingly call the needed method as objectName.method