在 magnolia 中检索并分配 jcr:mixinTypes 多值 属性
Retrieve and assign jcr:mixinTypes a multi-valued property in magnolia
当我尝试在 magnolia 中获取多值 属性 时出现错误,因为我将其分配给了字符串类型。如何正确读取单值和多值 属性(因为两者都存在)?
javax.jcr.ValueFormatException: property
/wcmstest/de/search/jcr:mixinTypes is a multi-valued property, so it's
values can only be retrieved as an array
at org.apache.jackrabbit.core.PropertyImpl.internalGetValue(PropertyImpl.java:483)
at org.apache.jackrabbit.core.PropertyImpl.getValue(PropertyImpl.java:510)
at org.apache.jackrabbit.core.PropertyImpl.getString(PropertyImpl.java:520)
at info.magnolia.jcr.wrapper.DelegatePropertyWrapper.getString(DelegatePropertyWrapper.java:162)
at info.magnolia.jcr.wrapper.DelegatePropertyWrapper.getString(DelegatePropertyWrapper.java:162)
at info.magnolia.jcr.wrapper.DelegatePropertyWrapper.getString(DelegatePropertyWrapper.java:162)
at info.magnolia.jcr.wrapper.DelegatePropertyWrapper.getString(DelegatePropertyWrapper.java:162)
at info.magnolia.jcr.wrapper.DelegatePropertyWrapper.getString(DelegatePropertyWrapper.java:162)
at ch.uzh.module.versioning.CreateNestedMap.nestedMap(CreateNestedMap.java:32)
at ch.uzh.module.versioning.ContextLoggerCommand.execute(ContextLoggerCommand.java:78)
at info.magnolia.commands.MgnlCommand.executeSynchronized(MgnlCommand.java:80)
at info.magnolia.commands.MgnlCommand.execute(MgnlCommand.java:69)
at info.magnolia.commands.chain.ChainBase.execute(ChainBase.java:101)
at info.magnolia.commands.DelegateCommand.execute(DelegateCommand.java:66)
:
这是我使用的代码:
HashMap<String, Object> map = new LinkedHashMap<>();
PropertyIterator pi;
try {
pi = node.getProperties();
//Get properties for the root node
while(pi.hasNext())
{
Property p = pi.nextProperty();
String name = p.getName();
String val = p.getString();
map.put(name,val);
}//end of while for properties of root node
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
感谢@reporter 和一些反复试验。这里有一个working solution
.
HashMap<String, Object> map = new LinkedHashMap<>();
PropertyIterator pi;
try {
pi = node.getProperties();
//Get properties for the root node
while(pi.hasNext())
{
Property p = pi.nextProperty();
String name = p.getName();
String val;
//determine if property is multi-value
if (p.isMultiple()) {
//multi-value property
val=p.getValues().toString();
} else {
//single-value property
val = p.getValue().getString();
}
map.put(name,val);
}//end of while for properties of root node
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
当我尝试在 magnolia 中获取多值 属性 时出现错误,因为我将其分配给了字符串类型。如何正确读取单值和多值 属性(因为两者都存在)?
javax.jcr.ValueFormatException: property /wcmstest/de/search/jcr:mixinTypes is a multi-valued property, so it's values can only be retrieved as an array at org.apache.jackrabbit.core.PropertyImpl.internalGetValue(PropertyImpl.java:483) at org.apache.jackrabbit.core.PropertyImpl.getValue(PropertyImpl.java:510) at org.apache.jackrabbit.core.PropertyImpl.getString(PropertyImpl.java:520) at info.magnolia.jcr.wrapper.DelegatePropertyWrapper.getString(DelegatePropertyWrapper.java:162) at info.magnolia.jcr.wrapper.DelegatePropertyWrapper.getString(DelegatePropertyWrapper.java:162) at info.magnolia.jcr.wrapper.DelegatePropertyWrapper.getString(DelegatePropertyWrapper.java:162) at info.magnolia.jcr.wrapper.DelegatePropertyWrapper.getString(DelegatePropertyWrapper.java:162) at info.magnolia.jcr.wrapper.DelegatePropertyWrapper.getString(DelegatePropertyWrapper.java:162) at ch.uzh.module.versioning.CreateNestedMap.nestedMap(CreateNestedMap.java:32) at ch.uzh.module.versioning.ContextLoggerCommand.execute(ContextLoggerCommand.java:78) at info.magnolia.commands.MgnlCommand.executeSynchronized(MgnlCommand.java:80) at info.magnolia.commands.MgnlCommand.execute(MgnlCommand.java:69) at info.magnolia.commands.chain.ChainBase.execute(ChainBase.java:101) at info.magnolia.commands.DelegateCommand.execute(DelegateCommand.java:66) :
这是我使用的代码:
HashMap<String, Object> map = new LinkedHashMap<>();
PropertyIterator pi;
try {
pi = node.getProperties();
//Get properties for the root node
while(pi.hasNext())
{
Property p = pi.nextProperty();
String name = p.getName();
String val = p.getString();
map.put(name,val);
}//end of while for properties of root node
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
感谢@reporter 和一些反复试验。这里有一个working solution
.
HashMap<String, Object> map = new LinkedHashMap<>();
PropertyIterator pi;
try {
pi = node.getProperties();
//Get properties for the root node
while(pi.hasNext())
{
Property p = pi.nextProperty();
String name = p.getName();
String val;
//determine if property is multi-value
if (p.isMultiple()) {
//multi-value property
val=p.getValues().toString();
} else {
//single-value property
val = p.getValue().getString();
}
map.put(name,val);
}//end of while for properties of root node
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}