com.mizar.faces.ajax.phaseListener
Class AjaxPhaseListener

java.lang.Object
  extended by com.mizar.faces.ajax.phaseListener.AjaxPhaseListener
All Implemented Interfaces:
java.io.Serializable, java.util.EventListener, javax.faces.event.PhaseListener

public class AjaxPhaseListener
extends java.lang.Object
implements javax.faces.event.PhaseListener

The AjaxPhaseListener is designed to handle Ajax Requests that require access to resources of the Faces application. An independent AjaxServletclass is available to handle requests that do not require access to the application.

In addition to being inside the scope of the Faces application this AjaxPhaseListener also has access to the SessionConstants EntityManager, something the AjaxServlet does not. The SessionConstants EntityManager SessionConstants.getCurrentInstance().getEm() returns RowInterface instances for any entity bean that has been registered in the faces-config.xml file SessionConstants managed-bean beanTableMap and beanRowMap Map definitions. Beans identified in these properties are assigned to a RowFactory that returns the implementing RowInterface instance instead of the JPA Entity bean superclass.

From Javascript the function phaseListenerRequest(requestName,message,getResponse) may be used to send requests to this AjaxPhaseListener class. The third argument getResponse should be false if the handler does not return a message and true if the calling Javascript anticipates a response.


 if (getResponse){
 return client.responseText;
 }
 
 
Example Javascript:
 phaseListenerRequest('AddToParcelTable',"pkey="+pkey+"&bean="+theBean,false);
 
Example Handler:
 public class ParcelQuickPickHandler extends AjaxHandler {
 public ParcelQuickPickHandler() {
 }

 public boolean processRequest(HttpServletRequest request, HttpServletResponse response) {
 boolean rtn = false;
 String pkey = request.getParameter("pkey");
 if (StringUtils.isNotEmpty(pkey)) {
 try {
 rtn = true;
 EntityManager em = SessionConstants.getCurrentInstance().getEm();
 if (em != null) {
 //Note: that em.find actually returns a ParcelRow because of the RowFactory used to create the EntityManager.
 ParcelBean row = em.find(ParcelBean.class, pkey);
 if (row != null) {
 ParcelTable.getCurrentInstance().addRow((RowInterface)row);
 }
 }
 } catch (Exception e) {
 System.out.println(e.getLocalizedMessage());
 }
 }
 return rtn;
 }
 }
 

The Ajax Phase Listener must be registered in the WEB-INF/faces-config.xml file.

 <lifecycle>
 <phase-listener>com.mizar.faces.ajax.AjaxPhaseListener</phase-listener>
 </lifecycle>
 

Handlers must be registed in the WEB-INF/faces-config.xml file in the ApplicationConstants eventHandlerNames property

 <managed-property>
 <property-name>eventHandlerNames</property-name>
 <map-entries>
 <key-class>java.lang.String</key-class>
 <value-class>java.lang.String</value-class>
 <map-entry><key>AddToParcelTable</key><value>us.cuyahogacounty.cegisportal.callback.ParcelQuickPickHandler</value></map-entry>
 <map-entry><key>SearchArea</key><value>com.mizar.ajax.servlet.SearchAreaHandler</value></map-entry>
 </map-entries>
 </managed-property>
 

There is always a 'Ping' handler, which be default is @{link PingHandler PingHandler} and There is always a 'SearchArea' handler, which be default is @{link SearchAreaHandler SearchAreaHandler}. Ping is required by the mizarMaps.js in order to keep the main application alive when the recipient is working in the Maps <div> for an extended period of time. You can override these handlers by adding a 'Ping' or 'SearchArea' handler definition to the faces-config.xml as per the example above.

See Also:
ApplicationConstants.getEventHandlers(), PingHandler, Serialized Form

Field Summary
protected  org.apache.commons.logging.Log _Logger
           
 
Constructor Summary
AjaxPhaseListener()
           
 
Method Summary
 void afterPhase(javax.faces.event.PhaseEvent e)
           
 void beforePhase(javax.faces.event.PhaseEvent e)
           
 javax.faces.event.PhaseId getPhaseId()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_Logger

protected org.apache.commons.logging.Log _Logger
Constructor Detail

AjaxPhaseListener

public AjaxPhaseListener()
Method Detail

afterPhase

public void afterPhase(javax.faces.event.PhaseEvent e)
Specified by:
afterPhase in interface javax.faces.event.PhaseListener

beforePhase

public void beforePhase(javax.faces.event.PhaseEvent e)
Specified by:
beforePhase in interface javax.faces.event.PhaseListener

getPhaseId

public javax.faces.event.PhaseId getPhaseId()
Specified by:
getPhaseId in interface javax.faces.event.PhaseListener