aspose file tools*
The moose likes JSF and the fly likes Custom Component : Missing Update Model Phase? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Custom Component : Missing Update Model Phase?" Watch "Custom Component : Missing Update Model Phase?" New topic
Author

Custom Component : Missing Update Model Phase?

Mario Fatone
Greenhorn

Joined: Apr 07, 2008
Posts: 4
Hey all, before i begin explaining my problem. Please take in acount that i'm really new to JSF :-)

Ok, i'm trying to create a custom component that consist of 5 seperate input boxes. This component represents the National Register Number

The problem is that when the input is valid and I click on next on my form, it doesn't trigger the actionlistener => function "ProcessAction".

I know this , because I wrote a System.out.prinln() and it doesn't show on my server log.

I believe I'm missing something that has to do with the "Update Model Phase"

However i'm not sure.

I'll post the code here below. Be aware there will be noobish mistake like ordering the code etc. sorry about that, but hey, this is my first ever custom component. ;-)

Part 1 : component
Part 2 : tag
Part 3 : Renderer
Part 4 : Validator

sorry for the extreme long post


Part 1 : Component

public class InputNRN extends UIInput {

private String year;
private String month;
private String day;
private String part1;
private String part2;

public InputNRN() {
super();
}




@Override
public void setSubmittedValue(Object arg0) {
super.setSubmittedValue(arg0);
Object [] values = (Object[]) arg0;
this.setYear((String)values[0]);
this.setMonth((String)values[1]);
this.setDay((String)values[2]);
this.setPart1((String)values[3]);
this.setPart2((String)values[4]);

}





@Override
public String getFamily() {
return "BRGFAMILY";
}

@Override
public void restoreState(FacesContext context, Object val) {

Object [] values = (Object[]) val;
super.restoreState(context, values);
setYear((String)values[1]);
setMonth((String)values[2]);
setDay((String)values[3]);
setPart1((String)values[4]);
setPart2((String)values[5]);

}

@Override
public Object saveState(FacesContext context) {

Object[] values = new Object[6];
values[0] = super.saveState(context);

values[1] = getYear();


values[2] = getMonth();
values[3] = getDay();
values[4] = getPart1();
values[5] = getPart2();

return ((Object) values);
}

@Override
protected Object getConvertedValue(FacesContext context, Object value) throws ConverterException {





System.out.println("get Converted Value");
StringBuffer nrn = new StringBuffer();


Object [] values = (Object[]) value;
nrn.append((String)values[0]);
nrn.append((String)values[1]);
nrn.append((String)values[2]);
nrn.append((String)values[3]);
nrn.append((String)values[4]);


return nrn.toString();
}

@Override
public Object getValue() {

return super.getValue();
}

@Override
public void setValue(Object arg0) {

super.setValue(arg0);
}

@Override
public Object getLocalValue() {

return super.getLocalValue();
}

public String getDay() {
return day;
}

public void setDay(String day) {
this.day = day;
}

public String getMonth() {
return month;
}

public void setMonth(String month) {
this.month = month;
}

public String getPart1() {
return part1;
}

public void setPart1(String part1) {
this.part1 = part1;
}

public String getPart2() {
return part2;
}

public void setPart2(String part2) {
this.part2 = part2;
}

public String getYear() {
return year;
}

public void setYear(String year) {
this.year = year;
}
}

Part 2 : tag

public class InputNRNTag extends UIComponentTag {

private String styleClass;
private String required;
private String value;
private String tabindex;
private String disabled;
private String onChange;

@Override
public String getComponentType() {
return "BRGFAMILY";
}

@Override
public String getRendererType() {
return "BRGRENDERER";
}

@Override
protected void setProperties(UIComponent component) {
super.setProperties(component);

FacesContext context = getFacesContext();

if (styleClass != null) {
if (isValueReference(styleClass)) {
ValueBinding vb =
context.getApplication().createValueBinding(styleClass);
component.setValueBinding("styleClass", vb);
} else {
component.getAttributes().put("styleClass", styleClass);
}
}

if (required != null) {
if (isValueReference(required)) {
ValueBinding vb =
context.getApplication().createValueBinding(required);
component.setValueBinding("required", vb);
} else {
component.getAttributes().put("required", new Boolean(required));
}
}



if (value != null) {
if (isValueReference(value)) {
ValueBinding vb =
context.getApplication().createValueBinding(value);
component.setValueBinding("value", vb);
} else {
component.getAttributes().put("value", value);
}
}

if (tabindex != null) {
if (isValueReference(tabindex)) {
ValueBinding vb =
context.getApplication().createValueBinding(tabindex);
component.setValueBinding("tabindex", vb);
} else {
component.getAttributes().put("tabindex", tabindex);
}
}

if (disabled != null) {
if (isValueReference(disabled)) {
ValueBinding vb =
context.getApplication().createValueBinding(disabled);
component.setValueBinding("disabled", vb);
} else {
component.getAttributes().put("disabled", disabled);
}
}

if (onChange != null) {
if (isValueReference(onChange)) {
ValueBinding vb =
context.getApplication().createValueBinding(onChange);
component.setValueBinding("onChange", vb);
} else {
component.getAttributes().put("onChange", onChange);
}
}

}

public String getRequired() {
return required;
}

public void setRequired(String required) {
this.required = required;
}

public String getStyleClass() {
return styleClass;
}

public void setStyleClass(String styleClass) {
this.styleClass = styleClass;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public String getTabindex() {
return tabindex;
}

public void setTabindex(String tabindex) {
this.tabindex = tabindex;

}

public String getDisabled() {
return disabled;
}

public void setDisabled(String disabled) {
this.disabled = disabled;
}

public String getOnChange() {
return onChange;
}

public void setOnChange(String onChange) {
this.onChange = onChange;
}
}

Part 3 : Renderer

public class InputNRNRenderer extends Renderer {

@Override
public void decode(FacesContext context, UIComponent component) {

System.out.println("decode");
Map params = context.getExternalContext().getRequestParameterMap();

String clientId = component.getClientId(context);

Object values[] = new Object[5];

String year = (String)(params.get(clientId + "_year"));

if( year != null){
values[0] = (params.get(clientId + "_year"));
values[1] = (params.get(clientId + "_month"));
values[2] = (params.get(clientId + "_day"));
values[3] = (params.get(clientId + "_part1"));
values[4] = (params.get(clientId + "_part2"));

((EditableValueHolder) component).setSubmittedValue(values);

}
}

@Override
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {

System.out.println("encode");
String clientId = component.getClientId(context);
ResponseWriter out = context.getResponseWriter();

String styleClass = (String) component.getAttributes().get("styleClass");
String tabindex = (String)component.getAttributes().get("tabindex");
String disabled = (String)component.getAttributes().get("disabled");
String onChange = (String)component.getAttributes().get("onChange");

InputNRN inputComponent = (InputNRN) component;



renderMenu(out, clientId + "_year", styleClass, tabindex , disabled, onChange ,2, ".", component, inputComponent.getYear());
renderMenu(out, clientId + "_month", styleClass,tabindex , disabled,onChange,2, ".", component, inputComponent.getMonth());
renderMenu(out, clientId + "_day", styleClass, tabindex, disabled,onChange,2, "-", component, inputComponent.getDay());
renderMenu(out, clientId + "_part1", styleClass,tabindex, disabled, null, 3, ".", component, inputComponent.getPart1());
renderMenu(out, clientId + "_part2", styleClass, tabindex,disabled, null, 2, "", component, inputComponent.getPart2());

ValueExpression exp = component.getValueExpression("value");
String nrn = (String) context.getApplication().evaluateExpressionGet(context, exp.getExpressionString(), String.class);
}

private void renderMenu(ResponseWriter out, String clientId, String styleClass,String tabindex, String disabled, String onChange ,int size, String delim, UIComponent component, String value) throws IOException {



out.startElement("input", component);
out.writeAttribute("id", clientId, "id");
out.writeAttribute("name", clientId, "id");
if (styleClass != null) {
out.writeAttribute("class", styleClass, "styleClass");
}

if(tabindex != null){
out.writeAttribute("tabindex", tabindex, "tabindex");
}
if(tabindex != null){
out.writeAttribute("disabled", disabled, "disabled");
}
out.writeAttribute("size", size, "size");
out.writeAttribute("maxlength", size, "maxlength");
out.writeAttribute("value", value, "value");
if(onChange != null){
out.writeAttribute("onChange", onChange, "onChange");
}
out.endElement("input");

}

@Override
public void encodeChildren(FacesContext arg0, UIComponent arg1) throws IOException {
super.encodeChildren(arg0, arg1);
}

@Override
public void encodeEnd(FacesContext arg0, UIComponent arg1) throws IOException {
super.encodeEnd(arg0, arg1);
}

@Override
public boolean getRendersChildren() {
return super.getRendersChildren();
}
}

Part 4 : Validator

public class InputNRNValidator implements Validator {

public void validate(FacesContext arg0, UIComponent arg1, Object obj) throws ValidatorException {



if (obj != null && obj instanceof String) {
String value = (String) obj;
if (obj.equals("") || !value.matches("[0-9]+") || value.length() != 11) {

FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Value should contain 11 numeric characters", "Value should contain 11 numeric characters");
throw new ValidatorException(facesMsg);

} else {

Integer firstNumber = Integer.parseInt(value.substring(0, 1));

Integer firstPart = Integer.parseInt(value.substring(0, 9));

Integer lastPart = Integer.parseInt(value.substring(9, 11));


if (firstNumber > 0) {
if (lastPart != (97 - (firstPart % 97))) {
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Value is not valid", "Value is not valid");
throw new ValidatorException(facesMsg);

}
} else {
if (lastPart != (97 - ((firstPart + 2000000000) % 97))) {
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Value is not valid", "Value is not valid");
throw new ValidatorException(facesMsg);


}
}
}
} else {

FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Value is required", "Value is required");
throw new ValidatorException(facesMsg);

//absolete code, will use the default "required-error" message


}


}
}
[ April 07, 2008: Message edited by: Mario Fatone ]
Mario Fatone
Greenhorn

Joined: Apr 07, 2008
Posts: 4
no one that can give me some pointers?
 
 
subject: Custom Component : Missing Update Model Phase?