Liferay Mvc 开发(增删改查)

1、ThemeDisplay是什么意思?

在Portlet中request分为两种renderRequet和actionRequest而portlet需要取得实例Id的时候都在renderRequest的时候才可以取到,如下例子

PortletPreferences preferences = renderRequest.getPreferences();
String portletResource = ParamUtil.getString(request, "portletResource");
if (Validator.isNotNull(portletResource)) {
	preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
	
}
其中portletResource是portlet的id 然后通过PortletPreferencesFactoryUtil工具类可以取得相应的portlet配置信息。

而在actionRequest是取不到的portlet实例的id的,actionRequest只是把当前页面所有东西传进来而已,中间经过portletAcition做了处理,如果在portletAction中做取portletId是不可取的,但是在Liferay中已经提供了这样的方法可以让我们在actionRequest中取得portlet的实例Id代码如下

 ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest
		 .getAttribute(WebKeys.THEME_DISPLAY);
		 PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
		System.out.println("portletDisplay---->"+portletDisplay.getInstanceId());
		System.out.println("portletDisplay getId---->"+portletDisplay.getId());
		System.out.println("portletDisplay getPortletName---->"+portletDisplay.getPortletName());

这样就可以取到我们需要的对应的portlet实例id了

 

其中WebKeys.THEME_DISPLAY是Liferay中里面的接口常量 对应的名字是 THEME_DISPLAY

 

2、

PermissionChecker 是什么意思?

  1. com.liferay.portal.kernel.portlet.LiferayPortlet.StrutsPortlet中获取方式:
  2. PermissionChecker permissionChecker =
  3. PermissionThreadLocal.getPermissionChecker();
  4. com.liferay.portlet.journal.service.permission.JournalArticlePermission
  5. 页面中:
  6. JournalArticlePermission.contains(permissionChecker, article, ActionKeys.UPDATE)
  7. 页面中直接拿permissionChecker对象是通过com.liferay.taglib.theme.DefineObjectsTei定义的:
  8. new VariableInfo(
  9. "permissionChecker", PermissionChecker.class.getName(), true,
  10. VariableInfo.AT_END),
  11. JournalArticlePermission:>>>>>
  12. publicstaticboolean contains(
  13. PermissionChecker permissionChecker, JournalArticle article,
  14. String actionId) {
  15. if (permissionChecker.hasOwnerPermission(
  16. article.getCompanyId(), JournalArticle.class.getName(),
  17. article.getResourcePrimKey(), article.getUserId(), actionId)) {
  18. returntrue;
  19. }
  20. return permissionChecker.hasPermission(
  21. article.getGroupId(), JournalArticle.class.getName(),
  22. article.getResourcePrimKey(), actionId);
  23. }
  24. //通过ThemeDisplay也可以拿permissionChecker
  25. ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
  26. WebKeys.THEME_DISPLAY);
  27. PermissionChecker permissionChecker =
  28. themeDisplay.getPermissionChecker();
  29. //==================================================
  30. //通过PermissionThreadLocal也可以拿permissionChecker
  31. PermissionChecker permissionChecker =
  32. PermissionThreadLocal.getPermissionChecker();
  33. 为什么?
  34. 因为>>:PermissionCheckerUtil
  35. publicstaticvoid setThreadValues(User user) {
  36. long userId = user.getUserId();
  37. String name = String.valueOf(userId);
  38. PrincipalThreadLocal.setName(name);
  39. try {
  40. PermissionChecker permissionChecker =
  41. PermissionThreadLocal.getPermissionChecker();
  42. if (permissionChecker == null) {
  43. permissionChecker = (PermissionChecker)Class.forName(
  44. PropsValues.PERMISSIONS_CHECKER).newInstance();
  45. /**
  46. PropsValues.PERMISSIONS_CHECKER
  47. PropsValues>>>
  48. public static final String PERMISSIONS_CHECKER =
  49. PropsUtil.get(PropsKeys.PERMISSIONS_CHECKER); //就是PropsUtil.get("permissions.checker");
  50. portal.properties>>>>:
  51. permissions.checker=com.liferay.portal.security.permission.AdvancedPermissionChecker
  52. */
  53. }
  54. permissionChecker.init(user, _CHECK_GUEST);
  55. PermissionThreadLocal.setPermissionChecker(permissionChecker);
  56. }
  57. catch (Exception e) {
  58. _log.error(e);
  59. 什么时候在哪里会调用PermissionCheckerUtil.setThreadValues(user);呢?
  60. 两个地方:
  61. 1)MessageListenerImpl类的方法deliver中: PermissionCheckerUtil.setThreadValues(user);
  62. 2)MailingListMessageListener类的方法processMessage中:PermissionCheckerUtil.setThreadValues(user);
  63. 继续问,什么时候操作MessageListener?
  64. 在MainServlet的init方法中:
  65. try {
  66. Iterator<Portlet> itr = portlets.iterator();
  67. while (itr.hasNext()) {
  68. Portlet portlet = itr.next();
  69. MessageListener popMessageListener =
  70. portlet.getPopMessageListenerInstance();
  71. if (!portlet.isActive() || (popMessageListener == null)) {
  72. continue;
  73. }
  74. POPServerUtil.addListener(popMessageListener);
  75. /*上面这个方法会调用
  76. _instance._addListener(listener);
  77. 而_instance:private static POPServerUtil _instance = new POPServerUtil();
  78. */
  79. }
  80. }
  81. catch (Exception e) {
  82. _log.error(e, e);
  83. }
  84. //============================权限验证方式二=========================================
  85. PermissionLocalServiceImpl:
  86. boolean hasUserPermission(long userId, String actionId, long resourceId)
  87. resourceId是什么东西?
  88. //=========================权限验证方式三===========================================
  89. PortletPermissionUtil:
  90. publicstaticboolean contains(PermissionChecker permissionChecker, long plid, String portletId,String actionId)
  91. throws PortalException, SystemException{}
  92. 怎么获取plid和portletId? (分别数据库表PORTLETPREFERENCES 的字段PLID和PORTLETID)====>>>>>>>
  93. ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
  94. long plId=themeDisplay.getPlid();
  95. String portletId=themeDisplay.getPortletDisplay().getId();
  96. 实战代码:
  97. PermissionChecker permissionChecker=PermissionThreadLocal.getPermissionChecker();
  98. ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
  99. long plId=themeDisplay.getPlid();
  100. String portletId=themeDisplay.getPortletDisplay().getId();
  101. if( PortletPermissionUtil.contains(permissionChecker,plId,portletId,ActionKeys.CONFIGURATION)){
  102. //做你想做的事
  103. }

你可能感兴趣的:(Liferay Mvc 开发(增删改查))