在 Liferay 中调用一个方法
invoke a method in Liferay
我使用 Liferay Tomcat bundle 6.2,我使用 Liferay IDE(eclipse)
如何调用 Liferay 中的方法?
我创建了一个 Liferay 插件项目并在 java class 中写入以下代码。但我不知道,如何调用此方法?
我无法在 Liferay 中创建主 class。但是,我想,我可以在 view.jsp 中通过创建一个动作 URL 来调用这个方法,对吗?
你能给我一个示例吗?
谢谢
public class TestLoggerPortlet extends MVCPortlet {
private static final Log logger = LogFactoryUtil.getLog(TestLoggerPortlet.class);
public void addEntry() {
logger.info("This is my message.");
if (logger.isDebugEnabled()) logger.debug("Not always printed.");
}
}
我认为您混淆了 desktop/mobile 应用程序和 Web 应用程序。
在我看来,你必须学习 Java EE 基础知识(浏览器如何向应用程序服务器执行请求,以及服务器如何继续理解请求并将其分派给正确的正确方法class, 等等...它被称为 "servlet lifecycle").
然后应该很容易理解与 portlet lifeycle 的区别(以及 Liferay MVC portlet 如何管理您需要的东西)。
我可以推荐一些有趣的读物(按学习路径顺序):
- http://docs.oracle.com/javaee/6/tutorial/doc/bnafd.html(见生命周期部分);了解各个阶段背后发生的事情的基础知识;
- http://www.opensource-techblog.com/2014/12/introduction-to-portlet-phases-and.html 与 portlet 生命周期的简单比较;
- https://www.liferay.com/it/documentation/liferay-portal/6.0/development/-/ai/portlet-development - Liferay官方开发总介绍
- https://dev.liferay.com/develop/tutorials/-/knowledge_base/6-2/creating-a-liferay-mvc-portlet-project - 您需要了解如何创建简单的 LR mvc portlet 的教程;
- https://www.liferay.com/it/web/meera.success/blog/-/blogs/liferay-mvc-portlet-development-introduction 另一个有趣的教程。
希望对您有所帮助
参见下面的代码。
view.jsp
<portlet:defineObjects />
<portlet:actionURL var="myAction" name="myAction">
<portlet:param name="someTxt" value="Text Message" />
</portlet:actionURL>
<form action="<%= myAction %> method="post" name="fm">
<input name="<portlet:namespace/>txtName" type="text" />
<input type="submit" name="<portlet:namespace/>submit" value="submit"/>
</form>
控制器Class。 TestLoggerPortlet.java
package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserServiceUtil;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
/**
* Portlet implementation class TestLoggerPortlet
*/
public class TestLoggerPortlet extends MVCPortlet {
private static Log _log = LogFactoryUtil.getLog(TestLoggerPortlet.class);
@ProcessAction
public void myAction(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
String var1 = actionRequest.getAttribute("someText");//output -> "Text Message"
String var2 = actionRequest.getAttribute("txtName");//output -> Value of Text field.
sendRedirect(actionRequest, actionResponse);
}
}
我使用 Liferay Tomcat bundle 6.2,我使用 Liferay IDE(eclipse)
如何调用 Liferay 中的方法? 我创建了一个 Liferay 插件项目并在 java class 中写入以下代码。但我不知道,如何调用此方法? 我无法在 Liferay 中创建主 class。但是,我想,我可以在 view.jsp 中通过创建一个动作 URL 来调用这个方法,对吗?
你能给我一个示例吗?
谢谢
public class TestLoggerPortlet extends MVCPortlet {
private static final Log logger = LogFactoryUtil.getLog(TestLoggerPortlet.class);
public void addEntry() {
logger.info("This is my message.");
if (logger.isDebugEnabled()) logger.debug("Not always printed.");
}
}
我认为您混淆了 desktop/mobile 应用程序和 Web 应用程序。
在我看来,你必须学习 Java EE 基础知识(浏览器如何向应用程序服务器执行请求,以及服务器如何继续理解请求并将其分派给正确的正确方法class, 等等...它被称为 "servlet lifecycle").
然后应该很容易理解与 portlet lifeycle 的区别(以及 Liferay MVC portlet 如何管理您需要的东西)。
我可以推荐一些有趣的读物(按学习路径顺序):
- http://docs.oracle.com/javaee/6/tutorial/doc/bnafd.html(见生命周期部分);了解各个阶段背后发生的事情的基础知识;
- http://www.opensource-techblog.com/2014/12/introduction-to-portlet-phases-and.html 与 portlet 生命周期的简单比较;
- https://www.liferay.com/it/documentation/liferay-portal/6.0/development/-/ai/portlet-development - Liferay官方开发总介绍
- https://dev.liferay.com/develop/tutorials/-/knowledge_base/6-2/creating-a-liferay-mvc-portlet-project - 您需要了解如何创建简单的 LR mvc portlet 的教程;
- https://www.liferay.com/it/web/meera.success/blog/-/blogs/liferay-mvc-portlet-development-introduction 另一个有趣的教程。
希望对您有所帮助
参见下面的代码。 view.jsp
<portlet:defineObjects />
<portlet:actionURL var="myAction" name="myAction">
<portlet:param name="someTxt" value="Text Message" />
</portlet:actionURL>
<form action="<%= myAction %> method="post" name="fm">
<input name="<portlet:namespace/>txtName" type="text" />
<input type="submit" name="<portlet:namespace/>submit" value="submit"/>
</form>
控制器Class。 TestLoggerPortlet.java
package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserServiceUtil;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
/**
* Portlet implementation class TestLoggerPortlet
*/
public class TestLoggerPortlet extends MVCPortlet {
private static Log _log = LogFactoryUtil.getLog(TestLoggerPortlet.class);
@ProcessAction
public void myAction(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
String var1 = actionRequest.getAttribute("someText");//output -> "Text Message"
String var2 = actionRequest.getAttribute("txtName");//output -> Value of Text field.
sendRedirect(actionRequest, actionResponse);
}
}