Below is the sample code which will give a clear idea about how you can set/get a value from Portlet Preference while developing a portlet using WebSphere Portlet Factory.
Create a LJO(Linked Java Object) builder and copy the below code and replace MY_DEPARTMENT value into the value you want to set & get from Portlet Preference.
########################## Sample Code Starts #############################
public class PortletPreferenceWPFSample {
PortletRequest portletRequest = null;
private static final String MY_DEPARTMENT="myDepartment";
private static final String DEFAULT_MY_DEPARTMENT="";
public void setPreferences(WebAppAccess webAppAccess , String myDepartment)
{
try{
// Getting HttpServletRequest from WebAppAcces object.
HttpServletRequest request = webAppAccess.getHttpServletRequest();
// Type casting into Portlet Request.
portletRequest = (PortletRequest) request.getAttribute(com.bowstreet.adapters.Constants.PORTLET_REQUEST);
javax.portlet.PortletPreferences preferences = portletRequest.getPreferences();
//Storing the myDepartment in to portlet preferences Object.
preferences.setValue(MY_DEPARTMENT,myDepartment);
preferences.store();
}catch(Exception err)
{
err.printStackTrace();
}
}
public String getPreferences(WebAppAccess webAppAccess)
{
HttpServletRequest request = webAppAccess.getHttpServletRequest();
portletRequest = (PortletRequest) request.getAttribute(com.bowstreet.adapters.Constants.PORTLET_REQUEST);
javax.portlet.PortletPreferences preferences = portletRequest.getPreferences();
String myDepartment= preferences.getValue(MY_DEPARTMENT,DEFAULT_MY_DEPARTMENT);
return myDepartment;
}
}
##### Function call to above method from remote place such as method or other LJO class #####
// Calling setPreferences method to store folder path into it. Where PortletPreferenceWPFSample_LJO is the LJO builder name for PortletPreferenceWPFSample class.
webAppAccess.callMethod("PortletPreferenceWPFSample_LJO.setPreferences", myDepartment);
########################## Sample Code Ends #############################
No comments:
Post a Comment