1
How can I format a datatable column (ZIP) with the mask "00.000-000"?
Example:
Valor: 90560120 - Exit: 90.560-120
1
How can I format a datatable column (ZIP) with the mask "00.000-000"?
Example:
Valor: 90560120 - Exit: 90.560-120
1
I’m still looking for a way more siples, but I ended up solving this way:
datatable:
<h:outputText value="#{bean.format('##.###-###', item.cep, true)}" style="float:left"/>
Bean:
public String format(String pattern, Object value, boolean suppressZero) {
if (!suppressZero || Double.parseDouble(value.toString()) != 0) {
MaskFormatter mask;
try {
mask = new MaskFormatter(pattern);
mask.setValueContainsLiteralCharacters(false);
return mask.valueToString(value);
} catch (ParseException e) {
throw new RuntimeException(e);
}
} else {
return "";
}
}
1
If you want a input
then the @Anderson response is more appropriate. If you want just one OutputText
or OutputLabel
i recommend creating your own convert, would have a better semantics, a clearer understanding of what is being done and is easily reused on other screens.
Your facelets would be:
<h:outputText value="#{seuBean.cep}">
<f:converter converterId="CEPConverter" />
</h:outputText>
And the convert would be:
@FacesConverter("CEPConverter")
public class CEPConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
String[] parts = value.split("-\\.");
return Integer.parseInt(join(parts));
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
String valueAsString = value.toString();
return formatCEP(valueAsString.substring(0, 2), valueAsString.substring(2, 5), valueAsString.substring(5));
}
private String formatCEP(String part1, String part2, String part3) {
return part1.concat(".").concat(part2).concat("-").concat(part3);
}
private String join(String[] parts) {
StringBuilder sb = new StringBuilder();
for(String part : parts) {
sb.append(part);
}
return sb.toString();
}
}
This is a very simple implementation, you can even improve/customize it according to your understanding.
Using Converter
how could I pass the mask as parameter?
1
I believe that creating a converter that saves only the numbers is the simplest way to solve this issue.
package br.com.t2tecnologia;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
@FacesConverter("app.numbersConverter")
public class AppNumbersConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null) {
return null;
}
return value.replaceAll("[^0-9+]", "");
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object object) {
return object.toString();
}
}
No . xhtml:
<p:inputMask mask="99.999-999" id="enderecoCepText" value="#{e.cep}" size="10"
converter="app.numbersConverter"
required="true" requiredMessage="CEP não informado."/>
Use this convert to various other data like CPF, CNPJ, etc to store only the numbers in the database column.
0
The most suitable to use in your case would be the inputMask component of Primefaces.
In your case the tag would look like this: <p:inputMask mask="99.999-9999"/>
You can find more talking about the tag here.
The p:inputMask
didn’t work because I’m just listing the data, not editing. Broken link!
Browser other questions tagged jsf primefaces
You are not signed in. Login or sign up in order to post.
What have you tried?
– GabrielOshiro
<f:convertNumber pattern="00.000.000"/>
, but I couldn’t find a way to hyphenate.– mwramos