0
votes

I am new to jsf jpa rest.I was wondering how do set values in beans from the controller beans.

Say suppose once the user registers I want to insert the values to the DB along with date which I generate in the controllerbean.Now how do I set this in the POJO

register.xhtml

<h:form id="registerForm">
            <table>
                <tr>
                    <td colspan="3" id="register">
                        <h2 style="border-bottom: 1px solid #CCCCCC;">Customer
                            Information</h2>
                        <tr>
                            <td class="label">
                                <h:outputLabel for="name" value="Name:"></h:outputLabel>
                            </td>
                            <td class="field">
                                <h:inputText id="name" size="20" required="true" requiredMessage="Please enter your name" value="#{registerController.selected.contactName}">
                                    <f:validateLength maximum="50"></f:validateLength>
                                </h:inputText>
                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                <h:outputLabel for="company" value="Company:"></h:outputLabel>
                            </td>
                            <td class="field">
                                <h:inputText id="company" size="20" required="true" requiredMessage="Please enter your company" value="#{registerController.selected.company}">
                                    <f:validateLength maximum="50"></f:validateLength>
                                </h:inputText>
                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                <h:outputLabel for="designation" value="Designation:"></h:outputLabel>
                            </td>
                            <td class="field">
                                <h:inputText id="designation"  size="20" required="true" requiredMessage="Please enter your designation" value="#{registerController.selected.designation}">
                                    <f:validateLength maximum="50"></f:validateLength>
                                </h:inputText>

                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                <h:outputLabel for="email" value="Email Id:"></h:outputLabel>
                            </td>
                            <td class="field">
                                <h:inputText id="email"  size="20" required="true" requiredMessage="Please enter your email,eg:[email protected]" value="#{registerController.selected.primaryEmailId}" title="eg:www.domain.com OR http://127.0.0.1/domain">
                                    <f:validateLength maximum="70"></f:validateLength>
                                </h:inputText>
                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                <h:outputLabel for="phone" value="Contact No:"></h:outputLabel>
                            </td>
                            <td class="field">
                                <h:inputText id="phone" size="20" required="true" requiredMessage="Please enter your contact number" value="#{registerController.selected.contactNo}" title="eg:+919999999999">
                                    <f:validateLength minimum="13" maximum="25"></f:validateLength>
                                </h:inputText>
                            </td>
                        </tr>

                    </td>

                </tr>
                <tr>
                    <td colspan="4" id="loginInfo">
                        <h2 style="border-bottom: 1px solid #CCCCCC;">Login Information</h2>
                        <tr>
                            <td class="label"> 
                                <h:outputLabel for="email" value="Login Email:"></h:outputLabel>
                            </td>
                            <td class="field">
                                <h:inputText id="email"  size="20" maxlength="70" required="true" requiredMessage="Please enter your login email id,eg:[email protected]" value="#{loginController.selected.clientId}">
                                    <f:validateLength maximum="70"></f:validateLength>
                                </h:inputText>

                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                <h:outputLabel for="password" value="Password:"></h:outputLabel>
                            </td>
                            <td class="field">
                                <h:inputSecret id="password"  size="20" maxlength="70" required="true" requiredMessage="Please enter your desired password(minimun 6 characters)" value="#{loginController.selected.password}">
                                    <f:validateLength minimum="6" maximum="50"></f:validateLength>
                                </h:inputSecret>

                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                <h:outputLabel for="confirm_password" value="Confirm Password:"></h:outputLabel>
                            </td>
                            <td class="field">
                                <h:inputSecret id="confirm_password" size="20" required="true" requiredMessage="Please confirm your password">
                                    <f:validateLength minimum="6" maximum="50"></f:validateLength>
                                </h:inputSecret>
                            </td>
                        </tr>
                    </td>
                    <tr>
                        <td>
                            <h:commandButton value="Sign Up" action="">
                            </h:commandButton>
                        </td>
                    </tr>
                </tr>
                </table>

Now my Register and login bean contain getters and setters

login bean

       public String getLoginEmail() {
        return loginEMail;
    }

    public void setLoginEmail(String loginEMail) {
        this.loginEMail = loginEMail;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

Register bean

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String ContactName) {
        this.contactName = contactName;
    }

    public String getCompany() {
        System.out.println("get....." + company);
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
        System.out.println("set....." + company);
    }

    public String getDesignation() {
        System.out.println("get....." + designation);
        return designation;
    }

    public void setDesignation(String designation) {
        this.designation = designation;
        System.out.println("set....." + primaryDesignation);
    }

    public String getContactNo() {
        return contactNo;
    }

    public void setContactNo(String contactNo) {
        this.contactNo = contactNo;;
    }

    public String getEmailId() {
        return emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    public String getActiveFlag() {
        return activeFlag;
    }

    public void setActiveFlag(String activeFlag) {
        this.activeFlag = activeFlag;
    }

    public String getCreatedBy() {
        System.out.println("get create name....." + createdBy);
        createdBy=getPrimaryContactName();
        return createdBy;
    }

    public void setCreatedBy(String primaryContactName) {
        this.createdBy = primaryContactName;
    }

    public Date getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
        System.out.println("set....." + createdOn);
    }
    public String getLastUpdatedBy() {
        return lastUpdatedBy;
    }

    public void setLastUpdatedBy(String primaryContactName) {
        this.lastUpdatedBy = primaryContactName;
    }
    public Date getLastUpdatedOn() {
        return lastUpdatedOn;
    }

   public void setLastUpdatedOn(Date lastUpdatedOn) {
       this.lastUpdatedOn = lastUpdatedOn;
    }

I have two controllers registerController and loginController where I have the business logic.

@ManagedBean(name = "registerController")
@SessionScoped
    public String prepareCreate() {
    System.out.println("Inside create");
    current = new Register();
    System.out.println("Inside create current");
    selectedItemIndex = -1;
    return "Signup Success";
}

public String create() {
    try {
        System.out.println("I am inside create");
        getFacade().create(current);
        JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("RegisterCreated"));
        return prepareCreate();
    } catch (Exception e) {
        System.out.println("I am here inside create exception");
        e.printStackTrace();
        JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        return null;
    }
}

public Date generateCurrentTime()
{
    // get current system time yyyy/MMM/dd HH:mm:ss
    Calendar currentDate = Calendar.getInstance();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String dateNow = formatter.format(currentDate.getTime());
            Date convertedDate=null;
            try
            {
            convertedDate=formatter.parse(dateNow);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            System.out.println("date now is:"+dateNow);
            return convertedDate;
}

Similarly loginController also contains the same code. How do I set the convertedDate attribute for createdOn and lastUpdatedOn feilds in the pojo??

I used @ManagedBeanProperty but it dint set the value...

@ManagedBeanProperty(value=#{registerController.getgenerateCurrentTime})
  @Column(name = "created_on")
   @Temporal(TemporalType.DATE)
   private Date createdOn;


  @ManagedBeanProperty(value=#{registerController.getgenerateCurrentTime})
  @Column(name = "created_on")
   @Temporal(TemporalType.DATE)
   private Date lastUpdatedOn;

Kindly point out where I am goin wrong.I have used @sessionscoped

Also as u can see I have two tables in a form and they are dependent on each other.How do I insert them into database when user clicks on register button...Should I pass the control initially on the button click to controller say setupController and then call register and login controller

Thanks:)

2
have you provided method in action of <h:commandButton value="Sign Up" action=""> ?Hemant Metalia
I am asking if I shuld say action="#{setupController.create}" or is there another way to do it.newbie

2 Answers

0
votes
<h:commandButton value="Sign Up" action="">

does the submission of the form which leads to storing the values of fields in back bean (if no ajax used)

and providing an action in command button like action="#{setupController.create}" will cause the invokation of that method create on the submission of the form after the assignment of the fields to the bean.

also you can provide it in action of form

refer How to define form action in JSF?

0
votes

as the other friends said you need to invoke an action to set the values on the backing bean...you can make a method that is invoked by your button's action , and inside that method yo can call your service to store your data in the DB. cheers