2013-04-04 89 views
0

我有一个Primefaces的SelectOneRadio,它扩展了标准的SelectOneRadio。 我的问题是,当我选择我想要下载这个选项或不保存或丢失的选项。我附上了代码。为什么会发生?由于JSF/Primefaces SelectedOneRadio:值为空

这是我下载豆:

@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. 
    } 
} 

这是我selecter豆:

@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> 

回答

0

命令组件上的属性将仅处理输入组件,其中也有具有此属性集。您的单选按钮输入组件没有它,因此在处理表单提交时忽略它。

此外,如果被创建#{downloadFile}托管bean该构建将失败,并且更新模型值阶段之前实例化(即前#{selecter.selectedRadio}是被设定JSF)。

通过

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

替换

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

和操作方法访问selectedRadio代替。

+0

如何管理和解决在赋值之前发生的Bean的创建和初始化问题? 在这里,我遇到了同样的问题,我遵循了第二种方法,因为他们无法解决第一个问题。 http://stackoverflow.com/questions/15633818/login-with-hibernate-and-store-username-logged – 2013-04-04 13:03:49

+0

我不知道为什么你把它看作是一个问题。在构造实例之前,“纯Java”如何调用实例上的方法? – BalusC 2013-04-04 13:16:54

+0

@BalusC:我是否有权说如果他没有在'selecter'上方的任何位置调用他的'downloadFile'托管bean,他的代码就可以工作? – 2013-04-04 13:32:31