【发布时间】:2012-07-16 11:29:59
【问题描述】:
我正在尝试根据 Keith Strickland 的示例为 XPage 构建 JSF 库控件。
我在构建 FileDownloadControl 时遇到了一点麻烦 这是我构建的代码:
public class Libcontrol extends UIComponentBase implements FacesComponent {
private static final String RENDERER_TYPE = "de.chris.Libcontrol ";
private static final String COMPONENT_FAMILY = "de.chris";
public Libcontrol() {
setRendererType(RENDERER_TYPE);
}
@Override
public String getFamily() {
return COMPONENT_FAMILY;
}
@SuppressWarnings("unchecked")
public void initBeforeContents(FacesContext arg0) throws FacesException {
FacesContext context;
ExpressionEvaluatorImpl evaluator;
context = FacesContext.getCurrentInstance();
evaluator = new ExpressionEvaluatorImpl(context);
XspFileDownload result = new XspFileDownload();
String sourceId = "fileDownload1/@value";
String valueExpr = "#{document1.FileField}";
ValueBinding value = evaluator.createValueBinding(result, valueExpr, sourceId,Object.class);
result.setValueBinding("value", value);
result.setDisplayLastModified(true);
result.setAllowDelete(true);
result.setTitle("filedown");
result.setRows(30);
result.setId("fileDownload1");
this.getChildren().add(result);
}
public void buildContents(FacesContext arg0, FacesComponentBuilder arg1) throws FacesException {
// Do Nothing
}
public void initAfterContents(FacesContext arg0) throws FacesException {
// Do nothing
}
}
为什么控件没有完全渲染?当我查看 HTML 代码时,我看到来自控件的 starttag,但没有要下载的文件 是的,我已将文件上传到相应的 NotesDocument。
这是我实现的渲染器,分别复制:
public class MainLibcontrolRenderer extends Renderer {
@Override
public void encodeBegin(FacesContext context, UIComponent component) {
try {
super.encodeBegin(context, component);
context = FacesContext.getCurrentInstance();
UIViewRootEx rootEx = (UIViewRootEx) context.getViewRoot();
/*rootEx.setDojoParseOnLoad(true);
rootEx.setDojoTheme(true);*/
ResponseWriter writer = context.getResponseWriter();
writer.startElement("fieldset", component);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void encodeChildren(FacesContext context, UIComponent component) {
try {
super.encodeChildren(context, component);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void encodeEnd(FacesContext context, UIComponent component) {
try {
super.encodeEnd(context, component);
ResponseWriter writer = context.getResponseWriter();
writer.endElement("fieldset");
} catch (Exception e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
buildContents // 什么都不做?
-
这是我第一次创建这样的组件。我必须用这种方法写一些东西吗?
-
我不得不问,你想在这里完成什么?我问是因为我看到您在上面的 ValueBinding 中有一个硬编码的字段名称。这违背了将组件放入插件库的目的。您将一个组件放入插件中,以使其可供服务器上的所有开发人员使用,而硬编码的 ValueBinding 会破坏该目的。此外,如果您正在从库构建应用程序,我建议您使用传统的 XPages 方式,并在库中包含任何 REST 服务、组件和关联类以及 nsf 中的业务逻辑。
-
这只是我尝试做的一个例子。硬编码的值绑定、id 等将在我的应用程序中被替换。我的问题是我不知道为什么不呈现文件下载。我正在寻找我必须做什么的建议或简单的想法,我所做的事情有什么问题