How to change page after inserting object in database?

Asked

Viewed 982 times

2

I created a JSF page with a client form, after saving client by pressing the sign up button. The object persisted in the BD, and consequently I would like you to change pages going to the index.xhtml or clear form fields to continue registering customers.

cadastroCliente.xhtml

<form>
    <!-- inputTexts referentes ao cliente. Aqui-->
    <p:commandButton value="Cadastrar" id="cadastrar" ajax="false" 
        style="margin-top: 25px; margin-bottom:7px;"
        action="#{clienteBean.inserir}">
    </p:commandButton>
</form>

java customers.

public void inserir(){
    dataCadastro = new Date();
    Session session = DAOHibernateUtil.getSessionFactory().getCurrentSession();
    try{
        session.beginTransaction();
        if(!isCpfValido(session)){
            FacesContext.getCurrentInstance().addMessage("cpfmessage", new FacesMessage(FacesMessage.SEVERITY_WARN,"CPF já cadastrado", "este CPF já esta sendo utilizado!"));
        }else{
            Cliente cliente = this;
            session.save(cliente);
            session.getTransaction().commit();
            cliente = new Cliente();
        }
    }catch(RuntimeException e){
        session.getTransaction().rollback();
    }finally{
        if(session.isConnected()){
            session.close();
        }
    }
}

I am starting now with JSF porting if my code is wrong I am grateful for help to fix, but this code is working, persisting in the database correctly.

The problem is how to open another page or clear the inputtext form after clicking the sign up button.

1 answer

4


I suggest you take a look at official tutorial on JSF of Java EE, in particular in the chapter on Navigation Model from JSF. This is a fairly complex issue.

To give you a pragmatic response, your method must return a String with the Outcome of the action.

public String inserir() {
   // ...
   return "index";
}

In that case there is navigation implicit of view cadastroCliente to the view index.

It is also possible to declare navigation rules explicit in configuration files such as faces-config.xml:

<navigation-rule>
    <from-view-id>/cadastroCliente.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>success</from-outcome>
        <to-view-id>/index.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

In that case, every time Outcome of an action in cadastroCliente.xhtml for success will be presented to view index.

  • Anthony I use faces-config.xml. I implemented the "index" Return, it worked in parts because it reloaded the page to the index, but the url in the browser did not change, continued with the client registration url. Apparently just inflated html, but there was no page redirection

  • Thanks for the documentation.

  • Juarez, another of the many details of the navigation model. By default the JSF makes forward, to make redirect you need to declare this explicitly. With implicit navigation use index?faces-redirect=true, with explicit navigation use <redirect/>.

  • Thank you Anthony, problem solved, I used <redirect/> in faces-config.xml and returning the page Outcome in the insert method, and with this same solution I can make a redirect to the same page and the fields are clean. Thank you! is bone this JSF, I will kill myself in the documentation, grateful!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.