1

我仍在尝试访问存储库以添加新用户。我的组件连接到侧面,并且我在 FormMap 中有所有需要的值。

问题是我不知道该怎么做。在我最后一个问题在 Hippo cms 中注册用户时,我得到了我需要将组件连接到 /hippo:configuration/hippo:users 的答案。

怎么做?

这是我的实际组件:

package org.example.components;
import javax.jcr.Session;

import org.hippoecm.hst.component.support.bean.BaseHstComponent;
import org.hippoecm.hst.core.component.HstComponentException;
import org.hippoecm.hst.core.component.HstRequest;
import org.hippoecm.hst.core.component.HstResponse;
import org.hippoecm.hst.component.support.forms.FormMap;
import org.hippoecm.hst.component.support.forms.FormUtils;
import org.hippoecm.hst.component.support.forms.FormField;
import org.hippoecm.hst.content.annotations.Persistable;
import org.hippoecm.hst.content.beans.Node;
import org.hippoecm.hst.content.beans.standard.HippoFolderBean;


public class SignUpComponent extends BaseHstComponent {

@Override
public void doBeforeRender(HstRequest request, HstResponse response) {
    super.doBeforeRender(request, response);
}

@Persistable
@Override
public void doAction(HstRequest request, HstResponse response) throws HstComponentException {
    FormMap map = new FormMap(request, new String[]{"username","email","password"});
    FormField username = map.getField("username");
    FormField password = map.getField("password");
    FormField email = map.getField("email");

    try {
        // NOTE: This session will be logged out automatically in the normal HST request processing thread.
        Session persistableSession = request.getRequestContext().getSession();
    } catch (Exception e) {

    }
    Node users = persistableSession.getNode("/hippo:configuration/hippo:users");

}

虽然导入节点不起作用

error: cannot find symbol

我也试过

Node users = getSiteContentBaseBean(request).getNode().getSession().getRootNode().getNode("/hippo:configuration/hippo:users"); 
4

1 回答 1

1

对于持久化对文档的更改,以下代码可以工作。我已经根据您的示例对其进行了修改。

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.hippoecm.hst.component.support.bean.BaseHstComponent;
import org.hippoecm.hst.core.component.HstComponentException;
import org.hippoecm.hst.core.component.HstRequest;
import org.hippoecm.hst.core.component.HstResponse;
import org.hippoecm.repository.api.HippoNodeType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PersistenceExampleComponent extends BaseHstComponent {

    public static final Logger log = LoggerFactory.getLogger(PersistenceExampleComponent.class);
    public static final String USERS_PATH = "/hippo:configuration/hippo:users";

    @Override
    public void doAction(final HstRequest request, final HstResponse response) throws HstComponentException {
        Session writableSession = null;
        try {
            writableSession = this.getPersistableSession(request);
            Node usersNode = writableSession.getRootNode().getNode(USERS_PATH);
            final Node someusername = usersNode.addNode("someusername", HippoNodeType.NT_USER);
            writableSession.save();
        } catch (RepositoryException e) {
            log.error(e.getMessage());
        } finally {
            if(writableSession != null) {
                writableSession.logout();
            }
        }
    }
}

现在您需要注意,默认情况下,站点连接到存储库的用户可能没有适当的权限写入此文件夹。您可能想阅读基于 HST 的写入的访问权限页面,如果这还不够,您将需要了解存储库安全性的概念以及更改现有域以满足您的需求的域。

您可能还想查看以下代码片段,它是如何将信息从组件存储到存储库的示例。

于 2015-02-19T13:46:04.477 回答