Created
October 30, 2017 17:28
-
-
Save eltonsantos/190036155e04188159005ae718318ee3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class Endereco implements Serializable { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Integer id; | |
private String rua; | |
private String cep; | |
private String cidade; | |
private String estado; | |
@OneToOne(cascade=CascadeType.ALL) | |
private Usuario usuario; | |
public Endereco() { | |
} | |
(...) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Id: <h:outputText value="#{params.id}" /> | |
Id: <h:outputText value="#{params.usuarioId}" /> | |
<br /> | |
Id: <h:outputText value="#{usuarioBean.usuarioId}" /> | |
<br /> | |
<h:outputFormat value="Id:{0}. Nome: {1}."> | |
<f:param value="#{usuarioBean.usuario.id}" /> | |
<f:param value="#{usuarioBean.usuarioId}" /> | |
</h:outputFormat> | |
<br /> | |
<h:form id="endereco"> | |
Rua: <h:inputText id="rua" value="#{enderecoBean.endereco.rua}" /> | |
<br /> | |
CEP: <h:inputText id="cep" value="#{enderecoBean.endereco.cep}" /> | |
<br /> | |
Estado: <h:inputText id="estado" value="#{enderecoBean.endereco.estado}" /> | |
<br /> | |
Cidade: <h:inputText id="cidade" value="#{enderecoBean.endereco.cidade}" /> | |
<br /> | |
<h:commandButton type="submit" value="Salvar Endereco" action="#{enderecoBean.salvarEndereco()}" /> | |
</h:form> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ViewScoped | |
@ManagedBean(name="enderecoBean") | |
public class EnderecoController implements Serializable { | |
@Inject | |
private EntityManager manager; | |
private Endereco endereco; | |
private Usuario usuario; | |
private Integer usuarioId; | |
public EnderecoController(){ | |
this.endereco = new Endereco(); | |
} | |
public List<Endereco> listarEnderecos(){ | |
return manager.createNativeQuery("SELECT * FROM endereco", Endereco.class).getResultList(); | |
} | |
public String salvarEndereco(){ | |
manager.getTransaction().begin(); | |
System.out.println("UsuarioId: " +usuarioId); | |
Usuario usuario = manager.find(Usuario.class, this.usuarioId); | |
System.out.println("Usuario: " +usuario); | |
this.endereco.setUsuario(usuario); | |
manager.persist(endereco); | |
manager.getTransaction().commit(); | |
manager.close(); | |
return "endereco?faces-redirect=true"; | |
} | |
(...) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<f:metadata> | |
<f:viewParam name="usuarioId" value="#{usuarioBean.usuario.id}" /> | |
</f:metadata> | |
(...) | |
<h:form> | |
<h:dataTable value="#{usuarioBean.listarUsuarios()}" var="usuario"> | |
<h:column> | |
<f:facet name="header">Id</f:facet> | |
<h:outputText value="#{usuario.id}"></h:outputText> | |
</h:column> | |
<h:column> | |
<f:facet name="header">Nome</f:facet> | |
<h:outputText value="#{usuario.nome}"></h:outputText> | |
</h:column> | |
<h:column> | |
<f:facet name="header">Opcoes</f:facet> | |
<h:commandButton value="Cadastrar endereço" action="#{usuarioBean.executar}"> | |
<f:param name="usuarioId" value="#{usuarioBean.usuario.id}" /> | |
</h:commandButton> | |
</h:column> | |
</h:dataTable> | |
</h:form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class Usuario implements Serializable { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Integer id; | |
private String nome; | |
@OneToOne(mappedBy="usuario") | |
private Endereco endereco; | |
public Usuario() { | |
} | |
(...) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ViewScoped | |
@ManagedBean(name="usuarioBean") | |
public class UsuarioController implements Serializable { | |
@Inject | |
private EntityManager manager; | |
private Usuario usuario; | |
private Endereco endereco; | |
private String usuarioId; | |
public UsuarioController(){ | |
this.usuario = new Usuario(); | |
} | |
public List<Usuario> listarUsuarios(){ | |
return manager.createNativeQuery("SELECT * FROM usuario", Usuario.class).getResultList(); | |
} | |
public String salvarUsuario(){ | |
manager.getTransaction().begin(); | |
manager.persist(usuario); | |
manager.getTransaction().commit(); | |
manager.close(); | |
return "index?faces-redirect=true"; | |
} | |
public String executar(){ | |
System.out.println("------------------ Passa aqui"); | |
FacesContext fc = FacesContext.getCurrentInstance(); | |
this.usuarioId = getUsuarioParam(fc); | |
System.out.println("------------------ UsuarioId do Usuario: " +usuarioId); | |
return "endereco?faces-redirect=true"; | |
} | |
public String getUsuarioParam(FacesContext fc){ | |
Map<String,String> params = fc.getExternalContext().getRequestParameterMap(); | |
return params.get("usuarioId"); | |
} | |
(...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Olá Elton,
Vi sua duvida no grupo JSF Brasil.
Então, para resolver o seu problema de envio de parametros para uma outra view ou managed Bean. Ou você utiliza params ou um flash scope.Segue abaixo o exemplo:
Na Classe UsuarioController:
`
public String executar(){
System.out.println("------------------ Passa aqui");
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext().
this.usuarioId = getUsuarioParam(fc).getFlash().put("usuario", this.usuario);
System.out.println("------------------ UsuarioId do Usuario: " +usuarioId);
}
Na Classe EnderecoController
private Usuario usuario = new Ususario();
@PostConstruct
public void init(){
try{
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
if(!ec.getFlash().isEmpty()){
usuario = (Usuario) ec.getFlash().get("usuario");
}
}catch(Exception e){
System.out.println("Não foi possivel carregar o usuario");
}
}
`