File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes How to call a backingbean method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to call a backingbean method?" Watch "How to call a backingbean method?" New topic

How to call a backingbean method?

D. Formenton
Ranch Hand

Joined: Mar 20, 2008
Posts: 48
Hi, Guru,

I have a problem with this code:

<h:commandButton id="hiddenBtn" style="display: none"
onclick="#{personeController.delete()}" />

<script type="text/javascript">
function conferma(){
var b = confirm('Sei sicuro di voler eliminare la Persona?');
if(b == true){

I woul like to call the backing bean method personeController.delete() ONLY when the aswer to confirm dialog is "YES".
Instead the method is called also when the answer is "CANCEL" (and, obviously, b = false).

What is my mistake?

Thank you.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

Hint: use the "Code" button to format your Java code for readability.

You don't "call" a backing bean method. JSF Views are not supposed to contain logic, so the idea of a View "calling" anything is meaningless. However, you can set up a JSF action method to be invoked when you click on a commandButton. Yes, I know. Sounds pedantic, but the fundamental differences are considerable, which is why I pick the nits.

Speaking of nits, backing beans are not Controllers, they are Models. In JSF, most of the Controllers are pre-coded and supplied to you, not user-written.

Note that I've altered "conferma" to return a boolean value. The onclick attribute will suppress the action if its value is false. Also, conferma has parentheses since it defines a JavaScript function call (instead of something like a JavaScript boolean variable). However, the action does not, since the call is implied.

An IDE is no substitute for an Intelligent Developer.
D. Formenton
Ranch Hand

Joined: Mar 20, 2008
Posts: 48
Dear Tim,

thank you for your help, I tried to implement your suggestions but unfortunately without results.
Based on your tips I modified the code as follows:

<p:dataTable value="#{personeController.getAll()}" var="item"
paginator="true" rows="10" rowKey="#{}"
id="personeTable" style="border:solid 1px">

<h:commandButton image="/resources/icons16x16/delete.gif" id="delete"
type="button" title="#{misc.find_delete}"
onclick="confirm('Sei sicuro di voler eliminare la Persona?');"/>



If I properly understood, the call to confirm passes to 'onclick' a value (true or false), and this value activates or inhibits the start of 'action'.

The result is that 'action' is triggered whatever response is received from confirm (yes or cancel).

I want point out that the button is part of a table and the action is directed to delete a row of the table.

I thak you in advance for your further availaibilty and your courtesy.



Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

You're still trying to treat the View like it is executable code. Don't do that!

Actions are not coded with "()" and especially they should not be coded with parameters. An Action method originally was always supposed to be a method which takes no parameters and returns a String, although for the benefit of AJAX, JSF2 also allows returning void when page navigation is not required.

The reason that action methods don't have parameters is that the backing bean itself is the parameter. When you want to act on a row in a table, use the table datamodel's getRowData() or getRowNumber() method to determine which row the action control was located in. Not only does this eliminate the need to explicitly locate the row using View-side "logic", it's more secure, since parameters can be snooped over the network, but data in backing beans never leaves the server.

I may have miscoded the confirmation. I think it actually has to be:

Without the "return", the value is set, but not passed back to the onclick controller.
I agree. Here's the link:
subject: How to call a backingbean method?
It's not a secret anymore!