Hibernate JSF/Primefaces SelectedGeneradio:值为null
我有一个Primefaces的SelectOneRadio,它扩展了标准SelectOneRadio。 我的问题是,当我选择要下载或不保存或丢失的选项时。我附上密码。为什么会发生这种情况?谢谢 这是我要下载的Bean:Hibernate JSF/Primefaces SelectedGeneradio:值为null,hibernate,jsf,jakarta-ee,jsf-2,primefaces,Hibernate,Jsf,Jakarta Ee,Jsf 2,Primefaces,我有一个Primefaces的SelectOneRadio,它扩展了标准SelectOneRadio。 我的问题是,当我选择要下载或不保存或丢失的选项时。我附上密码。为什么会发生这种情况?谢谢 这是我要下载的Bean: @ManagedBean @RequestScoped public class DownloadFile { private StreamedContent file; @ManagedProperty(value = "#{selecter.selectedRadio}")
@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>
命令组件上的immediate=true属性将仅处理也设置了此属性的输入组件。您的单选按钮输入组件没有它,因此在处理表单提交时会被忽略
此外,如果在更新模型值阶段之前,即在JSF设置{selector.selectedRadio}之前,创建并实例化了{downloadFile}托管bean,则此构造将失败
替换
@ManagedProperty(value = "#{selecter.selectedRadio}")
private Files selectedRadio;
借
并在action方法中访问selectedRadio。如何管理和解决在值之前创建和初始化Bean的问题分配给谁?在这里,我遇到了同样的问题,我采用了您建议的第二种方法,因为他们无法解决第一种方法。我不知道你为什么认为这是个问题。在普通Java中,如何能够在实例构造之前调用实例上的方法?@BalusC:我说的对吗?如果他不调用选择器上方的下载文件托管bean,他的代码就可以工作?@BalusC:当然不,在实例构造之前不可能调用实例上的方法。但你如何看待这个问题,以及我在前面的评论中与你联系的内容似乎非常相似。在这两种情况下,bean都是在获得所有必要数据之前初始化的。我认为你不明白如何正确地修复。@BalusC:我试着做了你建议的替换,但效果不一样。它总是空的。
@ManagedProperty(value = "#{selecter}")
private Selecter selecter;