我在EXT中添加自定义方法时遇到了问题。 这是我的代码,
public class MyBlogsEntryServiceImpl extends BlogsEntryServiceImpl{
protected static final String DISPLAY_STYLE_VIEWS = "views";
protected static final String DISPLAY_STYLE_AVATAR = "avatar";
public List<BlogsEntry> getCompanyEntriesByViews(
long companyId, Date displayDate, int status, int max)
throws PortalException, SystemException {
long blogsEntryClassNameId = ClassNameLocalServiceUtil.getClassNameId(BlogsEntry.class);
DynamicQuery blogsEntriesQuery = DynamicQueryFactoryUtil.forClass(BlogsEntry.class, "BlogsEntry")
.setProjection(ProjectionFactoryUtil.property("BlogsEntry.entryId"))
.add(PropertyFactoryUtil.forName("BlogsEntry.companyId").eq(companyId));
DynamicQuery mostViewedAssetsQuery = DynamicQueryFactoryUtil.forClass(AssetEntry.class, "AssetEntry")
.add(PropertyFactoryUtil.forName("AssetEntry.classPK").in(blogsEntriesQuery))
.add(PropertyFactoryUtil.forName("AssetEntry.classNameId").eq(blogsEntryClassNameId))
.addOrder(OrderFactoryUtil.desc("AssetEntry.viewCount"));
if (max > 0) {
mostViewedAssetsQuery.setLimit(0, max);
}
List<AssetEntry> assetEntries = AssetEntryLocalServiceUtil.dynamicQuery(mostViewedAssetsQuery);
List<BlogsEntry> blogsEntries = new ArrayList<BlogsEntry>(assetEntries.size());
for (AssetEntry asset : assetEntries) {
try {
BlogsEntry entry = blogsEntryLocalService.getBlogsEntry(asset.getClassPK());
if (BlogsEntryPermission.contains(getPermissionChecker(), entry, ActionKeys.VIEW)) {
blogsEntries.add(entry);
}
} catch (Exception e) {
_log.warn("Cannot find BlogsEntry with id " + asset.getClassPK());
}
}
return blogsEntries;
}
}
&#13;
在上面的代码中,我得到了getCompanyEntriesByViews()方法的异常, 这是错误日志
05:22:29,156 INFO [localhost-startStop-1][DialectDetector:71] Determine dialect for MySQL 5
05:22:29,281 INFO [localhost-startStop-1][DialectDetector:136] Found dialect org.hibernate.dialect.MySQLDialect
Starting Liferay Portal Community Edition 6.2 CE GA2 (Newton / Build 6201 / March 20, 2014)
05:22:52,093 INFO [localhost-startStop-1][BaseDB:484] Database does not support case sensitive queries
05:22:52,546 INFO [localhost-startStop-1][ServerDetector:119] Server supports hot deploy
05:22:52,546 INFO [localhost-startStop-1][PluginPackageUtil:1013] Reading plugin package for the root context
05:25:23,250 INFO [localhost-startStop-1][AutoDeployDir:139] Auto deploy scanner started for E:\liferay62-migration\liferay-portal-6.2-ce-ga2\deploy
05:25:23,828 ERROR [localhost-startStop-1][JSONWebServiceRegistrator:96] java.lang.IllegalArgumentException: java.lang.NoSuchMethodException: com.sun.proxy.$Proxy153.getCompanyEntriesByViews(long, java.util.Date, int, int)
java.lang.IllegalArgumentException: java.lang.NoSuchMethodException: com.sun.proxy.$Proxy153.getCompanyEntriesByViews(long, java.util.Date, int, int)
at com.liferay.portal.jsonwebservice.JSONWebServiceActionConfig.<init>(JSONWebServiceActionConfig.java:79)
at com.liferay.portal.jsonwebservice.JSONWebServiceActionsManagerImpl.registerJSONWebServiceAction(JSONWebServiceActionsManagerImpl.java:269)
at com.liferay.portal.kernel.jsonwebservice.JSONWebServiceActionsManagerUtil.registerJSONWebServiceAction(JSONWebServiceActionsManagerUtil.java:92)
at com.liferay.portal.jsonwebservice.JSONWebServiceRegistrator.registerJSONWebServiceAction(JSONWebServiceRegistrator.java:261)
at com.liferay.portal.jsonwebservice.JSONWebServiceRegistrator.onJSONWebServiceBean(JSONWebServiceRegistrator.java:195)
at com.liferay.portal.jsonwebservice.JSONWebServiceRegistrator.processBean(JSONWebServiceRegistrator.java:93)
at com.liferay.portal.jsonwebservice.JSONWebServiceRegistrator.processAllBeans(JSONWebServiceRegistrator.java:68)
at com.liferay.portal.jsonwebservice.JSONWebServiceActionsManagerImpl.registerServletContext(JSONWebServiceActionsManagerImpl.java:324)
at com.liferay.portal.kernel.jsonwebservice.JSONWebServiceActionsManagerUtil.registerServletContext(JSONWebServiceActionsManagerUtil.java:102)
at com.liferay.portal.events.GlobalStartupAction.run(GlobalStartupAction.java:317)
at com.liferay.portal.events.EventsProcessorImpl.processEvent(EventsProcessorImpl.java:108)
at com.liferay.portal.events.EventsProcessorImpl.process(EventsProcessorImpl.java:59)
at com.liferay.portal.events.EventsProcessorUtil.process(EventsProcessorUtil.java:32)
at com.liferay.portal.servlet.MainServlet.processGlobalStartupEvents(MainServlet.java:1038)
at com.liferay.portal.servlet.MainServlet.init(MainServlet.java:314)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:656)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1635)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodException: com.sun.proxy.$Proxy153.getCompanyEntriesByViews(long, java.util.Date, int, int)
at java.lang.Class.getMethod(Unknown Source)
at com.liferay.portal.jsonwebservice.JSONWebServiceActionConfig.<init>(JSONWebServiceActionConfig.java:73)
... 31 more
&#13;
我有些怀疑。 1.我们可以在EXT中添加自定义方法吗? 2.如果是,那么上述问题的解决方案是什么。
请给我一些建议。
答案 0 :(得分:1)
如果这意味着将方法添加到现有界面:否。您can't change
一个界面 - API 必须保持稳定 - 考虑一下需要针对 版本的Liferay实现的插件的影响Liferay接口的界面与我的版本。
我会回答这个问题,尽管&#34; No&#34;以上,因为有一个非常简单,甚至可维护良好的解决方案:
您只需使用自己的服务实现一个钩子,例如MyBlogExtension。这可能只有一种方法,例如getCompanyEntriesByViews
。此方法的实现只是委托给Liferay的BlogService。现在你有一个可以使用,维护并且不会破坏众所周知的API的钩子。它与各种应用程序兼容,这就是你想要的。
答案 1 :(得分:0)
我认为这里的问题是Liferay将返回BlogsEntryServiceImpl的实例,而不是MyBlogsEntryServiceImpl。因此NoSuchMethodException上升了。您应该在BlogsEntryServiceImpl中实现该方法,而不是从该类继承。另一件事是,您尝试通过代理对象调用方法。因此,不清楚您正在调用该方法的实际实现。 你也应该考虑钩子方法是否足够。我们在论坛中总是给出的建议是:只有真正使用EXT才真的没有别的办法。 也许你看看这里:
如果您可以通过挂钩实现所需的更改,您应该明确地采用这种方式,因为这不会破坏您的门户网站的更新功能。