开发者

JSF 2.0,partially rendered form field input value not updated in the bean

开发者 https://www.devze.com 2023-04-07 16:12 出处:网络
I have an Edit product form which is pre-populated with values from DB . User can change one or more values and post the form back. One input field called t:inputFileUpload is rendered only after an a

I have an Edit product form which is pre-populated with values from DB . User can change one or more values and post the form back. One input field called t:inputFileUpload is rendered only after an ajax request ,if the user opts to change product image.During the final postback of the edit form through a save button,the bean is not updated with the value of t:inputFileUpload field.The relevant p开发者_JS百科ortion of the form is below:

    <h:form>    
                <tr>
                    <td>Product Image*:</td>
                    <td>
                        <h:graphicImage url="#{addItem.prodFileName}" width="100" height="100"/>
                        <br /><h:commandLink  value="change image" >
                            <f:ajax render="uploadimage" execute="@this" listener="#{addItem.ChangeImage}"/>
                        </h:commandLink>
                    </td>
                </tr>

                <tr >
                    <td>
                         <h:panelGroup id="uploadimage">
                            <t:inputFileUpload rendered="#{addItem.editImgChange}" label="editImage" value="#{addItem.uploadedFile}" />
                                <h:messages for="prodimage"/>
                            <h:inputHidden id="hiddeneditimgchange" value="#{addItem.editImgChange}" />
                        </h:panelGroup>
                    </td>
                </tr>
         <h:commandButton value="save" action="#{addItem.EditItem}" />
 </h:form>

The AddItem bean is request scoped and the relevant part of its code is :

    @ManagedBean
    public class AddItem extends AbstractBean{
        boolean editImgChange;
        private UploadedFile uploadedFile;

        //..
        //getters and setters

        public void ChangeImage(){
                this.editImgChange=true;
            }

        public String EditItem() {
            //some logic 
        }
    }

I have read a few similar questions some of whose answers were to make the bean viewscoped.I have tried making the bean ViewScoped ,but it breaks my initial logic of prepopulating the form values. Since i am happy with RequestScoped , I have saved the state of the editImgChange flag ,if its turning off is affecting the updation of the t:inputFileUpload . When i looked at the bean properties all is fine ,the flag is true , but the uploadedFile property is null.


As per the comments, you used <h:inputHidden value="#{addItem.editImgChange}" /> to save the state. This is not going to work. The rendered attribute is evaluated during apply request values phase, while that hidden value is made available during update model values phase which is thus too late.

Since you're already using Tomahawk, use <t:saveState value="#{addItem.editImgChange}" /> instead. Or, just fix the problem which you encountered when making the bean view scoped. I don't forsee why that would be a problem. Perhaps you're using @PostConstruct and expecting that it's invoked on every request. You should then use <f:event type="preRenderView"> instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号