What does <h:messages> do in JSF? - jsf

I am learning JSF and came across this line:
<h:messages layout="table"></h:messages>
in a sample application ?
Am not sure what does this line do? I get no error when removing the line from the code, and am able to run it and get the same output ?

The h:messages tag renders all messages for the current JSF view which are not covered by a h:message (remark the missing 's' at the end) tag. Messages can be generated explicitly by your backing beans (FacesContext.addMessage) or implicitly by JSF.
E.g. if you have marked an input value as required and the user submits the form without filling in the required value, an error message will be added to the view. If a h:message tag is bound to the relevant component, the message will be rendered there, otherwise it will be rendered by the global h:messages tag in your view (if any).
The layout attribute specifies what the HTML code to be generated should look like. The table layout (used in your example) uses an HTML table to display messages, while the list layout uses a bulleted list (HTML ul tag).
If you do not specify a h:messages tag in your view and also no h:message tags, the user will not be informed about errors. Therefore, it is best practice to include a h:message tag for each input component of your view and a h:messages tag for your whole view to ensure that all messages are visible to the user.
You will find a compact JSF tag reference at JSF Toolbox.

The components <h:message> and <h:messages> are dedicated to display messages to users (generally error message).
For example, when you have a validation on a field that failed (for example the user didn't fill a required field, or inputed a string in a number-only field), then a FacesMessage is added to the FacesContext object. The <h:message> and <h:messages> are then used to display the message in the page.
The component <h:messages> will display all the messages contained in the FacesContext, while the <h:message> is dedicated to a specific clientId (a specific field). The latter is usefull when you want to place the message close to a field for example.
Note that you can add any kind of message that will displayed to the user:
FacesContext.getInstance().addMessage(null, new FacesMessage("The message to display"));
In this example, the first parameter is the ID field of the field that is concerned by this message (usefull when the message is a validation message for a specific field). null means that the message is a general information (i.e. not linked to any particular field).
You can see an example of this component here. Note that this example uses the rich:messages that is an extension (provided by RichFaces) of the "basic" <h:message/>, but the principle is the same.

Related

available scopes for richfaces messages

What i want is to be able to choose the granularity for the messages displayed in my <rich:messages /> tag, meaning that the source of these messages could be the entire application, the current page or a single element. What are the options that RichFaces provides?
I am aware of the globalOnly attribute, which can be set to true or false, but i don't understand what is considered global and what is not.
So, i am looking for the whole list of message scopes .This list of scopes could be , for example : application, session, page, form. I would also appreciate a small example of each
You could read the reference doc.
A message is typically created like this:
FacesMessage message = new FacesMessage("Error!","An error has occurred.");
facesContext.addMessage(id, message);
If the id is null the message is global. So you can either use #for to show messages for a specific component, or #globalOnly to show global messages. If you use neither you will show all messages.

h:inputText and h:outputText show different values for same (EL) field

I've got a very strange problem here. I have a rich:popupModal which is used to edit an entity. Some of the input fields on that modal use a validator. There is also some ajax going on which is used to update other fields on that modal. There is e.g. a 'Number of packages' field, a weight per package field and a total weight field. If you change the number of packages it will update the total weight field, etc.
Now when e.g. the user entered a value that is too high, the validation fails, an error message is shown. The user can either correct the values on the modal or press cancel.
When he cancels the entity is reset to the values it had before the edit (there were some ajax changes so I need to reset those). The modal closes and a table is showing the correct (reset) values for that entity.
Now the strange thing happens. When I edit another entity the modal is suddenly showing the old values for the previous entity (the ones that did not pass validation).
To debug this I was checking whether the correct instance of the commodity is used so I added h:outputText fields for some of the values and I am also showing the System hash of the entity on the modal.
I've got e.g.
<h:outputText value="#{backingBean.entity.description}"/> which prints 'Stuff 1'
and then
<h:inputText value="#{backingBean.entity.description}"/> which prints 'Stuff 3' (the first entity I've edited). WTH?
I've added getter and a dummy setter on the entity to return the System.identityHashCode. And h:outputText and h:inputText use indeed a different entity but how is that possible when the EL is #{backingBean.entity.hashId} in both cases?
I've used the richfaces a:log to see whether that shows any errors but it all looks okay.
I have tried to clear the modal on cancel (rerender it with no components in) but that didn't fix the problem either. I've tried it on both Chrome and Firefox. Both have the same problem.
What could be going on here?
My environment: JSF 2, Seam 2.3, RichFaces 4.3.3, JBoss EAP 6.1

Audit trail for JSF application - Global valueChangeListener possible?

I am trying to implement an audit trail functionality for my web application that records:
lastModified (timestamp)
modifiedBy (user information)
userComment (reason for value change)
for each of my input fields (input fields are spread over several forms with different backing beans and different valueHolder classes).
The first two (lastModified and modifiedBy) are easily done with the help of an JPA AuditListener and #PrePersit and #PreUpdate methods.
The third one is a bit tricky since it requires user interaction. Best would be a dialog that asks for the user comment.
So there are (at least) two open issues: Can I establish a "global" valueChangeListener for all input fields in my application? Is this possible without attaching <f:valueChangeListener> to each single input component? Second: How can I grab the user comment. My idea is to put a p:dialog in my web page template but this dialog needs to know from which input component it is called.
Can I establish a "global" valueChangeListener for all input fields in my application? Is this possible without attaching to each single input component?
Yes, with a SystemEventListener which get executed during PreRenderViewEvent. You need to walk through the component tree as obtained by FacesContext#getViewRoot() to find all components which are an instanceofEditableValueHolder (or something more finer-grained) and then add the new YourValueChangeListener() by the addValueChangeListener() method. See also this answer how to register the system event listener: How to apply a JSF2 phaselistener after viewroot gets built?
Second: How can I grab the user comment. My idea is to put a p:dialog in my web page template but this dialog needs to know from which input component it is called.
You could in YourValueChangeListener#processValueChange() set the component in question as a property of some request or view scoped which you grab by evaluateExpressionGet().
Recorder recorder = (Recorder) context.getApplication().evaluateExpressionGet(context, "#{recorder}", Recorder.class);
recorder.setComponent(event.getComponent());
// ...
It will execute the EL and auto-create the bean in its scope if necessary. The bean in turn should also hold the property representing the user comment. Finally, you can use it in your <p:dialog>.
<p>You have edited #{recorder.component.label}, please mention the reason:</p>
...
<h:inputText value="#{recorder.comment}" />

best approach to do jsf form validation

If I have many input controls in a form (There are separate validators for each of these input controls - like required,length and so on ) , there is a command button which submits the form and calls an action method. The requirement is - though the input control values are , say , individually okay - the combination of these values should be okay to process them together after the form submission - Where do i place the code to validate them together?
1) Can i add a custom validator for the command button and validate the combination together? like validate(FacesContext arg0, UIComponent arg1, Object value) but even then I will not have values of the other input controls except for the command button's/component's value right ?
2) can i do the validation of the combination in the action method and add validation messages using FacesMessage ?
or do you suggest any other approach?
Thanks for your time.
Point 2 is already answered by Bozho. Just use FacesContext#addMessage(). A null client ID will let it land in <h:messages globalOnly="true">. A fixed client ID like formId:inputId will let it land in <h:message for="inputId">.
Point 1 is doable, you can grab the other components inside the validator method using UIViewRoot#findComponent():
UIInput otherInput = (UIInput) context.getViewRoot().findComponent("formId:otherInputId");
String value = (String) otherInput.getValue();
You however need to place f:validator in the last UIInput component. Placing it in an UICommand component (like the button) won't work.
True, hardcoding the client ID's is nasty, but that's the payoff of a bit inflexible validation mechanism in JSF.
I've just landed on your post after having the same question.
Articles I have read so far identify that there are four types of validation for the following purposes:
Built into the Components (subscribe to individual fields; required=true, LengthValidator, etc)
'Application Validation' added to the Action in the Backing Bean (Business Logic)
Custom Validators (subscribe to individual fields)
Method in the Backing Bean used as a Custom Validator (subscribe to individual fields).
With reference to Validators: The validation mechanism in JSF was designed to validate a single component. (See S/O Question here)
In the case where you want to validate a whole form as a logical grouping of fields, it appears with standard JSF/Apache MyFaces that the most appropriate to do it is as Application Validation, as the set of individual fields take on a collective business meaning at this point.
BalusC has come up with a way of shoehorning form validation into a single validator attached to the last form item (again, see S/O Question here and another worked example on his website here) however it isn't necessarily extensible/reusable, as the references to the ID's of the form have to be hardcoded as you can't append to the validate() method's signature. You'll get away with it if you're only using the form once, but if it pops up a few times or if you generate your ID's programmatically, you're stuck.
The JSF implementation portion of Seam has a <s:validateForm /> control which can take the IDs of fields elsewhere in your form as parameters. Unfortunately, it doesn't appear that any of the MyFaces/Mojara/Sun JSF implementations have an equivalent as it isn't part of the standard.
I've successfully used the 2nd approach:
FacesMessage facesMessage =
new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
FacesContext.getCurrentInstance().addMessage(null, facesMessage);

Displaying custom error message for a blank field in a simple JSF application

I was trying out a simple JSF application, in which I need to check if the "name" field is blank, then display an error message.
The code which takes the field's value is:
<h:outputLabel value="Name"/>
<h:inputText value="#{greeting.name}" required="true">
<f:validator validatorId="NumValidator"/>
</h:inputText>
The control of the program does not go into the validator class, if the field is submitted without entering anything, and it displays the default error message:
j_id_jsp_869892673_1:j_id_jsp_869892673_4: Validation Error: Value is required.
How do i display a custom message for this ?
The Message.properties file stored the default validation messages. This file is contained in the JAR of the JSF implementation library.
If you look at the content of this file, regarding the required validation, you will see that:
javax.faces.component.UIInput.REQUIRED={0}: Validation Error: Value is required.
To define your own error messages, create a new properties file, add the adequate error message, for example:
javax.faces.component.UIInput.REQUIRED=Hey, you forgot to fill the input {0}!
(note that {0} will be replaced by the ID of the field)
then, in your faces-config.xml, define a new message-bundle:
<message-bundle>package.that.contains.the.properties.file</message-bundle>
So for example, if your my-messages.properties is stored in the foo/bar package, you will have to write:
<application>
...
<message-bundle>foo.bar.my-messages</message-bundle>
</application>
(note that you will not have to specify the .properties extension)
The way suggested by romaintaz is definitely way to go.
On the other hand if you are looking for more customization, you can use a phase listener that fires before render response phase as does some customizing.
For example first define the value for key as below:
javax.faces.component.UIInput.REQUIRED=INPUT_REQ_FAIL
then for input components that require validation have them pass a attribute using f:attribute .Then in phase listener iterate over the face messages and check for INPUT_REQ_FAIL and replace it with cutom message along with the attribute value for the component.
I think you should look at JSR 303! This is fully standard based user input, it works out of the box with JSF 2 (I use My Faces). And the nice thing is, no need for xml or properties files to specify custom messages.

Resources