0

我有一个扩展标准 SelectOneRadio 的 Primefaces 的 SelectOneRadio。我的问题是,当我选择要下载或不保存或丢失的选项时。我附上代码。为什么会这样?谢谢

这是我的 Bean 供下载:

@ManagedBean
@RequestScoped
public class DownloadFile {

private StreamedContent file;
@ManagedProperty(value = "#{selecter.selectedRadio}")
private Files selectedRadio;

//all getters/setters methods
....
....


public DownloadFile() throws IOException {          
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
    response.reset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
    response.setContentType("application/xml"); // Check http://www.w3schools.com/media/media_mimeref.asp for all types. Use if necessary ServletContext#getMimeType() for auto-detection based on filename.
    response.setHeader("Content-disposition", "attachment; filename=\"immagine.jpg\""); 

    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        //Qui va sostituita la risorsa con pathname+/selectedRadio (pathname la otterremo da una query)
        System.out.println("Prova::" + selectedRadio);
        //String s= selectedRadio.getPathname()+"/"+selectedRadio.getNome();
        input = new BufferedInputStream(externalContext.getResourceAsStream("/downloaded_optimus.jpg"));
        output = new BufferedOutputStream(response.getOutputStream());

        byte[] buffer = new byte[10240];
        for (int length; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
        }
    } catch(Exception e) {
        output.close();
        input.close();
    }

    facesContext.responseComplete(); // Important! Else JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
    }
}

这是我的选择器 bean:

@ManagedBean
@SessionScoped
public class Selecter {
@ManagedProperty(value = "#{sessionHandler.db}")
private Session db;
private List<Files> res= new ArrayList<Files>();
private Files selectedRadio;
//all getters/setters methods ....

 @PostConstruct
 public void init(){
     db.beginTransaction();
     ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
     Map<String, Object> sessionMap = externalContext.getSessionMap();
     Query query = db.createQuery( "from Utenti where username= :name" );
     query.setParameter("name", (String)(sessionMap.get("username")));
     List<Utenti>user= query.list();

     for(Utenti a : user){
        Iterator<Files> it = a.getFileses().iterator();
        while (it.hasNext()){
            res.add( it.next());
        }
     }
     db.getTransaction().commit();
 }

}

这是我的文件 .xhtml

h2>Seleziona dall'elenco il file che vuoi scaricare</h2>
<h:form enctype="multipart/form-data">

<p:outputPanel id="customPanel">  
    <p:selectOneRadio id = "radioID" value="#{selecter.selectedRadio}" layout="pageDirection" >
       <f:selectItems value="#{selecter.res}" var="item" itemLabel="#{item.nome}"  itemValue="#{item}" />
    </p:selectOneRadio>

    <p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false" closable="false" resizable="false">  
        <p:graphicImage value="/design/ajax_loading_bar.gif" />  
    </p:dialog>  
    <br></br>       
    <br></br>

    <p:commandButton id="downloadLink" value="Download" ajax="false" immediate="true"
    icon="ui-icon-arrowthichk-s">  
        <p:fileDownload value="#{downloadFile.file}" />  
    </p:commandButton>  


</p:outputPanel>
4

1 回答 1

0

命令组件上的immediate="true"属性将仅处理具有此属性集的输入组件。您的单选按钮输入组件没有它,因此在处理表单提交期间被忽略。

此外,如果在更新模型值阶段之前(即在JSF 设置之前)创建#{downloadFile}和实例化托管 bean,则此构造将失败。#{selecter.selectedRadio}

代替

@ManagedProperty(value = "#{selecter.selectedRadio}")
private Files selectedRadio;

经过

@ManagedProperty(value = "#{selecter}")
private Selecter selecter;

并改为访问selectedRadio操作方法中的。

于 2013-04-04T12:39:29.233 回答