File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Best way to report errors to the user? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "Best way to report errors to the user?" Watch "Best way to report errors to the user?" New topic
Author

Best way to report errors to the user?

Mike Ma
Greenhorn

Joined: Oct 23, 2010
Posts: 1
Guys and Girls,

In a bit of situation and I am sure some (most) of you will sympathise. A senior developer at work decided to leave weeks before a release and I have been handed an almost complete interface written in JSF using Primefaces. I am relatively new to Java, around 6 months experience and even newer to JSF, around 4 weeks exposure. I come from a C++ background so I know programming, but with a deadline fast approaching I am having issues doing some very simple things. If anyone can give some advice or point me in the right direction it would be greatly appreciated.

Basically the tool I have is an Administrator interface that talks to a JAR that I wrote which in turn connects to SQL Server to perform administration on the database. Most of the development is done there is just a few usability issues I need to implement. What I mainly need to know is the following:

What is the best way to report an error to the user? For example I have a simple text entry form that pops when the user wants to add a record. Some fields are mandatory for the record to be successfully entered, if the user does not correctly enter these fields and then hits apply, it currently fails silently. How can I simply pop up a dialog to display an error or success message? I cannot see how JSF deals with a return value from the backing bean. All I need to do is check if true or false is returned and display a message accordingly. I thought of another way of doing this by disabling the apply button until all mandatory fields are entered, this proved difficult to do... I tried writing some JavaScript to do this (well I say write, find some code online and modify to what I need) but I have practically no experience in JavaScript so this venture didn't really get off the ground. Given a few more weeks I would grab a book and read from cover to cover but I just don't have that luxury at the moment.

If I can get this error reporting working I will fix 90% of the problems I have as everything just fails silently at the moment.

Thanks in advance for any help
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 356
hello,

I can't guarantee to tell you best practices, because I haven't used JSF 2.0 in a real project yet.

About web development in general:
  • You need server-side validation anyway, even if you use JavaScript as a "first check".
  • The most user friendly way to present errors in forms is an error message next to the field where the error occurs.


  • JSF is particularly well suited to make form validation simple. Actually, that's the one big gain when using JSF, while it makes other things much harder than they were with plain Servlets/JSP.
  • Use required="true" in the tags of mandatory input elements: http://download.oracle.com/javaee/6/tutorial/doc/gjcxv.html
  • For additional validation, check out integrated and custom validators in the tutorial
  • Use h:message (error for one particular field) or h:messages (global) to display validation errors and similar problems for fields: http://download.oracle.com/javaee/6/tutorial/doc/bnarf.html#bnaso


  • Your chances of making the application really secure and with not too many bugs aren't that good, given the background you stated. But it'll work.

    Kai


    Kai Witte's business website Kai Witte's private homepage
    Tim Holloway
    Saloon Keeper

    Joined: Jun 25, 2001
    Posts: 16101
        
      21

    It's a trivial matter in JSF to ensure that a form isn't submitted with blank items in it. Just put the 'required="true"' attribute on the controls where mandatory input is required. You can use the 'h:messsages for=' element to display the message, and either use the generic built-in message, or supply a field-specific message.

    "required=" works on most controls. SelectionOneMenuItem checks to make sure that the value of the selection isn't null/empty string. Boolean true/false checkboxes don't support it because there's an implied entry of a value (if you don't check it, it's false). Radio buttons can't be missing a required value as long as one of the buttons in the group was selected before the view was displayed. In generally, it does what most people would like it do do.

    A bigger issue that most people have is when an item is only required when certain other elements on the form are populated. That's one of our more popular questions.

    JSF is designed to avoid passing incomplete or corrupt data to the action processor (and from there to the backend), so it provides quite a bit of support for "up-front" validation and reporting validation failures.


    Customer surveys are for companies who didn't pay proper attention to begin with.
    Brendan Healey
    Ranch Hand

    Joined: May 12, 2009
    Posts: 218

    The best way of validating form input is to enclose the inputText statements in a panelGrid.
    Allow one paneGrid per component to allow for components that render in more than one
    column like a selectBooleanCheckbox.

    Allow columns=3, for example outputLabel = 1 + inputText = 1 + h:message = 1
    == 3. The h:message component would need to use for=id to connect to the component
    in question. For messages that are not connected to a single component the h:messages
    (note the plural) will display potentially multiple messages, but you need to manage the
    space on the page.

    Sorry if I've missed your moment in need, good luck,

    Regards,
    Brendan.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Best way to report errors to the user?