在 liferay 中编辑消息 portlet
Edit message portlet in liferay
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.ReadOnlyException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ValidatorException;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class GreetingMessage extends MVCPortlet {
public static final String GREETING = "greeting";
public static final String DEFAULT_GREETING = "Hello! It's my default greeting message";
@Override
public void render(RenderRequest request, RenderResponse response)
throws IOException, PortletException {
PortletPreferences preferences = request.getPreferences();
request.setAttribute(GREETING,
preferences.getValue(GREETING, DEFAULT_GREETING));
super.render(request, response);
}
public void updateGreeting(ActionRequest request, ActionResponse response)
throws ValidatorException, IOException, ReadOnlyException {
String greeting = request.getParameter("greeting");
PortletPreferences prefs = request.getPreferences();
if (greeting != null) {
prefs.setValue(GREETING, greeting);
prefs.store();
request.setAttribute(GREETING, greeting);
}
}
}
view.jsp:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<p>${greeting}</p>
<portlet:renderURL var="editGreetingURL">
<portlet:param name="mvcPath" value="/html/greetingmessage/edit.jsp"/>
</portlet:renderURL>
<p><a href="${editGreetingURL}">Edit greeting</a></p>
edit.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ page import="javax.portlet.PortletPreferences" %>
<portlet:defineObjects />
<portlet:actionURL name="updateGreeting" var="updateGreetingURL">
</portlet:actionURL>
<aui:form action="<%= updateGreetingURL %>" method="post">
<aui:input label="greeting" name="greeting" type="text" value="${greeting}" />
<aui:button type="submit" />
</aui:form>
<portlet:renderURL var="viewGreetingURL">
<portlet:param name="mvcPath" value="/html/greetingmessage/view.jsp" />
</portlet:renderURL>
<p><a href="${viewGreetingURL}">← AND NOW IT'S BACK</a></p>
这是我的 "edit-greeting" portlet 的测试代码。问题是,如何进行本地化???我已经阅读了很多文档,但这都是徒劳的。我在 WEB-INF 文件夹中创建 src/language.properties 和 src/language_es.properties。接下来我该怎么办?请帮帮我。 @Shivam
回答你的问题
1)您可以在渲染方法中处理您的属性和 portlet 首选项,并将它们设置为渲染请求中的属性,随后可以通过一些脚本语言(如 jstl)在您的 jsp 中读取它们
2) portlet.xml 文件无需更改。
init params 顾名思义就是添加了初始化portlet视图所需的一些参数。
您需要对渲染方法进行以下更改
public void render(RenderRequest req,RenderResponse res) throws IOException, PortletException
{
String greeting = req.getParameter("greeting");
PortletPreferences prefs = req.getPreferences();
String defaultGreeting="Hello! Welcome to our portalOLOLOLOLOL.";
if(prefs.getValue("greeting","true")==null)
{
prefs.setValue("greeting", defaultGreeting);
}
if (greeting != null)
{
prefs.setValue("greeting", greeting);
prefs.store();
req.setAttribute("greeting", prefs.getValue("greeting","true"));
}
else
{
req.setAttribute("greeting", prefs.getValue("greeting","true"));
}
super.render(req,res);
}
view.jsp 和 edit.jsp 中不需要任何更改(除了删除代码),因此我忘了提及。
至于 render 方法,最好的方法肯定是使用 action url 并使用 action 方法,但是由于您似乎正在尝试一些方法并对您的方法进行最小的更改,所以我保留了它 render只要。
至于代码,prefs.getValue("greeting","true")
检查某个属性是否存在于 portlet 首选项中。
已更新流程操作
public class NewPortlet 扩展了 MVCPortlet {
public static final String GREETING="greeting";
@Override
public void render(RenderRequest req,RenderResponse res) throws IOException, PortletException
{
PortletPreferences prefs = req.getPreferences();
String defaultGreeting="Hello! Welcome to our portalOLOLOLOLOL.";
if(prefs.getValue(GREETING,"true")==null)
{
prefs.setValue(GREETING, defaultGreeting);
prefs.store();
}
req.setAttribute(GREETING, prefs.getValue(GREETING,"true"));
super.render(req,res);
}
public void updateGreeting(ActionRequest req,ActionResponse res) throws ValidatorException, IOException, ReadOnlyException
{
String greeting = req.getParameter("greeting");
PortletPreferences prefs = req.getPreferences();
if (greeting != null)
{
prefs.setValue(GREETING, greeting);
prefs.store();
req.setAttribute(GREETING, greeting);
}
}
}
编辑中更新jsp
<portlet:actionURL name="updateGreeting" var="updateGreetingURL">
</portlet:actionURL>
<aui:form action="<%= updateGreetingURL %>" method="post">
<aui:input label="greeting" name="greeting" type="text" value="${greeting}" />
<aui:button type="submit" />
</aui:form>
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.ReadOnlyException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ValidatorException;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class GreetingMessage extends MVCPortlet {
public static final String GREETING = "greeting";
public static final String DEFAULT_GREETING = "Hello! It's my default greeting message";
@Override
public void render(RenderRequest request, RenderResponse response)
throws IOException, PortletException {
PortletPreferences preferences = request.getPreferences();
request.setAttribute(GREETING,
preferences.getValue(GREETING, DEFAULT_GREETING));
super.render(request, response);
}
public void updateGreeting(ActionRequest request, ActionResponse response)
throws ValidatorException, IOException, ReadOnlyException {
String greeting = request.getParameter("greeting");
PortletPreferences prefs = request.getPreferences();
if (greeting != null) {
prefs.setValue(GREETING, greeting);
prefs.store();
request.setAttribute(GREETING, greeting);
}
}
}
view.jsp:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<p>${greeting}</p>
<portlet:renderURL var="editGreetingURL">
<portlet:param name="mvcPath" value="/html/greetingmessage/edit.jsp"/>
</portlet:renderURL>
<p><a href="${editGreetingURL}">Edit greeting</a></p>
edit.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ page import="javax.portlet.PortletPreferences" %>
<portlet:defineObjects />
<portlet:actionURL name="updateGreeting" var="updateGreetingURL">
</portlet:actionURL>
<aui:form action="<%= updateGreetingURL %>" method="post">
<aui:input label="greeting" name="greeting" type="text" value="${greeting}" />
<aui:button type="submit" />
</aui:form>
<portlet:renderURL var="viewGreetingURL">
<portlet:param name="mvcPath" value="/html/greetingmessage/view.jsp" />
</portlet:renderURL>
<p><a href="${viewGreetingURL}">← AND NOW IT'S BACK</a></p>
这是我的 "edit-greeting" portlet 的测试代码。问题是,如何进行本地化???我已经阅读了很多文档,但这都是徒劳的。我在 WEB-INF 文件夹中创建 src/language.properties 和 src/language_es.properties。接下来我该怎么办?请帮帮我。 @Shivam
回答你的问题 1)您可以在渲染方法中处理您的属性和 portlet 首选项,并将它们设置为渲染请求中的属性,随后可以通过一些脚本语言(如 jstl)在您的 jsp 中读取它们 2) portlet.xml 文件无需更改。 init params 顾名思义就是添加了初始化portlet视图所需的一些参数。
您需要对渲染方法进行以下更改
public void render(RenderRequest req,RenderResponse res) throws IOException, PortletException
{
String greeting = req.getParameter("greeting");
PortletPreferences prefs = req.getPreferences();
String defaultGreeting="Hello! Welcome to our portalOLOLOLOLOL.";
if(prefs.getValue("greeting","true")==null)
{
prefs.setValue("greeting", defaultGreeting);
}
if (greeting != null)
{
prefs.setValue("greeting", greeting);
prefs.store();
req.setAttribute("greeting", prefs.getValue("greeting","true"));
}
else
{
req.setAttribute("greeting", prefs.getValue("greeting","true"));
}
super.render(req,res);
}
view.jsp 和 edit.jsp 中不需要任何更改(除了删除代码),因此我忘了提及。
至于 render 方法,最好的方法肯定是使用 action url 并使用 action 方法,但是由于您似乎正在尝试一些方法并对您的方法进行最小的更改,所以我保留了它 render只要。
至于代码,prefs.getValue("greeting","true")
检查某个属性是否存在于 portlet 首选项中。
已更新流程操作
public class NewPortlet 扩展了 MVCPortlet {
public static final String GREETING="greeting";
@Override
public void render(RenderRequest req,RenderResponse res) throws IOException, PortletException
{
PortletPreferences prefs = req.getPreferences();
String defaultGreeting="Hello! Welcome to our portalOLOLOLOLOL.";
if(prefs.getValue(GREETING,"true")==null)
{
prefs.setValue(GREETING, defaultGreeting);
prefs.store();
}
req.setAttribute(GREETING, prefs.getValue(GREETING,"true"));
super.render(req,res);
}
public void updateGreeting(ActionRequest req,ActionResponse res) throws ValidatorException, IOException, ReadOnlyException
{
String greeting = req.getParameter("greeting");
PortletPreferences prefs = req.getPreferences();
if (greeting != null)
{
prefs.setValue(GREETING, greeting);
prefs.store();
req.setAttribute(GREETING, greeting);
}
}
}
编辑中更新jsp
<portlet:actionURL name="updateGreeting" var="updateGreetingURL">
</portlet:actionURL>
<aui:form action="<%= updateGreetingURL %>" method="post">
<aui:input label="greeting" name="greeting" type="text" value="${greeting}" />
<aui:button type="submit" />
</aui:form>