简体   繁体   English

JSF 2 - 在复合组件子组件完成操作后,如何执行操作?

[英]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.我仍在学习使用 JSF 2 中的复合组件的一些功能。我对 JSF 1.2 开发经验丰富,最近阅读了《Core ZD52387880E1EA22817A72D37359213》一书。

What I'm trying to do is create a Composite Component that wraps a file upload component (currently using the PrimeFaces <p:fileUpload> ).我要做的是创建一个包含文件上传组件的复合组件(当前使用 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).我需要将上传的文件与会话范围托管 bean 上的基于字符串的键相关联(稍后使用)。 I am trying to provide the key via an attribute on my Composite Component interface named 'uploadedFileKey'.我正在尝试通过名为“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.实现很简单,使用前面提到的 PrimeFaces 文件上传标签。 It requires a managed bean with an event handler which I also created based on the sample code from the PrimeFaces showcase webapp.它需要一个带有事件处理程序的托管 bean,我也是根据 PrimeFaces 展示 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:我不会在这里包含整个 controller bean,但这里是 class 声明:

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

The PrimeFaces file upload is not unlike others that I have seen. PrimeFaces 文件上传与我见过的其他文件没有什么不同。 It uses a custom Filter on the Faces Servlet to get access to the uploading data.它使用 Faces Servlet 上的自定义Filter来访问上传数据。 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.实际的文件上传部分工作正常,上传成功后,我将上传的文件存储在 Tomcat 服务器上的临时文件中。

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.我希望我的复合组件将uploadedFileKey的文件密钥存储在特定会话范围托管 bean 上的Map中,并将上传的File作为 map 值。 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.我只需通过<f:attribute>将密钥作为自定义组件属性传递并让处理程序处理它。 You can get it in the handler method by event.getComponent().getAttributes() .您可以通过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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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