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 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}">← , it's back</a></p>
it's tested code of "edit-greeting" portlet. question is, how can make localization??? i've read lot of docs it's nothing. created in web-inf folder src/language.properties , src/language_es.properties. should next? me please. @shivam
to answer question 1)you can handle attributes , portlet preferences in render method , set them attributes in render request,which can subsequently read in jsp via scripting language jstl 2)there no need make changes in portlet.xml file. init params name suggests added provide params needed initializing portlet view.
you need make below changes render method
public void render(renderrequest req,renderresponse res) throws ioexception, portletexception { string greeting = req.getparameter("greeting"); portletpreferences prefs = req.getpreferences(); string defaultgreeting="hello! welcome 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); }
there not changes required in view.jsp , edit.jsp(apart removing code),hence forgot mention same. render method,the best approach defintely use action url , use action method,but since seems looking try out approach , make mininmum changes your,i have kept render only. code,the prefs.getvalue("greeting","true")
checks whether attribute present in portlet preferences or not.
updated process action
public class newportlet extends 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 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); } }
}
updates in edit 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>
Comments
Post a Comment