Showing posts with label JSF. Show all posts
Showing posts with label JSF. Show all posts

Friday, February 24, 2012

JSF Validation

1. Customize error message.




You can only have one validatorMessage. So if you have multiple validators, please use custom validators.

2. For default validator, if you want customrize message, you can do this too.





put this into resources bundle.
javax.faces.validator.LongRangeValidator.MAXIMUM={1}: Value is greater than allowable maximum of ''{0}''

3. How to specify field name in validator.







private void checkDate(Date date, UIComponent uiComponent, Locale locale) {
if(isDateInRange(date) == false) {
ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
String messageText = getFieldLabel(uiComponent) +" " + rb.getString(getErrorKey());

throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
messageText, messageText));
}
}

protected String getFieldLabel(UIComponent uiComponent) {
String fieldLabel = (String) uiComponent.getAttributes().get("fieldLabel");

if(fieldLabel == null) {
fieldLabel = "Date" ;
}

return fieldLabel;
}

Check this link for details.

Thursday, February 9, 2012

JSF Managed Bean

Today I made some changes JSF managed beans using annotation instead of XML. Below it is the example.


@ManagedBean(name="themeSwitcherBean") -- Declare it as managed bean
@RequestScoped
public class MyThemeSwitcherBean {

@ManagedProperty(value = "#{userAccountService}") -- How to reference spring bean or other managed bean.
UserAccountService userAccountService;