HTML and CSS Reference
In-Depth Information
<f:selectItem itemValue="English" itemLabel="English"/>
<f:selectItem itemValue="Others" itemLabel="Others"/>
</h:selectOneMenu>
</div>
<div class="form-group">
<h:outputLabel value="Content" for="content"/>
<h:inputFile id="content" value="#{bookAddBacking.filePart}"
class="form-control" required="true"
validator="#{bookAddBacking.validateFile}">
</h:inputFile>
</div>
<br/>
<h:commandButton value="Save Book" class="btn btn-default"
action="#{bookAddBacking.saveBook}">
<f:ajax execute="@form" render="@all"/>
</h:commandButton>
<h:messages id="messages" class="errorMessage"/>
</h:form>
</ui:define>
</ui:composition>
</html>
The topic add form includes the corresponding input fields to the
Book
JPA entity attributes, which were
described in Listing 13-6. When the user clicks the
"Save Book"
command button, and using the
<f:ajax>
tag, the
book add form attributes are sent to the server, the
#{bookAddBacking.saveBook}
action method is executed, and
finally either a success message or a failure message(s) is rendered to the client.
As you notice, the topic add form uses the JSF 2.2
<h:inputFile>
component in combination with
<f:ajax>
in
order to upload the topic content in an Ajaxified way. In order to have this functionality working, you have to make
sure that the form
enctype
is set to
"multipart/form-data"
.
■
there is a bug in Mojarra JSF 2.2
<h:inputFile>
component when used with
<f:ajax>
; this bug results
in rendering an extra iframe when the response is returned from the server:
https://java.net/jira/browse/
JAVASERVERFACES-2851
.
in order to solve this issue, you have to use
@all
in the
render
attribute of
<f:ajax>
tag.
Caution
Using
#{bookAddBacking.validateFile}
in the
validator
attribute of
<h:inputFile>
, the file size is validated
for not exceeding
1 megabyte
and the file type is validated to be of type PDF.
BookAddBacking
CDI bean which
includes
validateFile
is shown in Listing 13-31.
Listing 13-31.
BookAddBacking CDI Bean
package com.jsfprohtml5.megaapp.backing;
import com.jsfprohtml5.megaapp.model.Book;
import com.jsfprohtml5.megaapp.service.BookManagerLocal;
import com.jsfprohtml5.megaapp.service.exception.BookAlreadyExists;
import java.io.Serializable;
import java.util.ArrayList;
