The use of portletpreferences in Liferay

Source: Internet
Author: User

In Liferay, if you can use good portletpreferences then it will be very powerful:

Brief introduction:

Portletpreferences is a combination of a set of information related to the current user, and we can allow a logged on user to share data through this portletpreferences on different pages of a portlet, at different stages, Without having to worry about other users stealing the data. The benefit of using this shared data is that it does not require specific entity storage, such as databases, files.

Portletpreferences is divided into 2 kinds, one is static, one is dynamic.

The static portletpreference is written in the Portlet.xml file, as follows:

<portlet-preferences>  
 <preference> 
      <name>department</name> 
      <value> liferay</value> 
   </preference> 
     
    <preference>        
<name>company</name> 
      <value>cignex</value> 
   </preference> 
</portlet-preferences>

In the page or in Java code, we can use the following code to get:

Portletpreferences preferences = Renderrequest.getpreferences (); 
String Department = preferences.getvalue ("Departmentt", "");

Dynamic portletpreferences is much more powerful, for example, we need to set some parameters in the Portlet's configure mode, such as rest request URL, and some header parameters, And then we're going to read this information in view mode.

Well, we can do this:

First, customize a helper class, called Portlethelper, which defines a static method called Getportletpreference ():

public static portletpreferences getportletpreferences (Actionrequest actionrequest) 

throws Portalexception, systemexception{ 
portletpreferences preferences = Actionrequest.getpreferences (); 
String Portletresource = paramutil.getstring (actionrequest, "Portletresource"); 
if (Validator.isnotnull (Portletresource)) { 
 preferences = Portletpreferencesfactoryutil.getportletsetup ( Actionrequest, Portletresource); 
return preferences; 
 }

Then we create a new Portletpreferences object in config mode and populate it with the necessary content:

public void Processaction (Portletconfig portletconfig, Actionrequest actionrequ EST, actionresponse actionresponse) throws Exception {if (Logger.isdebugenabled ()) 
     
             {Logger.debug ("configurationaction:processaction ()"); 
     
          
     
     } portletpreferences Preferences = Portlethelper.getportletpreferences (actionrequest); 
     
          try{fileoperationhelper.uploadfiletodest (actionrequest); Preferences.setvalue (Rs_launch_portlet_preference_html_url, Configurationactionhelper.gethtmlurl (actionRequest) 
     
          ); Preferences.setvalue (Rs_launch_portlet_preference_rest_ws_url, Configurationactionhelper.getresturl ( 
     
          Actionrequest)); Preferences.setvalue (Rs_launch_portlet_preference_rest_ws_type, Configurationactionhelper.getresttype ( 
     
          Actionrequest)); PreferEnces.setvalue (Service_version, Configurationactionhelper.getrestversion (actionrequest)); 
     
         Preferences.setvalue (Service_env, Configurationactionhelper.getrestenv (actionrequest)); 
     
          Preferences.setvalue (Consumer_auth_token, Configurationactionhelper.getresttoken (actionRequest)); 
     
          Preferences.setvalue (consumer_id, Configurationactionhelper.getrestconid (actionrequest)); 
Preferences.store (); ..

Finally, we use the content stored in Portletpreferences in view mode:

@RenderMode (name = rs_launch_portlet_render_mode_view) public void VIEW (Renderrequest renderrequest, Renderre 
                          Sponse renderresponse) throws Portletexception, IOException, Portalexception, 
      
                 SystemException {String jsp = null; 
                 if (logger.isdebugenabled ()) {Logger.debug ("Rslaunchportlet:view ()"); 
      
                 } portlethelper.createactionurls (Renderrequest, renderresponse); String Portletinstanceid = (string) renderrequest. getattribute (rs_launch_portlet_rend 
      
                 ER_REQUEST_ATTRIBUTE_PORTLET_ID); 
                 Portletpreferences preferences = Renderrequest.getpreferences (); String Html_url = Preferences.getvalue (Rs_launch_portlet_preference_html_url, STRINGP Ool. 
                 Double_quote); String Rest_url = Preferences.getvalue (Rs_launch_portlet_preference_rest_ws_url,
    
Stringpool.double_quote); ..

This achieves the ability to share part of the data within a portlet specified by a user, or is simple.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.