• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

commandButton is fired multiple times.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Amlan Karmakar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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())".
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic