1 Reply Latest reply on Apr 27, 2010 6:28 PM by Juanyong Zhang

    JSF Wizard with Weld Implementation

    Juanyong Zhang Newbie

      Hi All,


      Wanna show my imaginary design of wizard using Wizard and JSF... but it seems not works some where. firstly let us the classes I have...



      2 inferfaces and 1 abstract class


      package wziard;
       
      public interface IWizard {
       
           // The return string for the JSF path
           public String next();
       
           public String back();
       
           public String abort();
       
           public boolean hasNext();
       
           public boolean hasBack();
       
      }




      package wziard;
       
      public interface WizardStep {
           //do the business and return JSF path
           String onNext();
       
           String onBack();
      }







        • 1. Re: JSF Wizard with Weld Implementation
          Juanyong Zhang Newbie

          Sry, the save button merely for the submit, and no modify and remove opt here....


          continue:


          One wizard contains couple of wizardStep



          package wziard;
           
          import java.util.List;
           
          public abstract class WizardBean {
               public String next() {
                    String next = getCurrentStep().onNext();
                    setCurrentStep(ListUtils.getNext(getWizSteps(), getCurrentStep()));
                    return next;
               }
           
               public String back() {
                    String back = getCurrentStep().onBack();
                    setCurrentStep(ListUtils.getBack(getWizSteps(), getCurrentStep()));
                    return back;
               }
           
               public boolean hasNext() {
                    return !ListUtils.isLast(getWizSteps(), getCurrentStep());
               }
           
               public boolean hasBack() {
                    return !ListUtils.isFirst(getWizSteps(), getCurrentStep());
               }
           
               public abstract WizardStep getCurrentStep();
           
               public abstract void setCurrentStep(WizardStep curStep);
           
               public abstract List<WizardStep> getWizSteps();
           
               public abstract String abort();
          }
          


          Abstract wizardbean is to implement the wizard's constitutional features.


          Then the wizard implements



          package wziard;
           
          import java.util.List;
          @Stateful
          @ConversationScope
          @Named
          public class RegisterWizard extends WizardBean implements IWizard {
               @RegisterWizard // qualifier for wizards producers
               @Inject
               private List<WizardStep> wizardSteps;
           
               private WizardStep currentStep;
           
               private Conversation conversation;
           
               @Override
               public String abort() {
                    // TODO Go the JSF and end session
                    conversation.end();
                    return null;
               }
           
               @Override
               public WizardStep getCurrentStep() {
                    // TODO Auto-generated method stub
                    return currentStep;
               }
           
               @Override
               public List<WizardStep> getWizSteps() {
                    // TODO Auto-generated method stub
                    return wizardSteps;
               }
           
               @Override
               public void setCurrentStep(WizardStep curStep) {
                    this.currentStep = curStep;
               }
           
          }



          The steps implementations:



          package wziard.step;
           
          import wziard.WizardStep;
          import wziard.model.Account;
           
          public class AccountInfoStep implements WizardStep {
               @Named("newAccount");
               @ConversationScope
               private Account account;
               public Account getAccount() {
                    return account;
               }
           
               public void setAccount(Account account) {
                    this.account = account;
               }
           
               @Override
               public String onBack() {
                    // TODO Auto-generated method stub
                    return null;
               }
           
               @Override
               public String onNext() {
                    // TODO Auto-generated method stub
                    return null;
               }
           
          }




          package wziard.step;
           
          import wziard.WizardStep;
          import wziard.model.Person;
           
          public class PersonInfoStep implements WizardStep {
               @Named("newPerson");
               @ConversationScope
               private Person person;
           
               public Person getPerson() {
                    return person;
               }
           
               public void setPerson(Person person) {
                    this.person = person;
               }
           
               @Override
               public String onBack() {
                    // TODO Auto-generated method stub
                    return null;
               }
           
               @Override
               public String onNext() {
                    // TODO Auto-generated method stub
                    return null;
               }
           
          }





          package wziard.step;
           
          import wziard.model.Contact;
           
          public class ContactInfoStep implements WizardStep {
               @Named("newContact");
               @ConversationScope
               private Contact contact;
           
               public Contact getContact() {
                    return contact;
               }
           
               public void setContact(Contact contact) {
                    this.contact = contact;
               }
           
               @Override
               public String onBack() {
                    // TODO do the business and return JSF path
                    
                    return null;
               }
           
               @Override
               public String onNext() {
                    // do the business and return JSF path
                    return null;
               }
           
          }





          other classes:



          package wziard.utils;
           
          import java.util.List;
           
          public class ListUtils {
           
               public static <T> T getNext(List<T> list, T obj) {
                    // TODO Auto-generated method stub
                    return null;
               }
           
               public static <T> T getBack(List<T> list, T obj) {
                    // TODO Auto-generated method stub
                    return null;
               }
           
               public static <T> boolean isLast(List<T> list, T obj) {
                    // TODO Auto-generated method stub
                    return false;
               }
           
               public static <T> boolean isFirst(List<T> list, T obj) {
                    // TODO Auto-generated method stub
                    return false;
               }
           
          }
           




          package wziard;
           
          import java.util.ArrayList;
          import java.util.List;
           
          import wziard.step.AccountInfoStep;
          import wziard.step.PersonInfoStep;
           
          public class WizardProducer {
               @RegisterWizard
               @Produces
               private List<WizardStep> wizardSteps(@New AccountInfoStep s1,
                         @New PersonInfoStep s2, @New ContactInfoStep s3) {
                    List<WizardStep> steps = new ArrayList<WizardStep>();
                    steps.add(s1);
                    steps.add(s2);
                    steps.add(s3);
                    return steps;
               }
          }





          after all these in the JSF page



          <h:inputtext name="username" value="#{newAccount.userName}">
          <h:inputtext name="password" value="#{newAccount.password}">
          
          <h:commandButton name="back" value="#{registerWizard.back}" rendered="#{registerWizard.hasBack()}">
          <h:commandButton name="next" value="#{registerWizard.next}" rendered="#{registerWizard.hasNext()}">
          




          These ideas should be very straightforward, while the newAccount, newPerson, and newContact, which @Named in the WizardStep seems not hook up with JSF component, these value were not populated at all....


          Can anyone kindly advise? thanks.