Gar*_*bis 5 editbox managed-bean xpages
我已经阅读了一些关于自定义控件中动态绑定字段的优秀帖子和文章,但他们都假设了一个文档数据源.
我想允许托管bean数据源的可能性.我尝试将属性类型设置为com.ibm.xsp.model.DataSourceor,com.ibm.xsp.extlib.model.ObjectDataSource并且这些都不适用于以下xml:
<xp:inputText
id="input"
value="${compositeData.dsn[compositeData.fieldName]}"
>
</xp:inputText>
Run Code Online (Sandbox Code Playgroud)
在使用控件的地方,我传递了自定义数据,如下所示:
<xc:input
dsn="#{issue}"
fieldName="Database"
>
</xc:input>
Run Code Online (Sandbox Code Playgroud)
为了我的测试目的,我有一个名为的托管bean issue,我调用了我的字段Database.我通常会绑定#{issue.Database}但我无法弄清楚如何动态地这样做.理想情况下,我也想支持文档数据源,但如果我不能同时支持,那么我需要绑定到托管bean.
编辑:问题似乎是数组符号.如果我硬编码我的价值#{issue.Database}它有效,但如果我硬编码#{issue[Database]}它失败.所以问题是是否存在点符号的替代表示.我今天没有时间,但我想知道如果我只是将#{issue}传递给dsn而不是将dsn和fieldName分开并使用它作为我的数据绑定会有效吗?当我有机会时,我会尝试.
Edit2:因为看起来问题可能与我正在使用的bean有关,我将在这里发布代码.
AbstractMapModel
public abstract class AbstractMapModel implements Serializable, DataObject {
private static final long serialVersionUID = 1L;
private Map<Object, Object> values;
public Class<?> getType(final Object key) {
Class<?> result = null;
if (getValues().containsKey(key)) {
Object value = getValues().get(key);
if (value != null) {
result = value.getClass();
}
}
return result;
}
protected Map<Object, Object> getValues() {
if (values == null) {
values = new HashMap<Object, Object>();
}
return values;
}
public Object getValue(final Object key) {
return getValues().get(key);
}
public boolean isReadOnly(final Object key) {
return false;
}
public void setValue(final Object key, final Object value) {
getValues().put(key, value);
}
}
Run Code Online (Sandbox Code Playgroud)
AbstractDocumentMapModel
public abstract class AbstractDocumentMapModel extends AbstractMapModel {
private static final long serialVersionUID = 1L;
private String unid;
public AbstractDocumentMapModel() {
String documentId = ExtLibUtil.readParameter(FacesContext
.getCurrentInstance(), "id");
if (StringUtil.isNotEmpty(documentId)) {
load(documentId);
}
}
protected abstract String getFormName();
public String getUnid() {
return unid;
}
public void setUnid(String unid) {
this.unid = unid;
}
public void load(final String unid) {
setUnid(unid);
Document doc = null;
try {
if (StringUtil.isNotEmpty(getUnid())) {
doc = ExtLibUtil.getCurrentDatabase().getDocumentByUNID(
getUnid());
DominoDocument wrappedDoc = DominoDocument.wrap(doc
.getParentDatabase().getFilePath(), // databaseName
doc, // Document
null, // computeWithForm
null, // concurrencyMode
false, // allowDeleteDocs
null, // saveLinksAs
null // webQuerySaveAgent
);
for (Object eachItem : doc.getItems()) {
if (eachItem instanceof Item) {
Item item = (Item) eachItem;
String itemName = item.getName();
if (!("$UpdatedBy".equalsIgnoreCase(itemName) || "$Revisions"
.equalsIgnoreCase(itemName))) {
setValue(item.getName(), wrappedDoc.getValue(item
.getName()));
}
DominoUtil.incinerate(eachItem);
}
}
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
DominoUtil.incinerate(doc);
}
}
protected boolean postSave() {
return true;
}
protected boolean querySave() {
return true;
}
public boolean save() {
boolean result = false;
if (querySave()) {
Document doc = null;
try {
if (StringUtil.isEmpty(getUnid())) {
doc = ExtLibUtil.getCurrentDatabase().createDocument();
setUnid(doc.getUniversalID());
doc.replaceItemValue("Form", getFormName());
} else {
doc = ExtLibUtil.getCurrentDatabase().getDocumentByUNID(
getUnid());
}
for (Entry<Object, Object> entry : getValues().entrySet()) {
String itemName = entry.getKey().toString();
doc.replaceItemValue(itemName, DominoUtil
.toDominoFriendly(entry.getValue()));
}
if (doc.save()) {
result = postSave();
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
DominoUtil.incinerate(doc);
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
}
IssueModel
public class IssueModel extends AbstractDocumentMapModel implements
Serializable {
private static final long serialVersionUID = 1L;
@Override
protected String getFormName() {
return "frmIssue";
}
@Override
protected boolean querySave() {
return super.querySave();
}
@Override
public boolean isReadOnly(final Object key) {
boolean result = super.isReadOnly(key);
/**
* Implement read only logic here as follows
*
* if ("jobTitle".equalsIgnoreCase((String) key)) { if
* (!ExtLibUtil.getXspContext().getUser().getRoles().contains("[HR]")) {
* result = true; } }
*/
return result;
}
Run Code Online (Sandbox Code Playgroud)
}
ccFieldset
<?xml version="1.0" encoding="UTF-8"?>
<xp:view
xmlns:xp="http://www.ibm.com/xsp/core"
>
<div
class="form-group"
>
<xp:label
id="label"
for="input"
value="${compositeData.label.text}"
>
<xp:this.styleClass><![CDATA[${javascript:styleClass = "control-label col-" + compositeData.sz + "-" + compositeData.label.columns;
return styleClass;}]]></xp:this.styleClass>
</xp:label>
<xp:div>
<xp:this.styleClass><![CDATA[${javascript:styleClass = "col-" + compositeData.sz + "-" + compositeData.input.columns;
return styleClass;}]]></xp:this.styleClass>
<xp:inputText
id="input"
>
<xp:this.value><![CDATA[${javascript:"#{"+compositeData.BindTo+"}"}]]></xp:this.value>
<xp:this.styleClass><![CDATA[${javascript:styleClass = "input-" + compositeData.sz;
return styleClass;}]]></xp:this.styleClass>
</xp:inputText>
</xp:div>
</div>
</xp:view>
Run Code Online (Sandbox Code Playgroud)
xpage中的工作字段
<div
class="form-group"
>
<xp:label
value="Database"
id="database_Label1"
for="database1"
styleClass="col-sm-2 control-label"
>
</xp:label>
<div
class="col-sm-6"
>
<xp:inputText
value="#{issue.Database}"
id="database1"
styleClass="input-sm"
>
</xp:inputText>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
不在xpage中使用ccFieldset
<xc:fieldset sz="md">
<xc:this.input>
<xc:input
columns="10"
bindTo="issue.Database"
>
</xc:input>
</xc:this.input>
<xc:this.label>
<xc:label
columns="2"
text="test"
>
</xc:label>
</xc:this.label>
</xc:fieldset>
Run Code Online (Sandbox Code Playgroud)
诀窍是将String作为参数移交给您想要使用的EL.假设你有一个参数bindTo as String,其值为myBean.Color
<xp:this.value><![CDATA[${javascript:"#{"+compositeData.BindTo+"}"}]]></xp:this.value>
Run Code Online (Sandbox Code Playgroud)
$将首先评估并将compositeData替换为实际值.方法之美:适用于任何EL.文档Bean等,因此您的自定义控件不需要对其容器进行任何假设.
因此,您可以使用各种绑定调用组件:
<xc:myComponent BindTo="document1.subject"></xc:myComponent>
<xc:myComponent BindTo="viewScope.someVariable"></xc:myComponent>
<xc:myComponent BindTo="myBean.Color"></xc:myComponent>
Run Code Online (Sandbox Code Playgroud)
让我们知道这对您有何帮助!