Controller

Creamos el controller para Persona

  • Creamos una clase llamada PersonaController

  • Definimos las anotaciones

@Named
@ViewScoped

implementamos Serializable

public void setPersonaRepository(PersonaRepository personaRepository) {

Agregamos

  • PersonaRepository

  • Entity Persona

  • Método save

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.avbravo.practica.controller;

import com.avbravo.jmoordb.configuration.JmoordbConnection;
import com.avbravo.jmoordbutils.JsfUtil;
import com.avbravo.practicaejb.entity.Persona;
import com.avbravo.practicaejb.repository.PersonaRepository;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;



/**
 *
 * @author avbravo
 */
@Named
@ViewScoped
public class PersonaController implements Serializable{
    @Inject
    PersonaRepository personaRepository;
     Persona persona = new Persona();

    public PersonaRepository getPersonaRepository() {
        return personaRepository;
    }

    public void setPersonaRepository(PersonaRepository personaRepository) {
        this.personaRepository = personaRepository;
    }

    public Persona getPersona() {
        return persona;
    }

    public void setPersona(Persona persona) {
        this.persona = persona;
    }



     @PostConstruct
    public void init() {

//       
//              JmoordbConnection  jmc = new JmoordbConnection.Builder()
//                    .withSecurity(false)                  
//                    .withDatabase("practica")
//                    .withHost("")
//                    .withPort(0)
//                    .withUsername("")
//                    .withPassword("")
//                    .build();
    }

    public String save(){
        try {

       if( personaRepository.save(persona)){
             JsfUtil.successMessage("Guardado");
       }
       else{
            JsfUtil.errorDialog("no se guardo()",personaRepository.getException().toString());
       }
        } catch (Exception e) {
            JsfUtil.errorDialog("save()", e.getLocalizedMessage());
        }

           return "";
            }

}

Last updated

Was this helpful?