我正在尝试使用AutoFields和DefaultConfigurationAction将一组键值对存储在数据库中。这是为了存储AD名称和人类可读名称之间的映射。我很难将我手动设置的首选项提供给最终用户。我查看了数据库,发现它们是在portlet配置首选项中输入的,而不是在实际首选项行中输入的。
有更好的方法吗?渲染时如何使手动设置的首选项可用于portlet。
public class StudentWorkerConfig extends DefaultConfigurationAction {
private static Log _log = LogFactoryUtil.getLog(StudentWorkerConfig.class);
@Override
public void processAction(
PortletConfig portletConfig, ActionRequest actionRequest,
ActionResponse actionResponse) throws Exception {
super.processAction(portletConfig, actionRequest, actionResponse);
PortletPreferences prefs = actionRequest.getPreferences();
String[] groupIndexes = ParamUtil.getString(actionRequest, "groupIndexes")
.split(",");
prefs.setValues("GC-groupIndexes", groupIndexes);
_log.info("groupIndexes: " + Arrays.toString(groupIndexes));
_log.info("Stored: " + Arrays.toString(
prefs.getValues("GC-groupIndexes", new String[]{"NO VALUES"})));
// re map to zero to end
int j = 0;
for (String i : groupIndexes) {
String adGroup = ParamUtil.getString(actionRequest, "adGroup" + i);
String humanName = ParamUtil.getString(actionRequest, "humanName" + i);
prefs.setValue("GC-adGroup" + j, adGroup);
prefs.setValue("GC-humanName" + j, humanName);
++j;
}
// We are not using the default stuff, so we need to store them
prefs.store();
for(Enumeration<String> e = prefs.getNames(); e.hasMoreElements();)
_log.info(e.nextElement());
_log.info(actionRequest.getParameterMap());
}
}
我需要支持任意数量的映射,因此我使用自动字段根据需要添加它们。我知道要保存的首选项键应该采用“ preferences--MyKey--”的形式,但我一直无法弄清楚如何使AUI做到这一点。
<aui:form action="<%= configurationURL %>" method="post" name="fm">
<aui:input name="<%= Constants.CMD %>" type="hidden" value="<%= Constants.UPDATE %>" />
<aui:input
name="preferences--primaryAffiliation--"
label="Primary Affiliation"
value='<%= portletPreferences.getValue("primaryAffiliation", "student") %>'
/>
<div id="group_mapping">
<%
String[] oldRun = portletPreferences.getValues("GC-groupIndexes", new String[]{"0"});
log("oldRun length = " + oldRun.length);
log(Arrays.toString(oldRun));
int j = 0;
for(String i : oldRun) { %>
<div class="lfr-form-row lfr-form-row-inline">
<div class="row-fields">
<aui:input
fieldParam='<%= "adGroup" + j %>'
id='<%= "adGroup" + j %>'
name='<%= "adGroup" + j %>'
label="Ad Full Group Name"
value='<%= portletPreferences.getValue("GC-adGroup" + j, "NO VALUE") %>' />
<aui:input
fieldParam='<%= "humanName" + j %>'
id='<%= "humanName" + j %>'
name='<%= "humanName" + j %>'
label="Human Readable Name"
value='<%= portletPreferences.getValue("GC-humanName"+j, "NO VALUE") %>' />
</div> <!-- .row-fields -->
</div><!-- .lfr -->
<%
++j;
}
%>
</div><!-- -->
<aui:button-row>
<aui:button type="submit" />
</aui:button-row>
<aui:script use="liferay-auto-fields">
new Liferay.AutoFields(
{
contentBox: '#group_mapping',
fieldIndexes: '<portlet:namespace/>groupIndexes',
on: {
'clone': function(event) {
console.log('clone', event);
},
'delete': function(event) {
console.log('delete', event);
}
}
}
).render();
</aui:script>
</aui:form>