0

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.

Simego
  • 431
  • 5
  • 16
  • Where are you throwing this `ValidatorException`? – Luiggi Mendoza May 29 '14 at 16:12
  • oh sorry, in my `ManagedBean` after i press a button in the screen, in a actionListener. – Simego May 29 '14 at 16:14
  • So the problem is for a misconception of actionListener (which usually happens to newcomers of JSF). Check here: [Differences between action and actionListener](http://stackoverflow.com/q/3909267/1065197) and read the accepted answer, it covers the reason of your problem and how to solve it. – Luiggi Mendoza May 29 '14 at 16:19
  • sorry sorry, again, huh, i'm using action in this method, i'll add in the question the following code. – Simego May 29 '14 at 16:21
  • Please update the question and add all the relevant content to reproduce this problem so we could provide a solution. – Luiggi Mendoza May 29 '14 at 16:22
  • Uh... do you at least have a `` (or ``) inside your ``? – Luiggi Mendoza May 29 '14 at 16:25
  • af course, didn't expect it to be so relevant, i'll put it in the question... – Simego May 29 '14 at 16:26
  • Again: put all the necessary code **to reproduce the problem** in your question. Maybe you have some very specific configuration/stuff that makes this fail. – Luiggi Mendoza May 29 '14 at 16:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54739/discussion-between-simego-and-luiggi-mendoza). – Simego May 29 '14 at 17:20

1 Answers1

1

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.