6 Replies Latest reply on Mar 4, 2010 12:49 AM by ajaykadiyala

    How to implment polymorphism in JSF/Richfaces.

      I have one TopNavigation Interface

      TopNavigation.java

      interface  TopNavigation
      {
      public void newAction()
      {

      }
      public void viewAction()
      {

      }

      public void modifyAction()
      {

      }

      public void searchAction()
      {

      }

      public void saveAction() {

      }

      public void cancelAction()
      {

      }
      public void deleteAction()
      {

      }
      }
      ---------------------------------------------------------------------------
      1.a) Implementation Class
      public class PhoneTypesTopNavigationImpl implements TopNavigation
      {
      public void newAction()
      {
        // some logic
           return("-Phone-new-");
      }
      public void viewAction()
      {
        // some logic
           return("Phone-view-");
      }

      public void modifyAction()
      {
          // some logic
        return("Phone-modify-");
      }

      public void searchAction()
      {
          // some logic
        return("Phone-search-");

      }

      public void saveAction() {
          // some logic

        return("Phone-save-");

      }

      public void cancelAction()
      {
          // some logic

        return("Phone-cancel-");

      }
      public void deleteAction()
      {  // some logic

        return("Phone-delete-");

      }
      }

      1.b) Usage of implmentation class PhoneTypes
      public class PhoneTypes
      {
      public PhoneTypes()
      {
        TopNavigation ref= new PhoneTypesTopNavigationImpl();
        // Q1. I want to call the save when your clicked it's save button like ref.
        //Ex. ref.save(); How to get the reference in .xhtml file
      }
      }
      1.c)
      PhoneTypes.xtml

      <h:commandbutton action=#{ref.newAction()} />
      <h:commandbutton action=#{ref.viewAction()} />
      <h:commandbutton action=#{ref.modifyAction()} />
      <h:commandbutton action=#{ref.searchAction()} />
      <h:commandbutton action=#{ref.saveAction()} />
      <h:commandbutton action=#{ref.cancelAction()} />
      <h:commandbutton action=#{ref.deleteAction()} />

      2.a) Implementation Class
      public class CellTypesTopNavigationImpl implements TopNavigation
      {
      public void newAction()
      {// some logic
           return("-Cell-new-");
      }
      public void viewAction()
      {// some logic

           return("Cell-view-");
      }

      public void modifyAction()
      {// some logic
        return("Cell-modify-");
      }

      public void searchAction()
      {// some logic
        return("Cell-search-");

      }

      public void saveAction() {// some logic
        return("Cell-save-");

      }

      public void cancelAction()
      {// some logic
        return("Cell-cancel-");

      }
      public void deleteAction()
      {// some logic
        return("Cell-delete-");

      }
      }

      2.b) Usage of implmentation class CellTypes
      public class  CellTypes
      {
      public  CellTypes()
      {
        TopNavigation ref= new  CellTypesTopNavigationImpl();

      }
      }
      2.c) CellTypes.xtml //when user click on it's need call specify

      <h:commandbutton action=#{ref.newAction()} />
      <h:commandbutton action=#{ref.viewAction()} />
      <h:commandbutton action=#{ref.modifyAction()} />
      <h:commandbutton action=#{ref.searchAction()} />
      <h:commandbutton action=#{ref.saveAction()} />
      <h:commandbutton action=#{ref.cancelAction()} />
      <h:commandbutton action=#{ref.deleteAction()} />

      Q.2) Please sugest for face-config.xml as well

      ------------------------------------------------------------------------------------------

        • 1. Re: How to implment polymorphism in JSF/Richfaces.
          ilya_shaikovsky

          register both PhoneTypesTopNavigationImpl and CellTypesTopNavigationImpl as beans in faces config as usually.

           

          then using facelets:

           

          included page inlude.xhtml contains:

          ...
          <h:commandButton action="#{viewController.newAction}">
          ...
          

           

          first page which includes this include.xhtml

           

          <ui:include src="templates/includes/include.xhtml">
               <ui:param name="viewController" value="phoneTypesTopNavigationBean">
          <ui:include src="templates/includes/include.xhtml">
          

          second page which performs the same include but in the other context:

           

          <ui:include src="templates/includes/include.xhtml">
               <ui:param name="viewController" value="cellTypesTopNavigationBean">
          <ui:include src="templates/includes/include.xhtml">
          

           

          So you using different beans in the same include.

          • 2. Re: How to implment polymorphism in JSF/Richfaces.

            Hi Ilya Shaikovsky ,

             

            Please help me the code for PhoneTypesTopNavigationImpl and PhoneTypesTopNavigationBean in face-config.xml as well PhoneTypesTopNavigationImpl.java and PhoneTypesTopNavigationBean.java.(Is it both are same PhoneTypesTopNavigationBean and PhoneTypesTopNavigationImpl).

            • 3. Re: How to implment polymorphism in JSF/Richfaces.
              ilya_shaikovsky
               <managed-bean>
                <managed-bean-name>phoneTypesTopNavigationBean</managed-bean-name>
                <managed-bean-class>org.test.PhoneTypesTopNavigationImpl</managed-bean-class>
                <managed-bean-scope>session</managed-bean-scope>
               </managed-bean>
               <managed-bean>
                <managed-bean-name>cellTypesTopNavigationBean</managed-bean-name>
                <managed-bean-class>org.test.CellTypesTopNavigationImpl</managed-bean-class>
                <managed-bean-scope>session</managed-bean-scope>
               </managed-bean>
               
               
              
              
              
              • 4. Re: How to implment polymorphism in JSF/Richfaces.

                Hi Ilya Shaikovsky ,

                 

                I have implemented as your suggestion but gett following error ,please do the needful help.

                 

                 

                type Exception report

                message

                description The server encountered an internal error () that prevented it from fulfilling this request.

                exception

                javax.servlet.ServletException: javax.servlet.ServletException: #{viewController.newAction}: javax.el.MethodNotFoundException: /examples/Include.xhtml @8,70 action="#{viewController.newAction}": Method not found: phoneTypesTopNavigationBean.newAction()
                com.test.lbs.presentation.filter.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:75)


                root cause

                javax.servlet.ServletException: #{viewController.newAction}: javax.el.MethodNotFoundException: /examples/Include.xhtml @8,70 action="#{viewController.newAction}": Method not found: phoneTypesTopNavigationBean.newAction()
                javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
                org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
                org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
                org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
                org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
                com.test.lbs.presentation.filter.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:48)


                root cause

                javax.faces.FacesException: #{viewController.newAction}: javax.el.MethodNotFoundException: /examples/Include.xhtml @8,70 action="#{viewController.newAction}": Method not found: phoneTypesTopNavigationBean.newAction()
                com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:99)
                javax.faces.component.UICommand.broadcast(UICommand.java:383)
                org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:321)
                org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:296)
                org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:253)
                org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:466)
                com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
                com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
                com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
                javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
                org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
                org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
                org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
                org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
                com.test.lbs.presentation.filter.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:48)


                root cause

                javax.faces.el.MethodNotFoundException: javax.el.MethodNotFoundException: /examples/Include.xhtml @8,70 action="#{viewController.newAction}": Method not found: phoneTypesTopNavigationBean.newAction()
                javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:81)
                com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
                javax.faces.component.UICommand.broadcast(UICommand.java:383)
                org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:321)
                org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:296)
                org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:253)
                org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:466)
                com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
                com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
                com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
                javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
                org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
                org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
                org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
                org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
                com.test.lbs.presentation.filter.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:48)


                root cause

                javax.el.MethodNotFoundException: /examples/Include.xhtml @8,70 action="#{viewController.newAction}": Method not found: phoneTypesTopNavigationBean.newAction()
                com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:72)
                javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:77)
                com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
                javax.faces.component.UICommand.broadcast(UICommand.java:383)
                org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:321)
                org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:296)
                org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:253)
                org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:466)
                com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
                com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
                com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
                javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
                org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
                org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
                org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
                org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
                com.test.lbs.presentation.filter.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:48)

                 

                The related files are

                Include.xhtml

                <!

                 

                DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                <

                 

                html xmlns="http://www.w3.org/1999/xhtml"

                 

                xmlns:ui="http://java.sun.com/jsf/facelets"

                 

                xmlns:h="http://java.sun.com/jsf/html"

                 

                xmlns:f="http://java.sun.com/jsf/core"

                 

                xmlns:a4j="http://richfaces.org/a4j"

                 

                xmlns:rich="http://richfaces.org/rich">

                 

                <h:commandButton action="#{viewController.newAction}" value="new"/>

                </

                 

                html>

                LandingPhoneType.xhtml

                <!

                 

                DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                <

                 

                html xmlns="http://www.w3.org/1999/xhtml"

                 

                xmlns:ui="http://java.sun.com/jsf/facelets"

                 

                xmlns:h="http://java.sun.com/jsf/html"

                 

                xmlns:f="http://java.sun.com/jsf/core"

                 

                xmlns:a4j="http://richfaces.org/a4j"

                 

                xmlns:rich="http://richfaces.org/rich">

                <

                 

                ui:composition template="/pages/templates/Main.xhtml">

                 

                <ui:define name="body" >

                 

                <h:form>

                phone

                 

                <ui:include src="/examples/Include.xhtml" >

                 

                <ui:param name="viewController" value="phoneTypesTopNavigationBean" />

                 

                </ui:include>

                 

                </h:form>

                 

                </ui:define>

                </

                 

                ui:composition>

                </

                 

                html>

                LandingCellType.xhtml

                <!

                 

                DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                <

                 

                html xmlns="http://www.w3.org/1999/xhtml"

                 

                xmlns:ui="http://java.sun.com/jsf/facelets"

                 

                xmlns:h="http://java.sun.com/jsf/html"

                 

                xmlns:f="http://java.sun.com/jsf/core"

                 

                xmlns:a4j="http://richfaces.org/a4j"

                 

                xmlns:rich="http://richfaces.org/rich">

                <

                 

                ui:composition template="/pages/templates/Main.xhtml">

                 

                <ui:define name="body" >

                 

                <h:form>

                cell

                 

                <h:commandButton action="#{viewController.newAction}" value="new">

                 

                <ui:param name="viewController" value="cellTypesTopNavigationBean" /></h:commandButton>

                 

                </h:form>

                 

                </ui:define>

                </

                 

                ui:composition>

                </

                 

                html>

                 

                PhoneTypes.xhtml

                <!

                 

                DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                <

                 

                html xmlns="http://www.w3.org/1999/xhtml"

                 

                xmlns:ui="http://java.sun.com/jsf/facelets"

                 

                xmlns:h="http://java.sun.com/jsf/html"

                 

                xmlns:f="http://java.sun.com/jsf/core"

                 

                xmlns:a4j="http://richfaces.org/a4j"

                 

                xmlns:rich="http://richfaces.org/rich">

                <

                 

                ui:composition template="/pages/templates/Main.xhtml">

                 

                <ui:define name="body" >

                phoneType

                 

                 

                 

                 

                </h:form>

                 

                </ui:define>

                </

                 

                ui:composition>

                </

                 

                html>

                 

                CellTypes.xhtml

                <!

                 

                DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                <

                 

                html xmlns="http://www.w3.org/1999/xhtml"

                 

                xmlns:ui="http://java.sun.com/jsf/facelets"

                 

                xmlns:h="http://java.sun.com/jsf/html"

                 

                xmlns:f="http://java.sun.com/jsf/core"

                 

                xmlns:a4j="http://richfaces.org/a4j"

                 

                xmlns:rich="http://richfaces.org/rich">

                <

                 

                ui:composition template="/pages/templates/Main.xhtml">

                 

                <ui:define name="body" >

                CellType

                 

                 

                 

                 

                </h:form>

                 

                </ui:define>

                </

                 

                ui:composition>

                </

                 

                html>

                TopNavigation.java

                package

                 

                com.test.lbs.presentation.examples;

                 

                public

                 

                interface TopNavigation {

                 

                public String newAction();

                 

                }

                 

                CellTypesTopNavigationImpl.java

                public

                 

                class CellTypesTopNavigationImpl implements TopNavigation {

                 

                 

                 

                @Override

                 

                public String newAction() {

                 

                 

                return("newCellTypes");

                 

                // TODO Auto-generated method stub

                 

                }

                 

                 

                 

                }

                 

                PhoneTypesTopNavigationImpl.java

                public

                 

                 

                class PhoneTypesTopNavigationImpl implements TopNavigation {

                 

                 

                 

                @Override

                 

                public String newAction() {

                 

                // TODO Auto-generated method stub

                return

                 

                 

                ("newPhoneTypes");

                }

                 

                 

                 

                }

                 

                Face-config.xml

                <managed-bean>
                  <managed-bean-name>phoneTypesTopNavigationBean</managed-bean-name>
                  <managed-bean-class>com.test.lbs.presentation.examples.PhoneTypesTopNavigationImpl</managed-bean-class>
                  <managed-bean-scope>session</managed-bean-scope>
                </managed-bean>
                <managed-bean>
                  <managed-bean-name>cellTypesTopNavigationBean</managed-bean-name>
                  <managed-bean-class>com.test.lbs.presentation.examples.CellTypesTopNavigationImpl</managed-bean-class>
                  <managed-bean-scope>session</managed-bean-scope>
                </managed-bean>
                <navigation-rule>
                <navigation-case>
                   <from-action>#{phoneTypesTopNavigationBean.newAction}</from-action>
                   <from-outcome>newPhoneTypes</from-outcome>
                   <to-view-id>/examples/PhoneTypes.xhtml</to-view-id>
                  </navigation-case>
                </navigation-rule>
                <navigation-rule>
                <navigation-case>
                   <from-action>#{cellTypesTopNavigationBean.newAction}</from-action>
                   <from-outcome>newCellTypes</from-outcome>
                   <to-view-id>/examples/CellTypes.xhtml</to-view-id>
                  </navigation-case>
                </navigation-rule>

                 

                • 5. Re: How to implment polymorphism in JSF/Richfaces.
                  ilya_shaikovsky

                  my fault.. should be

                   

                   

                  <ui:param name="viewController" value="#{phoneTypesTopNavigationBean}" />

                   

                  • 6. Re: How to implment polymorphism in JSF/Richfaces.

                    Hi Ilya Shaikovsky ,

                     

                    Thanks for your excellent support,I am building a confidence in my work after your's supports.

                     

                    Thanks

                    Ajaykumar