wood burning stoves 2.0*
The moose likes JSP and the fly likes Multiple submit buttons with JavaBeans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Multiple submit buttons with JavaBeans" Watch "Multiple submit buttons with JavaBeans" New topic
Author

Multiple submit buttons with JavaBeans

Tony Phillips
Greenhorn

Joined: Jan 30, 2007
Posts: 14
Hello all, I am pretty new to JSP because I am an ASP.NET developer by day so pardon me if this is an elementary question. How does JSP know which button was clicked when you have more than one button within a form?

I have the following buttons within the same form:

<input type="submit" name="command" value="Change Price">
<input type="submit" name="command" value="Delete">

I was told to add the 'name=command' part and JSP will know which button was submitted. But even when I click the "Change Price" button, the "Delete" button is always submitted?

Currently, the delete works perfectly in my bean. I can't seem to get the update to work right. How do I get my Bean to know which button was submitted?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61458
    
  67

The value of the "command" request parameter will reflect the value of the button that is clicked to submit the form. If this is not what you are seeing in your bean, check the URL of the submission (if using GET) to see if it is correct there, indicating that the bean has a coding issue.

If using POST you'll need to inspect the request body to see the submission parameters (or temporarily switch to GET).
[ February 14, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tony Phillips
Greenhorn

Joined: Jan 30, 2007
Posts: 14
Thank you for your help. I remember the request object, but how does one access the request object from within the bean? Is there a package I need to reference?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Are you using a particular library (Struts, etc..).
Beans aren't automatically connected to a request.
The request information is usually passed from a controller of some kind.

Or...
Are you using jsp:setProperty property="*" ?
[ February 14, 2008: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Tony Phillips
Greenhorn

Joined: Jan 30, 2007
Posts: 14
Actually, it just occured to me I could use a setProperty to set a command variable within my bean. I am not sure if that represent best practices in JSP, but it looks like it would work.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61458
    
  67

It sounds like you are processing the results of your submission in a JSP rather than a servlet. This is not considered modern best practices.

As you are new to JSP, this is a common first step, but not one that you should get entrenched within.

I've written a few articles for the JavaRanch Journal that you might find helpful:

The Secret Life of JSPs

The Front Man

The first is on how JSP works and why, The second on modern Java web application structure.
Tony Phillips
Greenhorn

Joined: Jan 30, 2007
Posts: 14
Ok, I just realize my ideal didn't work. I have a textbox within that form and the method (within JavaBean) getting the value of that textbox is called before any of my submit buttons.

I am not using any Structs, Servlets, or whatever, just using JavaBeans.

I reviewed the sites, but I am still lost on how to get my site to know which button was submitted.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You still haven't told us how the form parameters are getting to your beans.
Tony Phillips
Greenhorn

Joined: Jan 30, 2007
Posts: 14
I am using

<jsp:useBean class="MyBean" id="MyBean" scope="session" />

Then I set my properties like so

<jsp:setProperty name="MyBean" property="price" />

<form>
<input type="hidden" name="id" value="${MyBean.id}" />
<input type="text" name="price" value="${MyBean.price}" />
...........

The two submit buttons are above.

My bean has a method called

Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

As Bear mentioned, responding to requests with JSPs is no longer considered a best practice when developing with server side Java.

That being said...
An unclicked button will send no parameters to the server.
So, if you have two submit buttons in a form, the one that has been clicked will send a parameter, the other won't.

If you're bean has properties for each button, you can determine which was clicked by testing to see which is not null.




Also, if you give several submit buttons the same name but different values, you can determine which was pressed by reading the value sent to the server.
[ February 14, 2008: Message edited by: Ben Souther ]
S Reddy
Ranch Hand

Joined: May 17, 2007
Posts: 45
After assigning request parameters to the bean, Check the value of the request parameter "command" to perform the respective action.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61458
    
  67

I would not suggest using such code. Not only should such code not be in a JSP to begin with, it implicitly assumes that anything that's not a change operation, is a delete. That's not a good practice.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Besides Bear's excellent point...

One reason I don't branch on the value of a button (in the JSP, controller, or model) is that marketing and branding type people seem to love to change the text in buttons.
As soon as you write it with the text "Enter", someone will feel the urge to have it changed to "Submit". A year later, someone else will want it to say "Save".

For this reason, I think it's better to branch on whether the button name is present in the form submission and ignore the actual value.
 
Consider Paul's rocket mass heater.
 
subject: Multiple submit buttons with JavaBeans