我在我的 liferay 门户 6 中使用自定义 porltet。我有一些我想在我的 portlet 类中使用的全局变量,因此我在 portlet.xml 文件中编写了该变量,如下所示。
<init-param>
<name>Host</name>
<value>smtp.mail.yahoo.com</value>
</init-param>
<init-param>
<name>Port</name>
<value>465</value>
</init-param>
在我的 portlet 操作类中完美运行的功能
publicList<String> ReadSmtpDataForMailNotification() {
List<String> ValuesListObj = new ArrayList<String>();
ValuesListObj.add(this.getInitParameter("Host"));
ValuesListObj.add(this.getInitParameter("Port"));
return ValuesListObj;
}
现在的问题是,当我使用 portlet 操作类执行函数时,它的工作完美,但是当我想在我的 portlet 类之外访问这个变量时......例如:- 在我的本地服务实现类中,我无法访问该变量并且 come 的值始终为 null..所以如果有人可以建议我如何在其他 portlet 类中获取 initparam 的值。
@Advaita Gosvami
在portlet.properties文件中写了my.custom.host=liferay.css.com
当我尝试使用以下内容获取值时
System.out.println("Property value : - " + PropsUtil.get("my.custom.host"));
它给了我空值..
最佳答案
摘要:
如果您想在运行时获取 portlet 的初始化参数,您可以使用 com.liferay.portal.service.PortletLocalServiceUtil
来获取您的 portlet。
然后您将获得 com.liferay.portal.model.Portlet
的实例。
这是一个非常方便的对象,因为它为您提供了调用便捷方法的可能性:
public java.util.Map<java.lang.String, java.lang.String> getInitParams();
代码片段:
在 portlet 中,在您的 liferay 服务器上运行的 servlet,在服务器启动时运行的 Hook , quartz 作业,服务......
可以调用下面的代码来获取portlet的init-param map
import com.liferay.portal.model.Portlet;
import com.liferay.portal.service.PortletLocalServiceUtil;
import java.lang.String;
import java.util.Map;
...
/* Determine what is your portlet's id, you can go as follows or find it after placing one in a page and check it's id with a tool like firebug */
String myPortletId = "myPortletName_WAR_myPortletWar";
/* Get the actual portlet model object with the appropriate service call */
Portlet myPortletObject PortletLocalServiceUtil.getPortletById(myPortletId);
/* Get the map of init-params from the portlet model object */
Map<String,String> initParamMap = myPortletObject.getInitParams();
/* You can now iterate on your map as on any other */
for (String currentParamKey : initParamMap.keySet()) {
String currentParamValue = initParamMap.get(currentParamKey);
/* do stuff */
}
幕后原则:
在 Liferay 中,需要注册一个 portlet 才能使用。
部署时,门户注册它,注册它的 init-params
和一堆其他信息。
最幸运的是 Liferay 将所有这些数据存储在它的数据库中。
就像 Liferay 存储在数据库中的几乎所有信息一样,这些信息可以通过 LiferayService 访问。
这些服务附带了一堆实用方法。
- 一些,非常基础(像一个高级的 crud)
- 一些更具体的当前服务。
在我的代码示例中,由于通过 portletId
搜索 portlet 的方法,我得到了 portlet 对象。此 portletId
不是表 Portlet
的数据库主键。
因此,Liferay 自己负责关联 portletId
(实际上是名称),而不是强制询问主键,它是一个无意义的数字 long
.
您可以使用其他方法来获取 portlet 的对象,因为 PortletLocalServiceUtil 有许多可用的查找器。
我强烈建议您在任何提供“大纲”的 IDE 中检查文件 $LIFERAY_PORTAL_SRC/portal-service/src/com/liferay/portal/service/PortletLocalServiceUtil.java
查看以更好地了解它的所有可能性。
结论:
希望这有帮助;)
关于xml - 如何在 liferay 中获取 portlet.xml 的初始化参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14618924/