简体   繁体   中英

JSF 2 - How can I perform an action after a Composite Component child completes an operation?

I'm still learning to use some of the capabilities of Composite Components in JSF 2. I am experienced with JSF 1.2 development and I have recently read the book "Core Java Server Faces 3rd Edition" by Geary and Horstmann.

What I'm trying to do is create a Composite Component that wraps a file upload component (currently using the PrimeFaces <p:fileUpload> ). I need to associate the uploaded file with a string-based key on a session-scoped managed bean (to be used later). I am trying to provide the key via an attribute on my Composite Component interface named 'uploadedFileKey'. Here is the interface:

<html xmlns:composite="http://java.sun.com/jsf/composite">
    <composite:interface>
        <composite:attribute name="uploadedFileKey" 
            type="java.lang.String" 
            required="true" />
    </composite:interface>
    ...
</html>

The implementation is straightforward and uses the PrimeFaces fileUpload tag as I mentioned earlier. It requires a managed bean with an event handler which I also created based on the sample code from the PrimeFaces showcase webapp. Here is my implementation:

<composite:implementation>
    <p:fileUpload
        fileUploadListener="#{primeFacesFileUploadController.handler}"
        label="Browse"
        mode="advanced"
        allowTypes="png,gif,jpg" />
</composite:implementation>

I'm not going to include the entire controller bean here, but here is the class declaration:

@ManagedBean(name="primeFacesFileUploadController")
@RequestScoped
public class PrimeFacesFileUploadController {
    // ...
}

The PrimeFaces file upload is not unlike others that I have seen. It uses a custom Filter on the Faces Servlet to get access to the uploading data. The actual file upload part works fine and when the upload is successful I have the uploaded file stored in a temp file on my Tomcat server.

My problem is not knowing how to make my Composite Component take an action after a successful upload. I want my Composite Component to store the uploadedFileKey in a Map on a particular session-scoped managed bean with the uploaded File as the map value. How can I do that?

I'd just pass the key along as a custom component attribute by <f:attribute> and let the handler handle it. You can get it in the handler method by event.getComponent().getAttributes() .

Eg

<composite:implementation>
    <p:fileUpload
        fileUploadListener="#{primeFacesFileUploadController.handler}"
        label="Browse"
        mode="advanced"
        allowTypes="png,gif,jpg">
        <f:attribute name="key" value="#{cc.attrs.uploadedFileKey}" />
    </p:fileUpload>
</composite:implementation>

with

public void handler(FileUploadEvent event) {
    String key = (String) event.getComponent().getAttributes().get("key");
    yourSessionBean.getMap().put(key, event.getFile());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM