• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How can i can automatically pass a value from Primefaces to the bean when clicking a button

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have a datatable and i would like to have a delete option on every row. This is what i`ve tried but it doesnt work:
XHTML -->

JAVA-->


The button works, it calls the function (if i set the id that i want to delete it works) but if the id is taken with <f:param it doesnt work....please help

I would like when pressing delete button the delete the row that is located on...thanks
 
Bartender
Posts: 20842
125
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're doing it the hard way. Also, don't code function calls on JSF EL expressions. They're supposed to be references, not logic.
First, change this:
To this

As I said, this is supposed to be a reference. JSF knows how to do the method call. And it's what's supposed to be doing the calling, not you.

Now let's look at your table. You're trying to do brute-force operations on the rows.

Again, let's change the xhtml to the proper form:

Becomes this:

And, incidentally, it's not a bad idea to use a styleClass instead of style so that your tables can be consistent without having to edit every page. If you're really ambitious, you can even "skin" Primefaces, but that's too complex to cover here.

Notice that I changed the property name to returnListModel. That's because instead of using a raw List as the table's data model, we're going to wrap it in a JSF DataModel. Here's one way to do that:


Incidentally, the bean containing this method and object must not be Request scoped. Request scope is almost useless in JSF.

The DataModel is a class that adds cursor capabilities to the return list. That makes life much easier for the action method:


Wasn't that easy? No need to muck around in the FacesContext or anything. It all gets practically handed to you. You don't even have to update the ListModel, since it's still the same returnList, just modified.

And the dataTable xhtml gets simpler, too, since the DataModel already knows which row is which and doesn't need any parameters:



One other thing. Your database code would be bad in any Java webapp, not just J2EE. Use a Connection Pool, don't use DriverManager. If you're going to use a system as powerful as JSF, you shouldn't use inefficient database connections.

And DEFINITELY learn to do PreparedStatements. You've set yourself up for a SQL injection attack.
 
Serban Cameron
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your detailed reply, i am a junior developer started working for two months and we use jsf at work
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!