Custom error message from entity class - jsf

How can I create a custom message for the errors that accour in the field annotation?
#Size(max = 10)
or
#Column(name = "NAME" , length = 10)
Now I see a message
CustomerDetailsForm:customerName: Validation Error: Length is greater than allowable maximum of ''10'' .
How can I change this message?

Use the message attribute of the bean validation annotation.
#Size(max=10, message="May not be more than 10 characters.")
To internationalize it, supply ValidationMessages.properties bundle files in the desired locales and use the {} to specify the bundle key, e.g:
#Size(max=10, message="{validation.max_size}")
Alternatively, you can also just use maxlength on the <h:inputText> field so that the enduser already won't be able to enter too much characters.
<h:inputText ... maxlength="10" />

Related

Primefaces text in outputLabel pattern of réal

Is there any way to make a pattern to the value of outputLabel, I tried like this way :
#Digits(integer=3, fraction=2)
I put this annotation in my Entity but nothing is changed
Someone have an idea to chnage this number in ENTITY or in the page?
<p:outputLabel value="#{VarAvancementPrj.avancementprojet}%"/>
// #Max(value=?) #Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
#Column(name = "avancementprojet", precision = 17, scale = 17)
#Digits(integer=3,fraction=2)
private Double avancementprojet;
You should add converter inside your outputlabel
<f:convertNumber pattern="#0.00" />

Every inputText has a '0' and the placeholder can not be seen [duplicate]

I have an input place that should get a number. I want it to be displayed as empty. However when I run my program, I get 0 or 0.00 as default. How can I make it empty?
This will happen if you bound the value to a primitive instead of its wrapper representation. The primitive int always defaults to 0 and the primitive double always defaults to 0.0. You want to use Integer or Double (or BigDecimal) instead.
E.g.:
public class Bean {
private Integer number;
// ...
}
Then there are two more things to take into account when processing the form submit. First, you need to instruct JSF to interpret empty string submitted values as null, otherwise EL will still coerce the empty string to 0 or 0.0. This can be done via the following context parameter in web.xml:
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
Second, if you're using Tomcat/JBoss or any server which uses Apache EL parser under the covers, then you need to instruct it to not coerce null to 0 or 0.0 in case of Number types by the following VM argument (it's unintuitively dealing with Number types as if they are primitives):
-Dorg.apache.el.parser.COERCE_TO_ZERO=false
See also:
The empty string madness
h:inputText which is bound to Integer property is submitting value 0 instead of null
h:inputText which is bound to String property is submitting empty string instead of null
javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL does not work anymore since Java EE 7 / EL 3.0
I don't disagree with previous answers given, but I think you might be looking for
<h:inputText id="number" binding="#{controller.inputBox}" value="#{controller.number}" >
<f:event type="preRenderComponent" listener="#{controller.preRenderInput}" />
</h:inputText>
and inside the controller
private double number; // getters and setters
private UIInput inputBox; // getters and setters
public void preRenderInput() {
// if it is the first request
if(!FacesContext.getCurrentInstance().isPostback()) {
inputBox.setValue("");
}
}
JSF 2.0+
You can use inputNumber in primefaces-extension instead of inputText.
Like this
<pe:inputNumber />
In your XHTML code, assume you have your input field as :
<h:inputText id="newname" value="#{youAction.newName}"
style="width:130px" styleClass="form">
</h:inputText>
In your Managed Bean Action Class, map this field as :
#ManagedBean(name="yourAction")
public class YourAction {
private String newName;
...
// Add Getter & Setter Methods
...
}
By doing in this way, you won't get 0 or 0.00 as default value in your input fields.

Do not fill p:inputText with 0 when binding to an integer property [duplicate]

I have an input place that should get a number. I want it to be displayed as empty. However when I run my program, I get 0 or 0.00 as default. How can I make it empty?
This will happen if you bound the value to a primitive instead of its wrapper representation. The primitive int always defaults to 0 and the primitive double always defaults to 0.0. You want to use Integer or Double (or BigDecimal) instead.
E.g.:
public class Bean {
private Integer number;
// ...
}
Then there are two more things to take into account when processing the form submit. First, you need to instruct JSF to interpret empty string submitted values as null, otherwise EL will still coerce the empty string to 0 or 0.0. This can be done via the following context parameter in web.xml:
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
Second, if you're using Tomcat/JBoss or any server which uses Apache EL parser under the covers, then you need to instruct it to not coerce null to 0 or 0.0 in case of Number types by the following VM argument (it's unintuitively dealing with Number types as if they are primitives):
-Dorg.apache.el.parser.COERCE_TO_ZERO=false
See also:
The empty string madness
h:inputText which is bound to Integer property is submitting value 0 instead of null
h:inputText which is bound to String property is submitting empty string instead of null
javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL does not work anymore since Java EE 7 / EL 3.0
I don't disagree with previous answers given, but I think you might be looking for
<h:inputText id="number" binding="#{controller.inputBox}" value="#{controller.number}" >
<f:event type="preRenderComponent" listener="#{controller.preRenderInput}" />
</h:inputText>
and inside the controller
private double number; // getters and setters
private UIInput inputBox; // getters and setters
public void preRenderInput() {
// if it is the first request
if(!FacesContext.getCurrentInstance().isPostback()) {
inputBox.setValue("");
}
}
JSF 2.0+
You can use inputNumber in primefaces-extension instead of inputText.
Like this
<pe:inputNumber />
In your XHTML code, assume you have your input field as :
<h:inputText id="newname" value="#{youAction.newName}"
style="width:130px" styleClass="form">
</h:inputText>
In your Managed Bean Action Class, map this field as :
#ManagedBean(name="yourAction")
public class YourAction {
private String newName;
...
// Add Getter & Setter Methods
...
}
By doing in this way, you won't get 0 or 0.00 as default value in your input fields.

How to make number input area initially empty instead of 0 or 0.00?

I have an input place that should get a number. I want it to be displayed as empty. However when I run my program, I get 0 or 0.00 as default. How can I make it empty?
This will happen if you bound the value to a primitive instead of its wrapper representation. The primitive int always defaults to 0 and the primitive double always defaults to 0.0. You want to use Integer or Double (or BigDecimal) instead.
E.g.:
public class Bean {
private Integer number;
// ...
}
Then there are two more things to take into account when processing the form submit. First, you need to instruct JSF to interpret empty string submitted values as null, otherwise EL will still coerce the empty string to 0 or 0.0. This can be done via the following context parameter in web.xml:
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
Second, if you're using Tomcat/JBoss or any server which uses Apache EL parser under the covers, then you need to instruct it to not coerce null to 0 or 0.0 in case of Number types by the following VM argument (it's unintuitively dealing with Number types as if they are primitives):
-Dorg.apache.el.parser.COERCE_TO_ZERO=false
See also:
The empty string madness
h:inputText which is bound to Integer property is submitting value 0 instead of null
h:inputText which is bound to String property is submitting empty string instead of null
javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL does not work anymore since Java EE 7 / EL 3.0
I don't disagree with previous answers given, but I think you might be looking for
<h:inputText id="number" binding="#{controller.inputBox}" value="#{controller.number}" >
<f:event type="preRenderComponent" listener="#{controller.preRenderInput}" />
</h:inputText>
and inside the controller
private double number; // getters and setters
private UIInput inputBox; // getters and setters
public void preRenderInput() {
// if it is the first request
if(!FacesContext.getCurrentInstance().isPostback()) {
inputBox.setValue("");
}
}
JSF 2.0+
You can use inputNumber in primefaces-extension instead of inputText.
Like this
<pe:inputNumber />
In your XHTML code, assume you have your input field as :
<h:inputText id="newname" value="#{youAction.newName}"
style="width:130px" styleClass="form">
</h:inputText>
In your Managed Bean Action Class, map this field as :
#ManagedBean(name="yourAction")
public class YourAction {
private String newName;
...
// Add Getter & Setter Methods
...
}
By doing in this way, you won't get 0 or 0.00 as default value in your input fields.

"ValueExpression Map" of a JSF component

I'm storing value expressions in a JSF component with the f:attribute tag, e.g.:
<h:inputText ...>
<f:attribute name="myId1" value="#{bean.prop1}" />
<f:attribute name="myId2" value="#{bean.prop2}" />
<f:attribute name="myId3" value="#{bean.prop3}" />
</h:inputText>
Is there a way to access all of those value expressions programmatically? (without knowlegde of the names myId1, myId2,...)
Section 9.4.2 of the JSF 2.1 specification says that those values are stored "in the component’s ValueExpression Map".
That's the only occurrence of the term "ValueExpression Map" in the complete spec.
How do I access that map?
In the UIcomponent's Method getValueExpression() of the Jboss/Mojarra implementation the map
getStateHelper().get(UIComponentBase.PropertyKeys.bindings)
is used to obtain a single value expression.
I guess that map is a super set of the "ValueExpression Map"?
Can I be sure that all implementations and all inherited (standard) components use that map to store ValueExpressions?
Thanks.
In theory you should be able to see them all by UIComponent#getAttributes():
Map<String, Object> attributes = component.getAttributes();
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
System.out.printf("name=%s, value=%s%n", entry.getKey(), entry.getValue());
}
However, that doesn't work the way as you'd expect. It only returns static attributes. This does not seem to ever going to be fixed/implemented. See also JSF issue 636. I'd suggest to stick to attribtues with predefinied prefix and an incremental numerical suffix, like as you've presented in your example. That's also what I've always used to pass additional information from the component on to custom validators and converters. You can just collect them as follows:
Map<String, Object> attributes = component.getAttributes();
List<Object> values = new ArrayList<Object>();
for (int i = 1; i < Integer.MAX_VALUE; i++) {
Object value = attributes.get("myId" + i);
if (value == null) break;
values.add(value);
}
System.out.println(values);
An alternative to the answer given by BalusC might be to use nested facets or UIParameter components. Facets can be retrieved as a map using getFacets but you probably need to put an additional UIOutput inside each facet to access its value expression.
Nested UIParameters can be accessed by iterating over the components children and checking for instanceof UIParameter. UIParameters have name and value attributes and so could be easily converted to a map.
I have used parameters in a custom component, but I'm not sure how a standard UIInput like in your example reacts to these.
BalusC is right. UIComponent#getAttributes().get(name) gets values from both places - at first from attributes map and then if not found from "value expression map". To put some value you have to call UIComponent#setValueExpression(name, ValueExpression). If value is literal, it gets stored into the attribute map, otherwise into the "value expression map". Everything is ok then.

Resources