Simego
Simego

Reputation: 431

JSF show thrown message in h:message

i searched everywhere and i'm trying everything, no success with a real exception..
i need to throw an exception so it 'ignore' everything after the method point (a single return wouldn't be enough cuz there are more methods above) so I found something like this:

throw new javax.faces.validator.ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, ex instanceof ValidationException ? ex.getMessage() : MessageProvider.getMessage("sales.insert.error"), null));

EDIT 1
(This is done inside a void method in my ManagedBean (@ViewScoped), the method is called from a p:commandButton with a action="#{something}".

EDIT 2
The button:

<p:commandLink id="finalizarBtn" action="#{vendaMB.finalizarVenda()}"  update="@form" process="@this, vendaList" styleClass="btn btn-regalo margin-side-5 btn-block" title="#{msg['sales.add']}" style="width: 500px; display: inline-block; margin: 20px 0px;" ><i class="fa fa-fw fa-2x fa-check"></i></p:commandLink>

And the complete method:

public void finalizarVenda() throws ValidatorException {
    if(vendaProdutos.size() < 1) {
        FacesUtil.addErrorMessage("vendaForm:produto", "");
        return;
    }
    venda.setDataVenda(new Date());
    try {
        venda = vendaBO.incluir(venda, vendaProdutos);
    } catch(Exception ex) {
        venda.setDataVenda(null);
        vendaBO.getDB().rollbackTransaction();
        throw new javax.faces.validator.ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, ex instanceof ValidationException ? ex.getMessage() : MessageProvider.getMessage("sales.insert.error"), null));
    }
    FacesUtil.addInfoMessage(MessageProvider.getMessage("sales.insert.success"));
    novaVenda();
}

The messages component:

<p:messages id="messages" autoUpdate="true" globalOnly="true" showSummary="true" showDetail="false" escape="false" rendered="true" closable="true" />


i use this inside my try/catch block, it throws the exception as expected (the message) but it's never rendered at the view, if i manually add the FacesMessage to the FacesContext and throw the exception that is being catch, the same happens, no error messages at the view, example:

try {
  //something
} catch (Exception ex) {
  FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("something"));
  throw ex;
}

Both solutions doesn't work, i'm using JSF, PrimeFaces and Tomcat 8.

Upvotes: 0

Views: 2083

Answers (1)

user3688900
user3688900

Reputation: 36

Throwing a ValidatorException from your managed bean will not set the message. Will the following work:

public void finalizarVenda() throws ValidatorException {
    if(vendaProdutos.size() < 1) {
        FacesUtil.addErrorMessage("vendaForm:produto", "");
        return;
    }
    venda.setDataVenda(new Date());
    try {
        venda = vendaBO.incluir(venda, vendaProdutos);
        FacesUtil.addInfoMessage(MessageProvider.getMessage("sales.insert.success"));
        novaVenda();
    } catch(Exception ex) {
        venda.setDataVenda(null);
        vendaBO.getDB().rollbackTransaction();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex instanceof ValidationException ? ex.getMessage() : MessageProvider.getMessage("sales.insert.error"), null));
    }
}

If the novaVenda() method can also throw an exception you might also want to wrap that in a try catch block and throw the caught exception.

Upvotes: 1

Related Questions