I am trying to put link in the error messages produced by JSF. For this I am using the custom renderer, it works(clicking the error, focuses the respective input field) but the problem is , all the form values gets empty. ie when error occurs, all the input fields get empty. Any suggestion will be appreciated.
package custom;
public class CustomErrorRenderer extends Renderer {
@Override @SuppressWarnings("unchecked")
public void encodeEnd(FacesContext context,
UIComponent component) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.startElement("div", component);
writer.writeAttribute("id", component.getClientId(context), "id");
writer.writeAttribute("style", "color: red", null);
writer.startElement("ul", null);
Iterator clientIds = context.getClientIdsWithMessages();
while (clientIds.hasNext()) {
String clientId = clientIds.next();
Iterator messages = context.getMessages(clientId);
if (!messages.hasNext()) { continue; }
String javaScript = "var field = document.getElementById('"
+ clientId + "');" + "if(field == null) return false;"
+ "field.focus(); return false;";
writer.startElement("li", null);
writer.startElement("a", null);
writer.writeAttribute("onclick", javaScript, null);
writer.writeAttribute("href", "#", null);
while (messages.hasNext()) {
writer.writeText(messages.next().getSummary(), null);
}
writer.endElement("a");
writer.endElement("li");
}
writer.endElement("ul");
writer.endElement("div");
}
}
This renderer is defined in faces-config.xml:
add to base HTML_BASIC renderkit
HTML_BASIC
HTML_BASIC
CustomErrorRenderer
javax.faces.Output
custom.CustomErrorRenderer
custom.CustomErrorRenderer
CustomErrorMessages
custom.Errors
javax.faces.component.UIOutput
a tag class:
package custom;
import javax.faces.webapp.UIComponentELTag;
public class CustomErrorTag extends UIComponentELTag {
@Override
public String getComponentType() {
return "custom.Errors";
}
@Override
public String getRendererType() {
return "custom.CustomErrorRenderer";
}
}
This is defined in a TLD file:
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1"
1.0
custom
http://custom
errors
custom.CustomErrorTag
empty
This goes at the top of the JSP page:
<%@ taglib prefix="custom" uri="http://custom"%