Liferay 自定义MVCPortlet并随时检查用户是否退出

1.新建MVCPortlet


import java.io.IOException;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.servlet.http.HttpServletRequest;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;


public class TabPortlet extends MVCPortlet{

	public static String isLoginUrl=null;
	
	@Override
	public void serveResource(ResourceRequest resourceRequest,ResourceResponse resourceResponse) throws IOException,PortletException {
		ThemeDisplay td=(ThemeDisplay)resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);
		HttpServletRequest req = PortalUtil.getHttpServletRequest(resourceRequest);
		//判断用户是否登录
		if(!td.isSignedIn()){
			//用户退出后设置session参数
			req.getSession().setAttribute("isLogin", false);
		}
		super.serveResource(resourceRequest, resourceResponse);
	}

	//用户访问界面时就执行这个方法
	@Override
	public void doView(RenderRequest renderRequest,RenderResponse renderResponse) throws IOException, PortletException {
		ThemeDisplay td=(ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
				
		try {
			
			String portalURL=PortalUtil.getLayoutURL(td);
			String ppid=td.getPortletDisplay().getId();
			
			//http://10.180.26.101:8090/tab?p_p_id=SmartCLOUD_TAB_WAR_SmartCLOUD_TABportlet&p_p_lifecycle=2&p_p_mode=view
			//url
			System.out.println("PortalURL:"+PortalUtil.getLayoutURL(td));
			//p_p_id
			System.out.println("pid:"+ td.getPortletDisplay().getId());
			
			//执行这个URL就会进入到serveResource方法里面
			isLoginUrl=portalURL+"?p_p_id="+ppid+"&p_p_lifecycle=2&p_p_mode=view";
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.doView(renderRequest, renderResponse);
	}
	
	

}

2.修改portlet.xml

QQ截图20200403123019

3.判断用户是否退出,先接执行链接(isLoginUrl
),然后获取session参数进行判断就可以了




















(1)