Have to click the button twice to invoke its action!!!!

I am using netbeans 5.5 visual web pack and tomcat server for my JSF project.

In one of my pages i have to click the submit button twice to invoke its action.

I searched forum for the answer but could not find any that could solve my problem.

Whenever i first click the button from the lifecycle listener class i used i found the application goes through following phases:

BeforePhase: RESTORE_VIEW 1

AfterPhase: RESTORE_VIEW 1

BeforePhase: APPLY_REQUEST_VALUES 2

com.sun.rave.web.ui.renderer.UploadRenderer::decode()

com.sun.rave.web.ui.renderer.UploadRenderer::Lookingfor id form1:citizenshipFileUPloadField_com.sun.rave.web.ui.upload

com.sun.rave.web.ui.renderer.UploadRenderer::Found id form1:citizenshipFileUPloadField_com.sun.rave.web.ui.upload

AfterPhase: APPLY_REQUEST_VALUES 2

BeforePhase: RENDER_RESPONSE 6

AfterPhase: RENDER_RESPONSE 6

There is no INVOKE_APPLICATION phase in first click. In second click, the status i got is:

BeforePhase: RESTORE_VIEW 1

AfterPhase: RESTORE_VIEW 1

BeforePhase: APPLY_REQUEST_VALUES 2

com.sun.rave.web.ui.renderer.UploadRenderer::decode()

com.sun.rave.web.ui.renderer.UploadRenderer::Lookingfor id form1:citizenshipFileUPloadField_com.sun.rave.web.ui.upload

com.sun.rave.web.ui.renderer.UploadRenderer::Found id form1:citizenshipFileUPloadField_com.sun.rave.web.ui.upload

AfterPhase: APPLY_REQUEST_VALUES 2

BeforePhase: PROCESS_VALIDATIONS 3

AfterPhase: PROCESS_VALIDATIONS 3

BeforePhase: UPDATE_MODEL_VALUES 4

AfterPhase: UPDATE_MODEL_VALUES 4

BeforePhase: INVOKE_APPLICATION 5

AfterPhase: INVOKE_APPLICATION 5

BeforePhase: RENDER_RESPONSE 6

AfterPhase: RENDER_RESPONSE 6

Now the INVOKE_APPLICATION phase is invoked.

I have <h:messages /> tag but no errors could be seen.

What can be the reasons behind this problem

[2027 byte] By [kirandeluxea] at [2007-11-27 0:48:17]
# 1
This is usually caused by faulty JSF code and/or wrong scopes of the properties involved in the action or renders.Can you show the smallest working JSF code to reproduce this problem?
BalusCa at 2007-7-11 23:17:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

[nobr]Well the code that is producing the problem is quite big. Anyway i will post it, hoope you can get something out of it.

The jsp page is:

<ui:body binding="#{ReceptionPage$CompanyPersonPage.body1}" id="body1" style="-rave-layout: grid">

<div align="center">

<ui:staticText binding="#{ReceptionPage$CompanyPersonPage.staticText1}" id="staticText1"

style="font-family: Georgia,'Times New Roman',times,serif; font-size: 18px; font-weight: bold" text="Company Person Page"/>

</div>

<div align="left" style="color:red;font-weight:bold;layout:table;">

<h:messages id="errMsgs"/>

</div>

<ui:form binding="#{ReceptionPage$CompanyPersonPage.form1}" id="form1">

<ul>

<li>

<ui:label binding="#{ReceptionPage$CompanyPersonPage.label1}" id="label1" style="" text="Citizenship Number:"/>

<br/>

<ui:textField binding="#{ReceptionPage$CompanyPersonPage.textField1}" id="textField1" required="true" style="" text="#{CompanyPersonBean.citizenshipNumber}"/>

</li>

<li>

<ui:label binding="#{ReceptionPage$CompanyPersonPage.label2}" id="label2" style="" text="Name:"/>

<br/>

<ui:textField binding="#{ReceptionPage$CompanyPersonPage.textField2}" id="textField2" required="true" style="" text="#{CompanyPersonBean.name}"/>

</li>

<li>

<ui:label binding="#{ReceptionPage$CompanyPersonPage.label3}" id="label3" style="" text="Country:"/>

<br/>

<ui:textField binding="#{ReceptionPage$CompanyPersonPage.textField3}" id="textField3" required="true" style="" text="#{CompanyPersonBean.country}"/>

</li>

<li>

<fieldset style="width:250px;float:left;">

<legend>Temporary Address</legend>

<ui:label binding="#{ReceptionPage$CompanyPersonPage.tempDistrictLabel}" id="tempDistrictLabel" text="District:"/>

<br/>

<ui:dropDown binding="#{ReceptionPage$CompanyPersonPage.tempDistrictDropDown}"

converter="#{ReceptionPage$CompanyPersonPage.integerConverter1}" id="tempDistrictDropDown" immediate="true"

items="#{ReceptionPage$CompanyPersonPage.tempDistrictDataProvider.options['DISTRICT_ID,DISTRICT_NEPALI_NAME']}"

onChange="common_timeoutSubmitForm(this.form, 'ul:li:fieldset:tempDistrictDropDown');" valueChangeListener="#{ReceptionPage$CompanyPersonPage.tempDistrictDropDown_processValueChange}"/>

<br/>

<ui:label binding="#{ReceptionPage$CompanyPersonPage.tempAreaLabel}" id="tempAreaLabel" text="Area(VDC/Municipality):"/>

<br/>

<ui:dropDown binding="#{ReceptionPage$CompanyPersonPage.tempAreaDropDown}"

converter="#{ReceptionPage$CompanyPersonPage.integerConverter3}" id="tempAreaDropDown" items="#{ReceptionPage$CompanyPersonPage.tempAreaDataProvider.options['AREA_ID,AREA_NEPALI_NAME']}"/>

<br/>

<ui:label binding="#{ReceptionPage$CompanyPersonPage.tempWardNoLabel}" id="tempWardNoLabel" text="Ward No:"/>

<br/>

<ui:textField binding="#{ReceptionPage$CompanyPersonPage.tempWardNoTextField}" id="tempWardNoTextField" required="true"/>

<br/>

<ui:label binding="#{ReceptionPage$CompanyPersonPage.tempHouseNoLabel}" id="tempHouseNoLabel" text="House No:"/>

<br/>

<ui:textField binding="#{ReceptionPage$CompanyPersonPage.tempHouseNoTextField}" id="tempHouseNoTextField"/>

</fieldset>

<fieldset style="width:250px;">

<legend>

Permanent Address</legend>

<ui:label binding="#{ReceptionPage$CompanyPersonPage.permDistrictLabel}" id="permDistrictLabel" text="District:"/>

<br/>

<ui:dropDown binding="#{ReceptionPage$CompanyPersonPage.permDistrictDropDown}"

converter="#{ReceptionPage$CompanyPersonPage.integerConverter2}" id="permDistrictDropDown" immediate="true"

items="#{ReceptionPage$CompanyPersonPage.permDistrictDataProvider.options['DISTRICT_ID,DISTRICT_NEPALI_NAME']}"

onChange="common_timeoutSubmitForm(this.form, 'ul:li:fieldset:permDistrictDropDown');" valueChangeListener="#{ReceptionPage$CompanyPersonPage.permDistrictDropDown_processValueChange}"/>

<br/>

<ui:label binding="#{ReceptionPage$CompanyPersonPage.permAreaLabel}" id="permAreaLabel" text="Area(VDC/Municipality):"/>

<br/>

<ui:dropDown binding="#{ReceptionPage$CompanyPersonPage.permAreaDropDown}"

converter="#{ReceptionPage$CompanyPersonPage.integerConverter4}" id="permAreaDropDown" items="#{ReceptionPage$CompanyPersonPage.permAreaDataProvider.options['AREA_ID,AREA_NEPALI_NAME']}"/>

<br/>

<ui:label binding="#{ReceptionPage$CompanyPersonPage.permWardNoLabel}" id="permWardNoLabel" text="Ward No:"/>

<br/>

<ui:textField binding="#{ReceptionPage$CompanyPersonPage.permWardNoTextField}" id="permWardNoTextField" required="true"/>

<br/>

<ui:label binding="#{ReceptionPage$CompanyPersonPage.permHouseNoLabel}" id="permHouseNoLabel" text="House No:"/>

<br/>

<ui:textField binding="#{ReceptionPage$CompanyPersonPage.permHouseNoTextField}" id="permHouseNoTextField"/>

</fieldset>

</li>

<li>

<ui:label binding="#{ReceptionPage$CompanyPersonPage.label4}" id="label4" style="" text="Phone Number:"/>

<br/>

<ui:textField binding="#{ReceptionPage$CompanyPersonPage.textField4}" id="textField4" style="" text="#{CompanyPersonBean.phoneNumber}"/>

</li>

<li>

<ui:label binding="#{ReceptionPage$CompanyPersonPage.label5}" id="label5" style="" text="Mobile:"/>

<br/>

<ui:textField binding="#{ReceptionPage$CompanyPersonPage.textField5}" id="textField5" style="" text="#{CompanyPersonBean.mobileNumber}"/>

</li>

<li>

<ui:label binding="#{ReceptionPage$CompanyPersonPage.label6}" id="label6" style="" text="Email:"/>

<br/>

<ui:textField binding="#{ReceptionPage$CompanyPersonPage.textField6}" id="textField6" style=""

text="#{CompanyPersonBean.emailAddress}" validator="#{ReceptionPage$CompanyPersonPage.textField6_validate}"/>

</li>

<li>

<b>Person Company Relation:</b>

<br/>

<ui:checkbox binding="#{ReceptionPage$CompanyPersonPage.checkbox1}" id="checkbox1" label="Is Promoter" style=""/>

<br/>

<ui:checkbox binding="#{ReceptionPage$CompanyPersonPage.checkbox2}" id="checkbox2" label="Is Board Oof Director" style=""/>

<br/>

<ui:checkbox binding="#{ReceptionPage$CompanyPersonPage.checkbox3}" id="checkbox3" label="Is Shareholder" style=""/>

<br/>

<br/>

</li>

<li>

<ui:label binding="#{ReceptionPage$CompanyPersonPage.label7}" id="label7" style="" text="Citizenship Certificate File"/>

<br/>

<ui:upload binding="#{ReceptionPage$CompanyPersonPage.citizenshipFileUPloadField}"

columns="#{CompanyPersonBean.citizenshipFilePath}" id="citizenshipFileUPloadField" required="true" style=""/>

</li>

<li>

<ui:button action="#{ReceptionPage$CompanyPersonPage.button1_action}" binding="#{ReceptionPage$CompanyPersonPage.button1}"

id="button1" style="height: 23px"

text="Save"/>

||

<ui:button

binding="#{ReceptionPage$CompanyPersonPage.button2}" id="button2" onClick="window.close()" style="height: 24px" text="Close"/>

</li>

</ul>

</ui:form>

</ui:body>

The corresponding bean is:

public class CompanyPerson {

private String citizenshipNumber;

private String name;

private String country;

private String phoneNumber;

private String mobileNumber;

private String emailAddress;

private String citizenshipFilePath;

private String isShareHolder;

private String isBoardOfDirector;

private String isPromoter;

public CompanyPerson() {

}

public String getCitizenshipNumber() {

return citizenshipNumber;

}

public void setCitizenshipNumber(String citizenshipNumber) {

this.citizenshipNumber = citizenshipNumber;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getCountry() {

return country;

}

public void setCountry(String country) {

this.country = country;

}

public String getPhoneNumber() {

return phoneNumber;

}

public void setPhoneNumber(String phoneNumber) {

this.phoneNumber = phoneNumber;

}

public String getMobileNumber() {

return mobileNumber;

}

public void setMobileNumber(String mobileNumber) {

this.mobileNumber = mobileNumber;

}

public String getEmailAddress() {

return emailAddress;

}

public void setEmailAddress(String emailAddress) {

this.emailAddress = emailAddress;

}

public int getTemporaryAddressId() {

return temporaryAddressId;

}

public void setTemporaryAddressId(int temporaryAddressId) {

this.temporaryAddressId = temporaryAddressId;

}

public int getPermanentAddressId() {

return permanentAddressId;

}

public void setPermanentAddressId(int permanentAddressId) {

this.permanentAddressId = permanentAddressId;

}

public String getCitizenshipFilePath() {

return citizenshipFilePath;

}

public void setCitizenshipFilePath(String citizenshipFilePath) {

this.citizenshipFilePath = citizenshipFilePath;

}

public String getIsShareHolder() {

return isShareHolder;

}

public void setIsShareHolder(String isShareHolder) {

this.isShareHolder = isShareHolder;

}

public String getIsBoardOfDirector() {

return isBoardOfDirector;

}

public void setIsBoardOfDirector(String isBoardOfDirector) {

this.isBoardOfDirector = isBoardOfDirector;

}

public String getIsPromoter() {

return isPromoter;

}

public void setIsPromoter(String isPromoter) {

this.isPromoter = isPromoter;

}

}

No all the properties of the bean has been bind to components.

I also have value change listener methods for two drop down lists whose immediate property is set to true to bypass the validation. If i change the dropdownlist selection then its value change listener method is successfully invoked, then after if i click the submit button, then it will works.[/nobr]

kirandeluxea at 2007-7-11 23:17:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
I recommend you to strip out the code until you get the smallest working snippet which reproduces this problem.
BalusCa at 2007-7-11 23:17:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Ok

I finally found the root cause of the problem. Actually i have set the immediate property of two dropdownlist that i have used to "true" in order to bypass the validation of the other components.

The code snippet is given below:

public void tempDistrictDropDown_processValueChange(ValueChangeEvent event) {

try {

getSessionBean1().getCro_areaRowSet1().setObject(1,tempDistrictDropDown.getValue());

tempAreaDataProvider.refresh();

} catch (SQLException ex) {

ex.printStackTrace();

}

FacesContext context = FacesContext.getCurrentInstance();

context.renderResponse();

}

If i remove the immediate property or if i remove the following lines of code which is in the above method:

FacesContext context = FacesContext.getCurrentInstance(); context.renderResponse();

the action of the button is invoked in first click.

But i am still confused with this problem although i get it working any how.

What can be the reason behind that.

Also i need to bypass the validation when dropdown list is changed.

How can i achive that.

kirandeluxea at 2007-7-11 23:17:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

FacesContext.getCurrentInstance().renderResponse();

Doing it so in the ValueChangeListener method means that the 4th and the 5th phase of the JSF lifecycle will be skipped.

The ValueChangeListeners are invoked during the 3rd phase. The values of UIInput components -if any- will be set during the 4th phase.

The method behind the UICommand component will be invoked during the 5th phase. Finally rendering of the response happens during the 6th phase.

When invoking FacesContext#renderResponse() manually, you're jumping to the 6th phase directly.

Also see http://balusc.xs4all.nl/srv/dev-jep-djl.html to get some insights in the JSF lifecycle.

BalusCa at 2007-7-11 23:17:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Thanks,

That helped a lot to get the insight of JSF lifecycle.

Now how can i accomplish the problem then.

I want to bypass the validation when the dropdownlist is changed and want the validation to be done only when the submit button is clicked.

Actually i did that by the help of some other posts.

As i have made my input fields as "required field" if i dont set immediate property true and don't use the line:

FacesContext.getCurrentInstance().renderResponse();

the validation is done even if i change the dropdown list as the page gets submitted when the dropdownlist is changed.

I just want to skip the validation phase when the dropdownlist is changed

Thanks in advance

Message was edited by:

kirandeluxe

kirandeluxea at 2007-7-11 23:17:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Trypublic void change(ValueChangeEvent e) {

// Do your VCE thing here.

...

// Then check if CommandButton is pressed:

Map requestParameterMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();

if (!requestParameterMap.containsValue("submit")) {

// If this is not true, then proceed to render response.

FacesContext.getCurrentInstance().renderResponse();

}

}

Where "submit" is the value of the button.<h:commandButton value="submit" action="#{myBean.action}" />

Or use requestParameterMap.containsKey('formId:commandButtonId');

I don't see another ways to check in the 3rd phase if any action is fired.

BalusCa at 2007-7-11 23:17:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Thanks man,this is exactly what i want
kirandeluxea at 2007-7-11 23:17:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
hai Kiran, u have posted the Good topic and solved the problem which i was facing. once again i thank u and Mr BalusC has to be appreciated for this.regards,vijay
vijaycanaana at 2007-7-11 23:17:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

Yw :)

Although I personally find it a nasty way to find out if any action is fired in the request.

I personally should find it useful if FacesContext provides a method to check whether an ActionEvent has been fired and if so, which one it is.

Digging in the requestParameterMap isn't a very gently solution.

BalusCa at 2007-7-11 23:17:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...