使用自定义Portlet

时间:2015-08-27 09:01:37

标签: java liferay portlet

我有一个包含2个字段的portlet - 用户名和密码。 而不是转到SIGN IN portlet(蓝色按钮,Liferay页面的右上角),然后登录,我想使用我的自定义portlet并登录Liferay。

以下是我使用的代码 -

LoginUtilCompat.java

package com.compass.service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.util.ClassResolverUtil;
import com.liferay.portal.kernel.util.MethodKey;
import com.liferay.portal.kernel.util.PortalClassInvoker;
/**
 * This class provides a compatibility layer that isolates differences between different versions of Liferay Portal.
 *
 * @author  Neil Griffin
 */
public class LoginUtilCompat {
    // Private Constants
    private static final String LOGIN_UTIL_FQCN = "com.liferay.portlet.login.util.LoginUtil";
    private static final String LOGIN_METHOD = "login";
    private static final Class <? > [] LOGIN_PARAM_TYPES = new Class <? > [] {
        HttpServletRequest.class, HttpServletResponse.class, String.class, String.class, boolean.class, String.class
    };
    public static Object invokeLogin(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
        String handle, String password, boolean rememberMe, String authType) throws Exception {
        Class <? > loginUtilClass = ClassResolverUtil.resolveByPortalClassLoader(LOGIN_UTIL_FQCN);
        MethodKey methodKey = new MethodKey(loginUtilClass, LOGIN_METHOD, LOGIN_PARAM_TYPES);
        return PortalClassInvoker.invoke(false, methodKey, httpServletRequest, httpServletResponse, handle, password,
            rememberMe, authType);
    }
}

我调用登录方法的课程如下所示 -

Login.java

public class Login extends MVCPortlet {

    @ProcessAction(name="customlogin")
    public void customlogin(ActionRequest actionRequest,
            ActionResponse actionResponse) throws IOException, PortletException {
        ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
        PortletConfig portletConfig = (PortletConfig)actionRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
        LiferayPortletConfig liferayPortletConfig = (LiferayPortletConfig) portletConfig;

        SessionMessages.add(actionRequest, liferayPortletConfig.getPortletId() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);

        String portletId = (String) actionRequest.getAttribute(WebKeys.PORTLET_ID);



        System.out.println("portlet id is : "+portletId);
        String login = ParamUtil.getString(actionRequest, "login");
        String password = ParamUtil.getString(actionRequest, "password");
        System.out.println("Login : "+login +"\nPassword :"+password);
        LoginUtilCompat loginutil =new LoginUtilCompat();
        HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
        HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);

        try {
            loginutil.invokeLogin(request, response, login, password, false, null);

            System.out.println("Login invoked !!");
            //SessionMessages.add(actionRequest,"success");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("Error !!");
            e.printStackTrace();
        }

    }
    }

此代码的作用是,当我输入登录凭据时,它成功显示登录调用!消息,但SIGN IN按钮仍然保持不变(表示我没有登录Liferay但是。)

我已经使用了here提供的解决方案,但即使这样也行不通。

Liferay文档的

This部分说明必须覆盖位于Tomcat目录的/ ROOT / WEB-INF寄存器中的<struts-config.xml>中的登录操作(这是一个Strut操作)。但是,由于我正在开发一个即将投入生产的门户网站,而且我不太了解Struts,我不想搞砸我不了解的技术。

0 个答案:

没有答案