当我尝试在我的代码中使用 primefaces 消息时,我遇到了问题。当我只是进行测试时,消息运行良好,但是当我执行流文件并尝试在我的 xhtml 中发送消息时,该过程运行良好,但消息没有长大。
我的代码:
public void cadastrar () throws KeyStoreException, CADoesntExistsException, NoSuchAlgorithmException, CertificateException, IOException
{
FacesContext fc = FacesContext.getCurrentInstance();
if((controlador.checkUserIfExist(certificadoModel.getUsername()) == false) || (controlador.checkUserIfExist(certificadoModel.getUsername()) == true && authorization == true))
{
System.out.println("Revoke: " + authorization);
System.out.println("Check: " + controlador.checkUserIfExist(certificadoModel.getUsername()));
//KeyStore keyStore = controlador.cadastrar(certificadoModel);
System.out.println("Bean Class: " + certificadoModel.getMensagem());
/*
ExternalContext ec = fc.getExternalContext();
ec.responseReset();
ec.setResponseContentType("application/x-pkcs12");
//ec.setResponseContentLength(contentLength);
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + certificadoModel.getUsername() + ".p12" + "\"");
OutputStream output = ec.getResponseOutputStream();
keyStore.store(output, certificadoModel.getPassword().toCharArray());
*/
fc.addMessage("mensagem", new FacesMessage(FacesMessage.SEVERITY_INFO, "Teste", ""));
//fc.responseComplete();
controlador.clean(certificadoModel);
}else
fc.addMessage("mensagem", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Um usuário com o mesmo username já está cadastrado!", ""));
controlador.clean(certificadoModel);
}
我的页面代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<ui:decorate template="/resources/template/default.xhtml">
<ui:define name="centro">
<h:form id="cadastro">
<p:panel id="painelCadastro" header="Novo Cadastro" style="margin-bottom:10px;">
<p:messages id="mensagem" autoUpdate="true" closable="true" />
<p:commandButton action="#{certificadoBean.cadastrar}" ajax="false" value="Cadastrar" update="mensagem" />
<p:commandButton outcome="homePage" ajax="false" value="Home Page"/>
</p:panel>
</h:form>
</ui:define>
</ui:decorate>
</html>
@BalusC,这就是我所做的。
xhtml
<p:commandButton value="Cadastrar" action="#{downloadBean.prepareDownload}" update="mensagem" />
<p:messages globalOnly="true" />
<h:outputScript rendered="#{not empty downloadBean.downloadKey}">
window.location = '#{request.contextPath}/download?key=#{downloadBean.downloadKey}';
</h:outputScript>
<p:commandButton outcome="homePage" ajax="false" value="Home Page"/>
</p:panel>
</h:form>
下载Bean类
public class DownloadBean {
private String downloadKey;
public void prepareDownload() throws NamingException {
FacesContext context = FacesContext.getCurrentInstance();
CertificadoModel certificadoModel = new CertificadoModel();
CertificadoControlador controlador = new CertificadoControlador();
if ((controlador.checkUserIfExist(certificadoModel.getUsername()) == false))
{
downloadKey = UUID.randomUUID().toString();
context.getExternalContext().getSessionMap().put(downloadKey, certificadoModel);
context.addMessage("mensagem", new FacesMessage(FacesMessage.SEVERITY_INFO, "Usuário cadastrado com sucesso!", ""));
}else {
context.addMessage("mensagem", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Um usuário com o mesmo username já está cadastrado!", ""));
}
}
public String getDownloadKey() {
return downloadKey;
}
下载Servlet类
public class DownloadServlet extends HttpServlet {
/****/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
String downloadKey = request.getParameter("key");
CertificadoModel certificadoModel = (CertificadoModel) request.getSession().getAttribute(downloadKey);
System.out.println("Teste!");
CertificadoControlador controlador = null;
try {
controlador = new CertificadoControlador();
} catch (NamingException e) {
e.printStackTrace();
}
request.getSession().removeAttribute(downloadKey);
KeyStore keyStore = null;
try {
keyStore = controlador.cadastrar(certificadoModel);
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (CADoesntExistsException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
response.reset();
response.setContentType("application/x-pkcs12");
//response.setContentLength(contentLength);
response.setHeader("Content-Disposition", "attachment; filename=\"" + certificadoModel.getUsername() + ".p12" + "\"");
try {
keyStore.store(response.getOutputStream(), certificadoModel.getPassword().toCharArray());
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
而在 web.xml
<servlet>
<servlet-name>DownloadServlet</servlet-name>
<servlet-class>br.com.certificado.beans.DownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownloadServlet</servlet-name>
<url-pattern>/download</url-pattern>
</servlet-mapping>