1

Hi In my application i have two block one in present Address and other one permanent Address.

If My present address and permanent address is same i need to copy the present address field to permanent Address field.

I have attached the code here

<h:outputText value="#{msg['elicense.examinationform.address.presentaddressline1']}" />
<p:inputText id="presentaddress1" value="#{personalBean.presentAddressLine1}" label="Present Address Line1" />
<p:watermark for="presentaddress1" value="Present Address Line1" id="presentaddressdata1"></p:watermark>


<h:outputText value="#{msg['elicense.examinationform.address.copy']}" />
<p:selectBooleanCheckbox value="#{examinationFormBean.copyAddress}" label="addresscopy" />
<p:spacer></p:spacer>

<h:outputText value="#{msg['elicense.examinationform.address.permanentline1']}" />
<p:inputText id="address1" value="#{personalBean.permanentAddressLine1}" label="Permanent Address Line1" />

I have updated my code. I need to copy presentAddressLine1 value to permanentAddressLine1 when ever i checked the check box using p:ajax. please help me

4
  • What have you tried so far? Please, provide only the necessary code for your case, you've got a lot of irrelevant code.
    – Aritz
    Jul 26, 2014 at 12:45
  • <p:selectBooleanCheckbox value="#{examinationFormBean.copyAddress}" label="addresscopy" /> by using it i need to copy my present address field to permanent address field. when user checked the checkbox it will copy the present address to permanent address. Jul 30, 2014 at 5:34
  • As you're new in StackOverflow, just keep in mind you can edit your question changing your code instead of posting it as a comment.
    – Aritz
    Jul 30, 2014 at 5:41
  • I have updated my code.please give a look and help me Jul 30, 2014 at 12:41

1 Answer 1

1

That's a matter of sending an Ajax request when the p:selectBooleanCheckBox component gets clicked. Here you've got a basic SSCCE:

@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    private String address1;
    private String address2;
    private boolean copyAdress;

    public String getAddress1() {
        return address1;
    }

    public String getAddress2() {
        return address2;
    }

    public boolean isCopyAdress() {
        return copyAdress;
    }

    public void listener() {
        if (copyAdress) {
            address2 = address1;
        } else {
            address2 = "";
        }
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    public void setCopyAdress(boolean copyAdress) {
        this.copyAdress = copyAdress;
    }

}
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head />
<h:body>
    <h:form>
        <p:inputText value="#{bean.address1}" />
        <p:inputText id="add2" value="#{bean.address2}" />
        <p:selectBooleanCheckbox value="#{bean.copyAdress}">
            <p:ajax listener="#{bean.listener}" update="add2" process="@form" />
        </p:selectBooleanCheckbox>
    </h:form>
</h:body>
</html>

The p:ajax specifies a listener with the method we want to invoke in server side. We'll send the whole form to process (that way the current values for the addresses are set before the listener being executed) and the component to update after the request must be the input attached to the second address, which is the one we want to copy the value to.

2
  • Thanks Xtreme Biker. I have implemented your code and its been working fine. thanks again Jul 31, 2014 at 9:21
  • @ChandanSarma you're welcome. As you're new in SO, remember you can mark the answer as correct clicking the checkmark at the left side and/or upvote it if you consider it to be useful. Regards
    – Aritz
    Jul 31, 2014 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.