• 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 ...
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Best way to report errors to the user?  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

    Posts: 20765
    Android Eclipse IDE Java Linux Redhat Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
    Ranch Hand
    Posts: 218
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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,

    brevity is the soul of wit - shakepeare. Tiny ad:
    how do I do my own kindle-like thing - without amazon
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!