File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes commandButton is fired multiple times. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "commandButton is fired multiple times." Watch "commandButton is fired multiple times." New topic
Author

commandButton is fired multiple times.

Amlan Karmakar
Greenhorn

Joined: Sep 20, 2012
Posts: 10
I am trying to comment on status that is retrieved from the database. Whenever I am submitting the comment for specific status I am getting the exception.I have used <ui:repeat> to repeat the commandButtons. I don't know if the commandButton can be put inside <ui:repeat> or not. It would be really helpful if somebody could point out my mistake.Actually what is happening is that whenever I am trying to insert data into the database using '<h:inputText>' other commandButtons created due to using '<ui:repeat>' is possibly getting fired. I have inputText and commandButton inside the ui:repeat tag. So, some of inputText are supposed to be empty because I am selecting one inputText and clicking the corresponding commandButton

1) Here is my xhtml



2)Here is statusBean




3)Here is the comment entity class




4) Here is stacktrace that I am getting

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15952
    
  19

Welcome to the JavaRanch, Amlan!

Your most immediate problem isn't JSF. You're violating the database constraint that the "comment" column cannot have a null value when you persist it out.

As far as ui:repeat goes, yes, commandButtons can be used in ui:repeats, but I strongly recommend that when you construct 2-dimensional (table-like) objects on a JSF view that you use a dataTable, not ui:repeat. Usually when you have a table full of rows with action buttons (and/or commandLinks), the entire table is encased within a single form, however, not form-per-row.


Customer surveys are for companies who didn't pay proper attention to begin with.
Amlan Karmakar
Greenhorn

Joined: Sep 20, 2012
Posts: 10
I am having the exact same problem when I am using dataTable instead of ui:repeat. The problem is that multiple commandLink are possibly getting fired. If I am using dataTable ,suppose there is an inputText and a commandLink inside the datatable and it is repeated four times. So whenever I click the first commandLink associated with the inputText , I get exception four times,comment cannot be null. So I am assuming the commandLink is fired four times. But if I click on the fourth commandLink everything is working fine, the comment field is getting inserted in the database.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15952
    
  19

That's not how to use a commandButton.

The proper form is:


The "$" EL designator is for data access, not data reference. You'll almost never use it in JSF, since the "#" referencing designator is a superset of that function.

The update() method should not take a parameter. Instead, have the update() method invoke the DataModel object's "getCurrentRow()" method to obtain the model row, which can then be queried to get its statusId value (or, for that matter, any other value on the table row).

The more "program-like" your View Definition is, the more likely you're not doing it JSF-style. That means that you not only end up fighting JSF instead of being helped by it, but also you're violating the Model/View/Controller contract. Which means that people cannot tell automatically where things are done, they have to "treasure hunt" through multiple resources. Plus, the tighter coupling between Model and View makes for less flexible, less maintainable, less reusable components.
Amlan Karmakar
Greenhorn

Joined: Sep 20, 2012
Posts: 10
Could you please give me an example on how to use the DataModel for obtaining the statusId, Actually I am new to JSF so I don't how the dataModel works
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15952
    
  19

DataModel is a wrapper class for a collection of items representing model rows in a dataTable. It comes in several flavors, such as ArrayDataModel, ListDataModel, and so forth.

A lot of people back a dataTable with a raw array or collection, but that's a convenience feature of limited utility. And, as far as it goes, I believe that current JSF implementations will simply construct an anonymous DataModel and wrap the raw data in it.

The reason for using the DataModel instead of a raw collection is that the DataModel maintains the context needed for the JSF UI to track rows and thus allow the underlying row collection model to be ui-independent. When you fire an action control on a row in a dataTable, the DataModel knows which row it was without you having to explicitly tell it. DataModels, BTW do not work on request-scope objects, because they track across multiple request/response cycles.

Using a DataModel is very easy. Simply construct a DataModel of your choosing, based on the wrapped data. For example, an ArrayList object would be wrapped by a ListDataModel. You can initialize like so:


Or, you can construct and wrap separately:


You then present the dataModel as the backing property for the dataTable object. The actual "dataList" object doesn't need its own set/get accessors, since only the wrapped data is actually referenced in the JSF View.

An action method can determine which row was selected when the action was fired using either the "getRowData()" or "getRowNumber()" DataModel methods. Actually, the "getRowData()" method is just a shorthand for "dataModel.getWrappedData().get(dataModel.getRowNumber())".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: commandButton is fired multiple times.
 
Similar Threads
Eclipselink problem with MySQL Stored Procedures
Image insertion into database through JPA
How to delete all records in a JPA table?
call a stored procedure using eclipselink Problem...
Storing images to database through JDBC