Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to call a backingbean method?

 
D. Formenton
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?');
alert(b);
if(b == true){
document.getElementById('hiddenBtn').click();
};
}
</script>

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.

Domenico
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18094
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
D. Formenton
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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="#{item.id}"
id="personeTable" style="border:solid 1px">
...

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

....

</p:dataTable>

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.

Sincerely

Domenico

 
Tim Holloway
Saloon Keeper
Pie
Posts: 18094
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic