在XPage中的新Java自定义控件中插入现有控件

时间:2013-04-02 13:20:58

标签: java xpages xpages-extlib

您好我仍在尝试为XPage开发自己的java控件。我想知道如何使用现有的控件并在我的新控件中使用它。

假设我想开发类似登录弹出控件的东西。我从com.ibm.xsp.extlib.component.dialog扩展了UIDialog但是如何在其上添加xp:inputText

好的,在这个例子中,我可以在xp:callback的标记部分添加control.xsp-config方面,所以我只需要像标准控件那样拖动一些输入字段,但如果我需要它,该怎么办?我可以使用更新站点部署一个控件。

更新

好的尝试了你的解决方案keithstric。我猜你在你的组件上工作。我使用e渲染器作为我的组件。因此,如果我使用渲染器或直接在我的组件中添加组件,那么组件树中是否存在差异:

渲染器:

public class MyRenderer extends Renderer {

public void encodeBegin(FacesContext context, UIComponent component)throws IOException {
    ResponseWriter w = context.getResponseWriter();
    NewComponentXY comp = new ComponentXY();
    component.getChildren().add(comp);
}

或直接:

public class MyComponent extends UIComponentBase {

    public void encodeBegin(FacesContext context, UIComponent component)throws IOException {
        ResponseWriter w = context.getResponseWriter();
        NewComponentXY comp = new ComponentXY();
        this.getChildren().add(comp);
    }

jsf生命周期有区别吗?

1 个答案:

答案 0 :(得分:1)

首先,为了确保我们都说同一种语言,你所谓的“java控件”,实际上是一个“组件”。这样它就不会与自定义控件混淆。

您需要在组件的encodeBegin方法中注入任何其他组件,而不是渲染器的encodeBegin,而是组件的encodeBegin。要做到这一点,你需要一个面板或div的某种容器,如果你不需要数据源我推荐一个div。然后将您的用户名和密码字段以及您可能想要的任何标签注入div。根据您希望的复杂程度(即一个带有标签列和另一列密码的表),此代码可能会变得相当大。但一个简单的例子是:

XspDiv cont = new XspDiv();
cont.setId("loginFormContainer");
cont.setStyleClass("loginFormContainer");
XspInputText userName = new XspInputText();
userName.setId("userNameInputId");
Attr placeHolderAttr = new Attr();
placeHolderAttr.setComponent(userName);
placeHolderAttr.setName("placeholder");
placeHolderAttr.setValue("Enter your Username");
userName.addAttr(attr);

XspInputText password = new XspInputText();
password.setId("passwordInputId");
password.setPassword(true);
//NOTE: I can't get the placeholder attr to work on a password field but
//you can try as your mileage may vary

cont.getChildren().add(userName);
cont.getChildren().add(password);
this.getChildren().add(cont);

您可以找到IBM here提供的90%的组件类以及所有组件属性的细分。我的博客here上还有一篇关于组件注入的文章,另一篇关于组件开发的文章here。如果encodeBegin方法太晚并且需要对注入的组件进行部分刷新,则可能需要添加FacesComponent接口并在initBeforeContents方法中执行注入。

作为奖励,要点击部分刷新,这需要通过CSJS完成。所以看看

((UIViewRootEx2)FacesContext.getCurrentInstance()。getViewRoot())。postScript(“CSJS Code here”);

相关问题