当前位置: 首页 > 知识库问答 >
问题:

如何以编程方式在liferay中添加站点?

农均
2023-03-14

我想以编程方式在liferay中添加站点,因此我创建了一个负责以编程方式添加站点的portlet,但它不起作用,所以请任何人在这方面帮助我。当我单击create site isportlet暂时不可用时,我得到的错误是。这是我的代码:

<%@page import="javax.portlet.PortletPreferences"%>
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@page import="com.liferay.portal.kernel.util.HtmlUtil"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="com.liferay.portal.kernel.util.UnicodeProperties"%>
<%@page import="com.liferay.portal.service.LayoutSetPrototypeServiceUtil"%>
<%@page import="com.liferay.portal.model.LayoutSetPrototype"%>
<%@page import="com.liferay.portal.service.GroupLocalServiceUtil"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.kernel.bean.BeanParamUtil"%>
<%@page import="com.liferay.portal.theme.ThemeDisplay"%>
<%@page import="com.liferay.portal.model.Group"%>
<%@page import="com.liferay.portal.kernel.util.WebKeys"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/theme"  prefix="theme"%>
<%@taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<%@taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>

<portlet:defineObjects />
<theme:defineObjects/>

<%

PortletPreferences commonpref = renderRequest.getPreferences();
String commonvalue = (String)commonpref.getValue("commonvalue", "Hello!   Welcome to our portal.");
//Group group = (Group)request.getAttribute("site.group");
//Group liveGroup = (Group)request.getAttribute("site.liveGroup");
LayoutSetPrototype layoutSetPrototype = (LayoutSetPrototype)request.getAttribute("site.layoutSetPrototype");
//boolean showPrototypes = GetterUtil.getBoolean(request.getAttribute("site.showPrototypes"));

long companyid=company.getCompanyId();
String languageid=user.getLanguageId();



long liveGroupId = 0;
//Group group = (Group)request.getAttribute(WebKeys.);
List<Group> objgroup=GroupLocalServiceUtil.getGroups(0,    GroupLocalServiceUtil.getGroupsCount());
Group group=objgroup.get(0);
long groupId = BeanParamUtil.getLong(group, request, "groupId");

System.out.println("Here are group :"+group);
Group stagingGroup = null;

Group liveGroup = null;

if (group != null) {
if (group.isStagingGroup()) {
    liveGroup = group.getLiveGroup();

    stagingGroup = group;
}
else {
    liveGroup = group;

    if (group.hasStagingGroup()) {
        stagingGroup = group.getStagingGroup();
    }
}
}
     liveGroupId = liveGroup.getGroupId();
     System.out.println("Here are livegroup id :"+liveGroupId);
     request.setAttribute("group", group);
    %>
   <%=commonvalue %>
  <portlet:actionURL var="creatURL" name="Create">
  <portlet:param name="viewJSP" value="/html/sites/view.jsp">   </portlet:param>

</portlet:actionURL>

<aui:form action="<%=creatURL %>" method="post">

<liferay-ui:error key="sitename" message="The site you has enter already eits" />
<aui:input name="commonvalue" type="hidden" value="<%=commonvalue %>"/>
<aui:input name="liveGroupId" type="hidden" value="<%=liveGroupId%>" />
<aui:input name="companyid" type="hidden" value="<%=companyid%>" />
<aui:input name="languageid" type="hidden" value="<%=languageid%>" />
<aui:input name="groupId" type="hidden" value="<%= groupId %>" />
<aui:input name="group" type="hidden" value="<%= group %>" />
<aui:input name="sitename" label="Enter Site" type="text" value="">
<aui:validator name="required"></aui:validator></aui:input>
<aui:input name="discription" label="Enter site discription" type="textarea" value="">
</aui:input>



 <aui:button type="submit" value="Create site" ></aui:button>






 </aui:form>

&这是我的sites.java,它是一个操作文件:

package com.test;

import java.io.IOException;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletModeException;
import javax.portlet.ReadOnlyException;
import javax.portlet.ValidatorException;
import javax.portlet.WindowStateException; 
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.HtmlUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.UnicodeProperties;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.LayoutConstants;
import com.liferay.portal.model.LayoutSet;
import com.liferay.portal.model.LayoutSetPrototype;

import com.liferay.portal.service.GroupServiceUtil;

import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.service.LayoutServiceUtil;
import com.liferay.portal.service.LayoutSetLocalServiceUtil;
import com.liferay.portal.service.LayoutSetPrototypeServiceUtil;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory; 
import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;

import javax.portlet.PortletPreferences;
//import com.liferay.portlet.sites.util.SitesUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class Sites
 */
 public class Sites extends MVCPortlet {


public void fiveplan(ActionRequest fiveplanrequest,ActionResponse fiveplanresponse) throws ReadOnlyException, ValidatorException, IOException, PortletModeException, WindowStateException
{
    String fiveplane=fiveplanrequest.getParameter("basicplan");    

    PortletPreferences fiveplanpref=fiveplanrequest.getPreferences();

    if(fiveplane !=null)
    {
        fiveplanpref.setValue("commonvalue", fiveplane);
        fiveplanpref.store();    

        fiveplanresponse.setRenderParameter("jspPage","/html/sites/view.jsp");

    }


}



public void Create(ActionRequest actionRequest ,ActionResponse res) throws Exception
{

    try {
        String sitename=actionRequest.getParameter("sitename");
         String findsitename="";
          List<Group> siteobj=GroupLocalServiceUtil.getGroups(0, GroupLocalServiceUtil.getGroupsCount());
            for(int k=0;k<siteobj.size();k++)
            {

                if(sitename.equals(siteobj.get(k).getName()))
                {
                    //SessionErrors.add(actionRequest, "sitename");
                    findsitename=siteobj.get(k).getName();
                }

            }
         String commonvalue=actionRequest.getParameter("commonvalue");

            long liveGroupId = Long.parseLong(actionRequest.getParameter("liveGroupId"));
            long companyid=Long.parseLong(actionRequest.getParameter("companyid"));
            String languageid=actionRequest.getParameter("languageid");
            String discription=actionRequest.getParameter("discription");
            System.out.println("Here is domain name"+sitename);
            System.out.println("Here is group name"+liveGroupId);
            int type = ParamUtil.getInteger(actionRequest, "type");
            String friendlyURL = ParamUtil.getString(actionRequest, "friendlyURL");
            System.out.println("Friendlay URL is :"+friendlyURL);
            ServiceContext serviceContext = ServiceContextFactory.getInstance(Group.class.getName(), actionRequest);
            Group liveGroup = null;
            String oldFriendlyURL = null;
            String oldStagingFriendlyURL = null;
            long privateLayoutSetPrototypeId = ParamUtil.getLong(actionRequest, "privateLayoutSetPrototypeId");
            long publicLayoutSetPrototypeId = ParamUtil.getLong(actionRequest, "publicLayoutSetPrototypeId");
            System.out.println("Here are private id: :"+privateLayoutSetPrototypeId);
            System.out.println("Here are public id: :"+publicLayoutSetPrototypeId);
            System.out.println("Here are servicecontext id: :"+serviceContext);
            ServiceContext serviceContexta = new ServiceContext();
            Group liveGroups = null;

           List<LayoutSetPrototype> layoutSetPrototypes = LayoutSetPrototypeServiceUtil.search(companyid, Boolean.TRUE, null); 
                        String uuid=null;
                        for (LayoutSetPrototype curLayoutSetPrototype : layoutSetPrototypes) 
                            {
                            UnicodeProperties settingsProperties = curLayoutSetPrototype.getSettingsProperties();
                            String servletContextName = settingsProperties.getProperty("customJspServletContextName", StringPool.BLANK);
                            String name=HtmlUtil.escape(curLayoutSetPrototype.getName(languageid));
                            System.out.println("hi this is :"+ curLayoutSetPrototype.getLayoutSetPrototypeId());

                              if(commonvalue.equals(name))
                                  {
                                uuid=  curLayoutSetPrototype.getUuid();
                                  }
                                  name="";
                            }

                              if(!uuid.equals(""))
                              {

                                 if(findsitename.equals(""))        

                                 {                                    
                                GroupServiceUtil.addGroup(sitename,discription, 1, friendlyURL, true, true ,serviceContext);

                                Group groupobj=GroupLocalServiceUtil.fetchGroup(companyid, sitename);
                                long groupid=groupobj.getGroupId();
                               System.out.println("Site is creates"); 
                               LayoutSet layoutsets=LayoutSetLocalServiceUtil.getLayoutSet(groupid,false); 

                               layoutsets.setLayoutSetId(layoutsets.getLayoutSetId());
                               layoutsets.setGroupId(layoutsets.getGroupId());
                               layoutsets.setCompanyId(layoutsets.getCompanyId());
                               layoutsets.setCreateDate(layoutsets.getCreateDate());
                               layoutsets.setModifiedDate(layoutsets.getModifiedDate());
                               layoutsets.setPrivateLayout(layoutsets.getPrivateLayout());
                               layoutsets.setLogo(layoutsets.getLogo());
                               layoutsets.setLogoId(layoutsets.getLogoId());
                               layoutsets.setThemeId(layoutsets.getThemeId());
                               layoutsets.setColorSchemeId(layoutsets.getColorSchemeId());
                               layoutsets.setWapThemeId(layoutsets.getWapThemeId());
                               layoutsets.setWapColorSchemeId(layoutsets.getWapColorSchemeId());
                               layoutsets.setCss(layoutsets.getCss());
                               layoutsets.setPageCount(1);
                               layoutsets.setSettings(layoutsets.getSettings());
                               layoutsets.setLayoutSetPrototypeUuid(uuid);
                               layoutsets.setLayoutSetPrototypeLinkEnabled(true);

                               LayoutSetLocalServiceUtil.updateLayoutSet(layoutsets);




                               boolean privateLayout = ParamUtil.getBoolean(actionRequest, "privateLayout");
                               long parentLayoutId = ParamUtil.getLong(actionRequest, "parentLayoutId");
                               String name1 = ParamUtil.getString(actionRequest, "name", "home");
                               String title = StringPool.BLANK;
                              String description = StringPool.BLANK;

                               boolean hidden = false;
                               String friendlyURL1 = StringPool.BLANK;


                               ServiceContext serviceContext1 = ServiceContextFactory.getInstance(actionRequest);
                               System.out.println("Service cintext is "+serviceContext1);

                               System.out.println("Here are new site id"+groupid);

                              LayoutServiceUtil.addLayout(groupid, false ,parentLayoutId ,name1 ,title ,description ,LayoutConstants.TYPE_PORTLET ,false ,friendlyURL1,serviceContext);
                              res.setRenderParameter("jspPage", "/html/sites/view.jsp");

                                     }
                                else
                                {

                                SessionErrors.add(actionRequest, "sitename");


                                }


                              }

         }
    catch (SystemException e) {
        // TODO Auto-generated catch block
                        e.printStackTrace();
    }    

}

}

我的日志文件是:

13:08:51,568 ERROR [RuntimePageImpl-5][render_portlet_jsp:132] null
java.lang.NullPointerException
at com.test.Sites.Create(Sites.java:118)
at  com.liferay.portal.kernel.portlet.LiferayPortlet.callActionMethod(LiferayPortlet.java:163)
at  com.liferay.util.bridges.mvc.MVCPortlet.callActionMethod(MVCPortlet.java:249)
at com.liferay.portal.kernel.portlet.LiferayPortlet.processAction(LiferayPortlet.java:90)
at com.liferay.util.bridges.mvc.MVCPortlet.processAction(MVCPortlet.java:212)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:71)
at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:48)
at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:112)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:604)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:543)
at com.liferay.portlet.InvokerPortletImpl.invoke(InvokerPortletImpl.java:583)
at com.liferay.portlet.InvokerPortletImpl.invokeAction(InvokerPortletImpl.java:628)

共有1个答案

邓翼
2023-03-14

您的问题似乎应该与NullPointerExceptionIf(!uuid.equals(“”))行有关。

唯一可为空的对象是uuid

查看您的代码,由于以下测试,uuidif(commonvalue.equals(name))可能不会分配任何东西给uuid

在我看来,因为您正在测试uuid的空值,所以替换第104行(还是103行?)字符串uuid=null;使用字符串uuid=“”;

 类似资料:
  • 我需要在Liferay的用户登录应用程序时为他们分配角色。 我已经实现了实现“身份验证器”的自定义类的“身份验证器”方法中的所有逻辑。 示例代码: 当我登录时,很明显它可以工作,我检查liferay数据库的表并更新它们,我的用户分配了“管理员”角色。但是,前端的门户不显示“Admin”选项。 但如果我转到“我的帐户”,按“保存”按钮,注销并再次登录,我有可用的管理选项。 有人知道为什么会这样吗?,

  • 我正在使用SpringDoc,并试图以编程方式向OpenApi添加一个模式,但没有成功。 mySchema的描述没有添加到我在生成的YAML文件中看到的模式列表中,如果我试图引用它:

  • 我正在尝试在Android上添加Wifi网络,我想知道如何连接到不广播其SSID的Wifi网络(它是否有空SSID或带有\0s的清晰SSID)。 这是我目前用于广播其SSID的Wifi网络的内容:

  • 问题内容: 我想在头部分中以编程方式添加StyleSheets,但是我看到的示例之一似乎需要多行代码才能仅添加一个样式表,即使我可能需要很多: 示例代码: 我也使用方法,但是它也不起作用。对象null抛出了错误。 我也使用了和东西,但是它们抛出了文字错误,这是我认为的常见错误。 我使用此代码: 起初它起作用,但是当我更改页面时,它停止工作。 我正在使用“母版页”,并且正在文件中编写这些代码,也有人

  • 我试图在logback中动态添加一个appender。这是我的代码。 它工作正常,但仅适用于添加追加器的特定记录器。有没有办法让它适用于应用程序中的所有记录器?我正在寻找一种动态添加和删除追加器的方法。

  • 问题内容: 如何通过UIViewController扩展的任何类以编程方式创建选项卡: 问题答案: 更新SWIFT 5 如何以编程方式创建的一个示例如下所示: 首先,我们创建标签栏界面的每个标签的内容。对于此示例,我们仅创建一个非常简单的示例。 现在,: 我们创建了要显示在标签栏中的的新实例。然后,我们为创建的每个实例创建一个图标,然后创建一个数组,其中包含所有为标签栏界面的每个标签指定内容的数组