Instead of behind closed doors, it is better to start the original, import, free Ferrari. --Wang Xin
Not much to say nonsense, directly on the code.
Paramutil
Paramutil and Getterutil are Liferay's most important helper classes.
- Paramutil use: Gets the parameter value of the Portletrequest, and makes the type conversion and the empty processing (the internal mechanism is through the Getterutil)
- Getterutil use: Convert input to type, empty processing, give default value (that is, the initial value of the expected get value is empty)
Example of use of Paramutil 1:
Public void Deleteinterview ( actionrequest actionrequest, Actionresponse actionresponse) throws Exception { longparamutil"interviewid"); Interviewlocalserviceutil.deleteinterview (INTERVIEWID); Sendredirect (Actionrequest, actionresponse);}
Example of use of Paramutil 2:
Public voidupdatequestion (actionrequest actionrequest, Actionresponse actionresponse) throws Exception {LongQuestionID = Paramutil.getlong (Actionrequest,"QuestionID"); LongQuestionsetid = Paramutil.getlong (Actionrequest,"Questionsetid"); String title= Paramutil.getstring (Actionrequest,"title"); String Description= Paramutil.getstring (Actionrequest,"Description"); intType = Paramutil.getinteger (Actionrequest,"type"); Servicecontext Servicecontext=servicecontextfactory.getinstance (actionrequest); Try { if(QuestionID <=0{questionlocalserviceutil.addquestion (Questionsetid, title, description, type, Serviceco ntext); } Else{questionlocalserviceutil.updatequestion (QuestionID, title, description, type, Servicecon Text); } } Catch(Exception e) {...} Sendredirect (Actionrequest, actionresponse);}
Paramutil can obtain portletrequest parameters directly through get, such as:
Public Static Short Get(portletrequest portletrequest, String param, ShortDefaultValue) { returnGetterutil.Get(Portletrequest.getparameter (param), DefaultValue); } Public StaticStringGet(portletrequest portletrequest, string param, String defaultvalue) {string returnvalue=Getterutil.Get(Portletrequest.getparameter (param), DefaultValue); if(ReturnValue! =NULL) { returnReturnvalue.trim (); } return NULL; }
It is also true that methods like Getinteger, Getlong get a single value, and get an array of getintegervalues, getlongvalues, and so on, are actually the same for the commit of a checkbox.
The difference is that the return value of Getintegervalues, Getlongvalues is by calling Getterutil
Public Static intGetinteger (httpservletrequest request, String param) {returnGetterutil.getinteger (Request.getparameter (param)); } Public Static intGetinteger (httpservletrequest request, String param,intDefaultValue) { return Get(Request, Param, DefaultValue); } Public Static int[] getintegervalues (httpservletrequest request, String param) {returnGetintegervalues (Request, Param,New int[0]); } Public Static int[] getintegervalues (httpservletrequest request, String param,int[] defaultvalue) { returngetterutil.getintegervalues (request.getparametervalues (param), DefaultValue); }
The output of the parameter can also be printed with print to make it easy for programmers to use
Public Static voidprint (Portletrequest portletrequest) {enumeration<String> ENU =Portletrequest.getparameternames (); while(Enu.hasmoreelements ()) {String param=enu.nextelement (); String[] Values=portletrequest.getparametervalues (param); for(inti =0; i < values.length; i++) {System. out. println (param +"["+ i +"] = "+Values[i]); } } }
Additional servicecontext parameters are added to the higher version
Static float float staticfloatfloat
Getterutil
Getterutil code, it does not specifically explain the
You can access:
Https://docs.liferay.com/portal/5.1/javadocs/portal-kernel/com/liferay/portal/kernel/util/GetterUtil.java.html
Getterutil Use Example: Send request attribute to JSP
Java:
Package Com.liferay.docs.exampleserviceconsumerportlet;import Java.io.ioexception;import Javax.portlet.portlet;import Javax.portlet.portletexception;import Javax.portlet.renderrequest;import Javax.portlet.renderresponse;import Org.osgi.service.component.annotations.component;import Org.osgi.service.component.annotations.reference;import Com.liferay.portal.kernel.portlet.bridges.mvc.mvcportlet;import Com.liferay.portal.service.UserLocalService; Import Com.liferay.bookmarks.service.BookmarksFolderLocalService; @Component (Immediate=true, Property= { "Com.liferay.portlet.display-category=category.sample", "com.liferay.portlet.instanceable=true", "javax.portlet.display-name=example Service Consumer portlet", "javax.portlet.init-param.template-path=/", "javax.portlet.init-param.view-template=/view.jsp", "Javax.portlet.security-role-ref=power-user,user"}, Service= Portlet.class) Public classExampleserviceconsumerportlet extends Mvcportlet {@Override Public voidDoview (renderrequest request, renderresponse response) throws IOException, portletexception {intUserCount =Getuserlocalservice (). Getuserscount (); Request.setattribute ("User_count", UserCount); intBookmarksfoldercount =Getbookmarksfolderlocalservice (). Getbookmarksfolderscount (); Request.setattribute ("Bookmarks_folder_count", Bookmarksfoldercount); Super.doview (request, response); } PublicBookmarksfolderlocalservice Getbookmarksfolderlocalservice () {return_bookmarksfolderlocalservice; } PublicUserlocalservice Getuserlocalservice () {return_userlocalservice; } @Reference Public voidSetbookmarksfolderlocalservice (Bookmarksfolderlocalservice bookmarksfolderlocalservice) {_bookmar Ksfolderlocalservice=Bookmarksfolderlocalservice; } @Reference Public voidSetuserlocalservice (Userlocalservice userlocalservice) {_userlocalservice=Userlocalservice; } PrivateUserlocalservice _userlocalservice; Privatebookmarksfolderlocalservice _bookmarksfolderlocalservice;}
Jsp:
<%@ taglib URI="Http://java.sun.com/portlet_2_0"prefix="Portlet" %><%@ Page Import="Com.liferay.portal.kernel.util.GetterUtil" %><portlet:defineobjects/><%intUserCount=Getterutil.getinteger (Renderrequest.getattribute ("User_count"));intBookmarksfoldercount=Getterutil.getinteger (Renderrequest.getattribute ("Bookmarks_folder_count"));%><P>The portal has<%=UserCount%>Users.</P><P>The portal has<%=Bookmarksfoldercount%>Bookmarks folders.</P>
The difference between Getterutil and Paramutil is that Paramutil is a dedicated class for parsing requests, and getterutil is a generic type conversion output class
The benefits of using these two classes Paramutil and Getterutil are obvious, and if you mate with validator classes, large pieces of code like empty, type conversion, and validation can be greatly omitted, very concise
Liferay7 BPM Portal Development 29: Generic Help Class Paramutil, getterutil using the core Kernel.util package