• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

h:commandButton not working

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I'm new to jsf2 and just can get the hang of wat I'm doing wrong

I have created a simple application containing jsf2 and facelets. Every time I click the
commandButton I would expect to go into my UserBean method username. I have debug
set within username and it never goes into it


It's not rocket science but not sure wats going on

file: administration.xhtml


file UserBean.java


Can anybody help

Mat
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<h:form/>?
 
Saloon Keeper
Posts: 24860
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSF is an HTTP-based framework, not a client/server framework. What that means is that clicking a button doesn't "call a method", it initiates an HTTP Request/Response process in accordance with the JSF lifecycle architecture.

In order for a commandButton or commandLink to actually result in the execution of a backing bean method, a number of pre-conditions have to be met.

First of all, since this is HTTP, the request from the client must be encapsulated within an HTTP form. The JSF form element will generate that, but you must specify it - there's no default.

Actually first of all, in JSF, the whole thing has to be in a JSF view element, but you did that. The form (or forms, for pages containing multiple forms) must be wholly contained within that view.

Having a button in a form guarantees that when you click the button, an HTTP request will be sent to the server. But that's only the start of it.

For HTTP requests that match the FacesServlet URL pattern in web.xml, the Request is then processed by the FacesServlet, which runs through a multi-phase lifecycle. The first step in that lifecycle is to validate ALL inputs coming from that form. If even one input fails validation, the lifecycle short-circuits and returns an error without calling the button's action method.

If the inputs are all valid, they are then used to update their corresponding properties on the backing bean. Thus, before the action method is invoked, the bean has been updated to reflect the latest (valid) values of the input controls on the form.

After the bean is updated, THEN the action method (or actionlisteners) is invoked. Note: people use listeners too much. I think this is because old stale documentation on JSF is still floating around. Listeners have their uses, but there are simpler, cleaner mechanisms that should be used in most cases.

If you code "immediate="true"" on a commandButton or commandLink, the form data will not be submitted, validated, or updated and the action method will be fired directly. This is useful for things like a "Cancel" button where you don't care what people did to the form's input controls, but it's not selective. If you use it, all you'll get is the action, not any data.
 
Mat Anthony
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,
that was an absolutely fantastic description of how things work. The web is full of solutions
many don’t work and some with difficulty descriptions to understand.

Thanks for all your help

Mat
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic