Dificuldades para redirecionar pagina JSF
Estou com dificuldades para redirecionar após efetuar cadastro. Segue código:
ClienteBean
listar.xhtml
inserir.xhtml
ClienteBean
package br.com.devmedia.beans;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import br.com.devmedia.dao.ClientesDAO;
import br.com.devmedia.dto.ClienteDTO;
@ManagedBean(name=cli)
@RequestScoped
public class ClienteBean {
private ClienteDTO dto = new ClienteDTO();
private ClientesDAO dao;
private DataModel<ClienteDTO> clientes;
public ClienteDTO getDto(){
return this.dto;
}
public void setDto(ClienteDTO dto){
this.dto = dto;
}
public String save() throws ClassNotFoundException, Exception{
String retorno = erro;
dao = new ClientesDAO();
if(dao.insert(dto)){
retorno = listar;
}
return retorno;
}
public DataModel<ClienteDTO> getClientes() throws ClassNotFoundException, Exception{
dao = new ClientesDAO();
List<ClienteDTO> lista = dao.getAll();
clientes = new ListDataModel<ClienteDTO>(lista);
return clientes;
}
public void setClientes(DataModel<ClienteDTO> clientes){
this.clientes = clientes;
}
}
listar.xhtml
<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html xmlns=http://www.w3.org/1999/xhtml
xmlns:ui=http://java.sun.com/jsf/facelets
xmlns:h=http://java.sun.com/jsf/html
xmlns:p=http://primefaces.prime.com.tr/ui
xmlns:f=http://java.sun.com/jsf/core>
<p:fieldset legend=Clientes>
<h:form>
<h:dataTable value=#{cli.clientes} var=cliente>
<h:column>
<f:facet name=header>
<h:outputText value=Código />
</f:facet>
<h:outputText value=#{cliente.id_cliente} />
</h:column>
<h:column>
<f:facet name=header>
<h:outputText value=#{labels.Nome} />
</f:facet>
<h:outputText value=#{cliente.nome} />
</h:column>
<h:column>
<f:facet name=header>
<h:outputText value=#{labels.email} />
</f:facet>
<h:outputText value=#{cliente.email} />
</h:column>
</h:dataTable>
<h:commandButton action=inserir value=Novo />
</h:form>
</p:fieldset>
</html>
inserir.xhtml
<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html xmlns=http://www.w3.org/1999/xhtml
xmlns:ui=http://java.sun.com/jsf/facelets
xmlns:h=http://java.sun.com/jsf/html
xmlns:f=http://java.sun.com/jsf/core>
<h:form>
<h:panelGrid columns=2>
<h:outputText value=#{labels.Nome} />
<h:inputText value=#{cli.dto.nome} id=txtNome required=true requiredMessage=#{labels.campo_vazio}></h:inputText>
<h:outputText value=#{labels.email} />
<h:inputText value=#{cli.dto.email} id=txtEmail required=true requiredMessage=#{labels.campo_vazio}></h:inputText>
<h:commandButton action=#{cli.save} value=Cadastrar /> <h:commandButton value=Limpar type=reset />
<h:message for=txtNome style=color: #369;/>
<h:message for=txtEmail style=color: #369;/>
</h:panelGrid>
</h:form>
</html>
Allan Barros
Curtidas 0
Respostas
Davi Costa
15/07/2012
Vc configurou na faces-config.xml?
Qual sua versão do jsf?
att Davi
Qual sua versão do jsf?
att Davi
GOSTEI 0
Filipe Souza
15/07/2012
Basta adicionar o parametro faces-redirect=true na string outcome.
Exemplo:
Se for ao executar um método do ManagedBean, ficaria assim:
Exemplo:
<h:form>
<h:commandButton action=page1?faces-redirect=true value=Page1 />
</h:form>
Se for ao executar um método do ManagedBean, ficaria assim:
public String alterar(){
//código que altera
return paga1?faces-redirect=true; //redirect
}
GOSTEI 0