带有bootstrap navbar的页面在被重定向到ManagedBean之后不能正常工作,我正在使用primefaces组件

时间:2016-02-29 18:45:42

标签: twitter-bootstrap jsf primefaces

我在一个名为header.xhtml的合成页面(我的模板名为common.xhtml的菜单)中使用bootstrap实现了一个导航栏。 我还有一个页面使用模板,名为principal.xhtml和login.xhtml;第一个只显示静态数据,第二个是一个loggin页面,它有一个名为LoginController.java的ManagedBean,它生成一个返回主体。但问题是,当发生这种情况时,在principal.xhtml上的导航栏不会特别工作下拉列表,但当我通过URL localhost:8080 / ProyectName / principal.jsf执行它时导航栏工作。 请帮忙!

  

名为menu.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:b="http://bootsfaces.net/ui"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui">


<ui:composition>
    <nav class="navbar navbar-default ">
        <div class="container-fluid ck-navbar">
            <div class="navbar-header ">
                <button class="navbar-toggle" data-toggle="collapse" data-target="#menu">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a href="" class="navbar-brand ck-navbar-brand">
                    <div class="logo">BRAND NAME</div>              
                </a>
            </div>
            <div class="collapse navbar-collapse" id="menu">
                <ul class="nav navbar-nav navbar-right">
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Empleados <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">Agregar Empleados</a></li>
                            <li class="divider"></li>
                            <li><a href="#">Listar Emplados</a></li>                    
                        </ul>
                    </li>
                    <li><a href="#">Guias</a></li>              
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Facturas <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">Agregar Facturas</a></li>
                            <li class="divider"></li>
                            <li><a href="#">Listar Facturas</a></li>                    
                        </ul>
                    </li>                                           
                    <li><a href="#">Reportes</a></li>
                    <li><a href="#">Estadistica</a></li>
                    <li><a href="#">Logout</a></li>
                </ul>
            </div>
        </div>      
    </nav>      
    <br/>           
</ui:composition>

  

common.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:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html"> 
    <h:head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />    
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="description" content="" />

        <h:outputStylesheet name="css/bootstrap.min.css"/>
        <h:outputStylesheet name="css/bootstrap-theme.min.css"/>
        <h:outputStylesheet name="css/main.css"/>
        <h:outputScript name="js/vendor/modernizr-2.8.3.min.js"></h:outputScript>
        <h:outputStylesheet name="css/font-awesome.css"></h:outputStylesheet>
        <title>
            <ui:insert name="titulo">
                Brand Name
            </ui:insert>
        </title>    
    </h:head> 
    <h:body> 
        <header>
            <ui:insert name="header">
                <ui:include src="/templates/menu.xhtml" />
            </ui:insert>
        </header>
        <br/>   
            <ui:insert name="content">
                <ui:include src="/templates/contents.xhtml" />
            </ui:insert>    
        <br/>
        <hr/>
        <footer>
            <ui:insert name="footer">
                <ui:include src="/templates/footer.xhtml"></ui:include>
            </ui:insert>        
        </footer>

        <h:outputScript name="js/vendor/bootstrap.min.js" />
        <h:outputScript name="js/main.js" />

        <h:outputScript library="primefaces" name="jquery/jquery.js" target="head"/>
        <h:outputScript library="primefaces" name="jquery/jquery-plugins.js" target="head"/>
        <h:outputScript library="primefaces" name="primefaces.js" target="head" />
        <h:outputScript library="javax.faces" name="jsf.js" target="head"/>
    </h:body> 
    </html>
  

login.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:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui">


<ui:composition template="/templates/common.xhtml">
        <ui:define name="header"></ui:define>

        <ui:define name="content">
        <section class="container">         
            <h2 class="text-center">Sistema de Brand</h2>
            <h:form styleClass="form-horizontal">
            <p:messages autoUpdate="true" showDetail="true"/>
            <div class="form-group">
                <h:outputLabel for="txtUsuario" value="Usuario" styleClass="col-sm-2 control-label"/>
                <div class="col-sm-10">
                <h:inputText id="txtUsuario" label="Usuario" value="#{loginMb.usuario.username}" required="true" styleClass="form-control"/>
                </div>
            </div>
            <div class="form-group">
                <h:outputLabel for="txtPassword" value="Contraseña" styleClass="col-sm-2 control-label"/>
                <div class="col-sm-10">                 
                <h:inputText id="txtPassword"  label="Contraseña" value="#{loginMb.usuario.password}" required="true" styleClass="form-control"/>
                </div>
            </div>                  
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <p:commandButton icon="fa fa-sign-in" value="Ingresar" styleClass="btn btn-primary btn-block" action="#{loginMb.iniciarSesion()}"></p:commandButton>
                </div>                  
            </div>              
            </h:form>
        </section>  
        </ui:define>

</ui:composition>
</html>
  

LoginController.java

package com.webstotales.bean;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.webstotales.entidades.Usuario;
import com.webstotales.servicio.UsuarioFacadeLocal;



@ViewScoped
@ManagedBean(name = "loginMb")
public class LoginController implements Serializable{
@EJB
private UsuarioFacadeLocal ejbUsuarioFacade;

private Usuario usuario;

@PostConstruct
public void init(){
    usuario=new Usuario();
}

public Usuario getUsuario() {
    return usuario;
}

public void setUsuario(Usuario usuario) {
    this.usuario = usuario;
}

public String iniciarSesion(){
    Usuario us;
    String redireccion=null;
    us=ejbUsuarioFacade.iniciarSesion(usuario);
    if(us != null){
        redireccion="/principal";
    }else{
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Aviso", "Contraseña incorrecta"));
    }       
    return redireccion;
}
}
  

principal.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:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui">

<ui:composition template="/templates/common.xhtml">
        <ui:define name="titulo">mensaje</ui:define>
        <ui:define name="content">
            <section class="container">
            <h2>Hola Bienvenido</h2>            

            </section>
        </ui:define>

</ui:composition>
</html>

我使用的是primefaces v5.0 bootstrap v3.3.1; 我希望你们中的一些人可以帮助我。 问候。

1 个答案:

答案 0 :(得分:0)

你可以尝试使用

public void iniciarSesion(){ Usuario us; us=ejbUsuarioFacade.iniciarSesion(usuario); if(us != null){ FacesContext.getCurrentInstance().getExternalContext().redirect("principal.xhtml"); }else{ FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Aviso", "Contraseña incorrecta")); }
}