package com.sterning.jsf.ajax;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
public interface AjaxRendererInterface {
public void handleAjaxRequest(FacesContext context, UIComponent component);
}
2.实现接口并继承Renderer类
com.sterning.jsf.ajax.component. AjaxComponentRenderer类
package com.sterning.jsf.ajax.component;
import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.*;
import com.sterning.jsf.ajax.AjaxRendererInterface;
public class AjaxComponentRenderer extends Renderer implements
AjaxRendererInterface {
private static final transient Log log = LogFactory
.getLog(com.sterning.jsf.ajax.component.AjaxComponentRenderer.class);
private static final String INPUT_ID = "com.sterning.jsf.ajax.component.INPUT";
private static final String INPUT_NAME = "com.sterning.jsf.ajax.component.INPUT";
private static final String BUTTON_ID = "com.sterning.jsf.ajax.component.BUTTON";
private static final String MESSAGE_DIV_ID = "com.sterning.jsf.ajax.component.MESSAGE_DIV";
private static final String CLIENT_ID = "com.sterning.jsf.ajax.component.CLIENT_ID";
/** *//**
* 定义渲染器。渲染器我们需要从Renderer类中继承,不过我们一般情况下会继承HtmlRenderer这个类,我们可以覆盖decode
* encodeBegin encodeChildren encodeEnd 来生成HTML
*/
public AjaxComponentRenderer() {
}
public void encodeBegin(FacesContext context, UIComponent component)
throws IOException {
if (log.isTraceEnabled()) {
log.trace("begin encodeBegin()");
}
HttpServletRequest request = (HttpServletRequest) context
.getExternalContext().getRequest();
AjaxComponent ajaxComp = (AjaxComponent) component;
ResponseWriter out = context.getResponseWriter();
String clientId = ajaxComp.getClientId(context);
out.startElement("div", ajaxComp);
out.writeAttribute("id", clientId, null);
out.writeAttribute("style", "border:solid; width:200; height:200;",
null);
out.startElement("div", ajaxComp); // Message div
out.writeAttribute("id", MESSAGE_DIV_ID, null);
out.endElement("div"); // Message div
out.startElement("input", ajaxComp);
out.writeAttribute("type", "text", null);
out.writeAttribute("id", INPUT_ID, null);
out.writeAttribute("name", INPUT_NAME, null);
out.endElement("input");
out.startElement("button", component);
out.writeAttribute("type", "button", null);
out.writeAttribute("name", BUTTON_ID, null);
out.writeAttribute("id", BUTTON_ID, null);
out.writeAttribute("value", BUTTON_ID, null);
out.writeText("Ajax It", "null");
out.endElement("button");
// A hidden field to hold the URL of the server for the ajax request
out.startElement("input", ajaxComp);
out.writeAttribute("id", "com.sterning.jsf.ajax.component.SERVER", null);
out.writeAttribute("type", "hidden", null);
out.writeAttribute("value", request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ request.getRequestURI(), null);
out.endElement("input");
// A hidden field to hold the component Client ID
out.startElement("input", ajaxComp);
out.writeAttribute("id", CLIENT_ID, null);
out.writeAttribute("type", "hidden", null);
out.writeAttribute("value", clientId, null);
out.endElement("input");
out.write("\nAjax组件\n");
out.startElement("script", ajaxComp);
out.write("dojo.addOnLoad(AjaxComponent.loadComponent());\n");
out.endElement("script");
}
/** *//**
* 处理页面按钮的请求, 该项按钮通过上面的encodeBegin()方法设置
*/
public void handleAjaxRequest(FacesContext context, UIComponent component) {
if (log.isInfoEnabled()) {
log.info("BEGIN handleAjaxRequest()");
}
HttpServletRequest request = (HttpServletRequest) context
.getExternalContext().getRequest();
String textField = request.getParameter(INPUT_NAME);
String serverContribution = "SERVER RESPONSE: ";
StringBuffer xml = null;
if (textField == null) {
if (log.isInfoEnabled()) {
log.info("No parameter found for text field.");
}
} else {
if (log.isTraceEnabled()) {
log.trace("textField: " + textField);
}
xml = new StringBuffer("");
xml.append("" + serverContribution + textField
+ "");
xml.append("OK");
}
if (xml == null) {
if (log.isInfoEnabled()) {
log.info("Response is null.");
}
xml = new StringBuffer(this.getErrorString());
}
HttpServletResponse response = (HttpServletResponse) context
.getExternalContext().getResponse();
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
try {
response.getWriter().write(xml.toString());
if (log.isInfoEnabled()) {
log.info("Response sent: " + xml);
}
} catch (IOException e) {
if (log.isErrorEnabled()) {
log.error("Error writing ajax response.", e);
}
}
}
protected String getErrorString() {
return new String(
"There was a problemERROR");
}
}
|