CAF Portlet Annotations
When a portlet action request is being handled, the framework will first look for a matching method that contains the @PortletAction annotation in your portlet preferences bean. If it does not find a match there it will try to find a match in the page bean for whatever is currently the active page of the portlet.
@PortletAction. When attached to an action handler method, this indicates that the method is a portlet action, meaning that it can be invoked as the target action of a portlet URL
Pertinent code is highlighted in bold.
Example:
package caf.war.testapp1.testportletaction;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.WindowState;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import com.webmethods.caf.faces.annotations.ExpireWithPageFlow;
import com.webmethods.caf.faces.annotations.DTManagedBean;
import com.webmethods.caf.faces.annotations.BeanType;
import com.webmethods.caf.faces.bean.portlet.PortletAction;
import com.webmethods.caf.portlet.IPortletURL;
import com.webmethods.caf.portlet.PortletURLFactory;
/**
* Sample demonstrating methods annotated as a PortletAction meaning
* that the methods can be invoked as the target action of a portlet url.
*/
@ManagedBean(name = "TestPortletAction")
@SessionScoped
@ExpireWithPageFlow
@DTManagedBean(displayName = "TestPortletAction", beanType = BeanType.PORTLET)
public class TestPortletAction extends
com.webmethods.caf.faces.bean.BaseFacesPreferencesBean {
private transient caf.war.searchapp1.Searchapp1 searchapp1 = null;
/**
* List of portlet preference names
*/
public static final String[] PREFERENCES_NAMES = new String[] {
"pref1"
};
/**
* Create new preferences bean with list of preference names
*/
public TestPortletAction() {
super(PREFERENCES_NAMES);
}
/**
* Sample of building a portlet action url that can be bound
* as the href of a link to run the action when the link is
* clicked.
*
* @return href for a link that runs the action
*/
public String getActionOneLink() { String href = null;
try {
String targetPageAlias = "targetPage"; // Base URL -- target page alias
String targetPortletAlias = getPortletURI(); // Portlet alias or uri to
target
IPortletURL targetPageURL =
PortletURLFactory.createActionUrl(getFacesContext());
//start with clean URL state (omit to keep the state)
targetPageURL.clearState();
targetPageURL.clearParameters();
//configure the page to start on (omit to stay on the same page)
targetPageURL.setBaseURL( targetPageAlias );
//associate the target component for the action
targetPageURL.setPortlet(targetPortletAlias);
//(optionally) pass additional parameters to the target portlet
targetPageURL.setParameter( "pref1", "new_value1" );
//define the action to run in the target portlet
targetPageURL.setTargetAction( "doActionOne" );
//add the Anti-Cross-Site-Request-Forgery-Token when required
targetPageURL.setAXSRFT( true );
//(optinally) add other options for the target portlet
targetPageURL.setWindowState( WindowState.NORMAL );
targetPageURL.setPortletMode( PortletMode.VIEW );
//build the href from the provided details
href = targetPageURL.toString();
} catch (Exception e) {
error(e);
}
return href;
}
/**
* Portlet action handler.Since it is not otherwise
* specified, the portlet action requires (by default) the
* Anti-Cross-Site-Request-Forgery-Token to be present on
* the action request.
*/
@PortletAction
public String doActionOne() {
//TODO: do your action logic here
return null;
}
/**
* Another portlet action handler.This one does not
* require the Anti-Cross-Site-Request-Forgery-Token
* to be present before running.
*/
@PortletAction(axsrft=false)
public String doActionTwo()
{//TODO: do your action logic here
return null ;
}
/**
* Call this method in order to persist
* portlet preferences
*/
public void storePreferences() throws Exception {
updatePreferences();
PortletPreferences preferences = getPreferences();
preferences.store();
}
public caf.war.searchapp1.Searchapp1 getSearchapp1() {
if (searchapp1 == null) {
searchapp1 =
(caf.war.searchapp1.Searchapp1)resolveExpression("#{Searchapp1}");
}
return searchapp1;
}
/**
* The algorithm for this 'smart' preference getter is:
* 1) Check the Request Map (skip this step if it isn't a 'smart' preference)
* 2) Check the Member variable
* 3) Fall back to the PortletPreferences
*/
public String getPref1() throws Exception {
return (String) getPreferenceValue("pref1", String.class);
}
/**
* Invoke {@link #storePreferences} to persist these changes
*/
public void setPref1(String pref1) throws Exception {
setPreferenceValue("pref1", pref1);
}
Related Topics