0

我正在尝试使用 jsf 在我的项目中上传图片。
xhtml页面

<ui:composition template="./template/templateAdmin.xhtml">
        <ui:define name="content">
            <f:view>
                <h:form enctype="multipart/form-data">

                    <table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
                        <tr>
                            <th rowspan="3" class="sized"><img src="images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
                            <th class="topleft"></th>
                            <td id="tbl-border-top">&nbsp;</td>
                            <th class="topright"></th>
                            <th rowspan="3" class="sized"><img src="images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
                        </tr>

                        <tr>
                            <td id="tbl-border-left"></td>
                            <td>
                                <!--  start content-table-inner -->
                                <div id="content-table-inner">

                                    <table border="0" width="100%" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td colspan="4" ><h1>NEW PRODUCT</h1></td>

                                        </tr>
                                        <tr></tr>
                                        <tr valign="top" style="float: left;padding-left: 76px; width: 69%;">
                                            <td>
                                                <table border="0" cellpadding="0" cellspacing="0"  id="id-form">
                                                    <tr>
                                                        <td> <h:outputText value="Product Name"/>&nbsp;&nbsp;<font color="red" style="font-weight: bold">*</font></td>
                                                        <td>  <h:inputText id="name" styleClass="inp-form" value="#{beanProducts.productName}" required="true" requiredMessage="Enter Product Name!!!">
                                                                <f:validateRegex pattern="^[a-zA-Z0-9]+$"/>
                                                            </h:inputText><h:message for="name"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td> <h:outputText value="Category Name"/></td>
                                                        <td>
                                                            <h:selectOneMenu styleClass="styledselect_form_1" value="#{beanProducts.categoryID}">
                                                                <f:selectItems value="#{beanCategory.list}" var="item" itemValue="#{item.get(0)}" itemLabel="#{item.get(1)}"/>

                                                            </h:selectOneMenu>

                                                        </td>
                                                    </tr> 
                                                    <tr>
                                                        <td> <h:outputText value="Quantity"/></td>
                                                        <td>  <h:inputText id="quantity" styleClass="inp-form" value="#{beanProducts.quantity}" required="true" requiredMessage="Enter product quantity!!!" validatorMessage="Quantity must is number">

                                                                <f:validateRegex pattern="^[0-9]+$"/>
                                                            </h:inputText><h:message for="quantity"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td> <h:outputText value="UnitCost"/></td>
                                                        <td>  <h:inputText id="unitcost" styleClass="inp-form" value="#{beanProducts.unitCost}" required="true" requiredMessage="Enter product unitcost!!!" validatorMessage="Unitcost must is number">

                                                                <f:validateRegex pattern="^[0-9]+$"/>
                                                            </h:inputText><h:message for="unitcost"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td> <h:outputText value="Summary"/></td>
                                                        <td> <h:inputText id="summary" styleClass="inp-form" value="#{beanProducts.prDescription}" required="true" requiredMessage="Enter Product description!!!">
                                                                <f:validateRegex pattern="^[a-z0-9]+"/>
                                                            </h:inputText><h:message for="summary"/></td>
                                                    </tr>

                                                    <tr>
                                                        <td> <h:outputText value="Description"/></td>
                                                        <td> <h:inputText id="description" styleClass="inp-form" value="#{beanProducts.prDescription}" required="true" requiredMessage="Enter Product description!!!">
                                                                <f:validateRegex pattern="^[a-z0-9]+"/>
                                                            </h:inputText><h:message for="description"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td> <h:outputText value="Image"/></td>
                                                        <td> <h:form enctype="multipart/form-data">
                                                                <p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
                                                                              mode="advanced"
                                                                              update="messages"
                                                                              sizeLimit="5000000"
                                                                              allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>

                                                                <p:growl id="messages" showDetail="true"/>
                                                            </h:form>
                                                        </td>
                                                    </tr>

                                                    <tr>
                                                        <td> </td>
                                                        <td><h:commandButton value="Save"   styleClass="form-submit" actionListener="#{beanProducts.insertProduct}"/><h:commandButton styleClass="form-reset" value="Cancel" actionListener="#{beanProducts.reset}"/></td>

                                                    </tr>
                                                    <tr>
                                                        <td><h:messages style="color: red" /></td>
                                                        <td></td>
                                                    </tr>
                                                </table>
                                            </td>

                                        </tr>
                                        <tr>
                                            <td><img src="images/shared/blank.gif" width="695" height="1" alt="blank" /></td>
                                            <td></td>
                                        </tr>
                                    </table>
                                    <div class="clear"></div>
                                </div>
                            </td>
                            <td id="tbl-border-right"></td>
                        </tr>
                        <tr>
                            <th class="sized bottomleft"></th>
                            <td id="tbl-border-bottom">&nbsp;</td>
                            <th class="sized bottomright"></th>
                        </tr>
                    </table>

                </h:form>
            </f:view>
        </ui:define>
    </ui:composition>

豆:

public void handleFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

如果我建立在一个简单的项目上,它会完美运行。但是当我将此代码包含在ui:define标签中时。它不起作用。

我该如何解决。

谢谢。

4

0 回答 0