页面未显示来自数据库的数据

时间:2019-01-07 04:40:19

标签: eclipse maven jpa jsf

我正在Web开发人员的Eclipse Java EE IDE中使用JSF,JPA和Maven启动具有Java SE Development 64位(版本8,编译1.8.0)的Java Web应用程序。版本:Oxygen.3a发行版(4.7.3a。)MySQL Workbench 8.0 CE中的Spring数据库。 Apache Tomcat v8.5服务器。

正如我所说的,我开始学习,所以我在寻找一个教程,并且在做的时候。它没有显示数据。我无法确定错误可能在哪里。如果有人可以帮忙。

在控制台中,没有错误出现。通过单击服务器上的Run As / Run。页面http: // localhost: 8080 / JSF-Project /仅显示标题:

  

Lista客户

     

Id Nombre Rut Razon Social ....

persistence.xml

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
  version="2.1">

  <persistence-unit name="PERSISTENCE">
    <description> Crud Hibernate JPA</description>
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <properties>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/basedatos_schema" />
      <property name="javax.persistence.jdbc.user" value="root" />
      <property name="javax.persistence.jdbc.password" value="xxxxxx" />
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.hbm2ddl.auto" value="update" />
    </properties>

  </persistence-unit>

</persistence>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.emaus.projectjsf</groupId>
  <artifactId>JSF-Project</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>

  <name>JSF-Project Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.9-rc</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.3.6.Final</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.17</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.17</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>JSF-Project</finalName>
    <pluginManagement>
        <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>

        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

ClienteBean.java

package com.xxx.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import com.xxx.model.Cliente;
import com.xxx.dao.ClienteDAO;

@ManagedBean(name="clienteBean")
@RequestScoped

public class ClienteBean {

    public String nuevo(){
        Cliente c = new Cliente();
        Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        sessionMap.put("cliente", c);
        return "/faces/nuevo.xhtml";
    }

    public String guardar(Cliente cliente) {
        //guarda la fecha de registro
        Date fechaActual= new Date();
        cliente.setFechacreacion(new java.sql.Date(fechaActual.getTime()));

        ClienteDAO clienteDAO = new ClienteDAO();
        clienteDAO.guardar(cliente);
        return "/faces/index.xhtml";
    }

    public List<Cliente> obtenerClientes(){
        ClienteDAO clienteDAO = new ClienteDAO();



        return clienteDAO.obtenerClientes();

    }

    public String editar(Long idcliente) {
        ClienteDAO clienteDAO = new ClienteDAO();
        Cliente c= new Cliente();
        c=clienteDAO.buscar(idcliente);
        System.out.println(c);

        Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        sessionMap.put("cliente", c);
        return "/faces/editar.xhtml";
    }

    public String actualizar(Cliente cliente) {
        //guarda la fecha de actualización
        Date fechaActual= new Date();
        cliente.setFechaactualizacion(new java.sql.Date(fechaActual.getTime()));

        ClienteDAO clienteDAO = new ClienteDAO();
        clienteDAO.editar(cliente);
        return "/faces/index.xhtml";
    }

    //eliminar cliente
    public String eliminar(Long idcliente) {
        ClienteDAO clienteDAO = new ClienteDAO();
        clienteDAO.eliminar(idcliente);
        System.out.println("Cliente eliminado");
        return "/faces/index.xhtml";
    }
}

ClienteDAO.java

package com.emaus.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.emaus.model.Cliente;
import com.emaus.model.JPAUtil;

public class ClienteDAO {
    EntityManager entity = JPAUtil.getEntityManagerFactory().createEntityManager();

    //guardar cliente
    public void guardar(Cliente cliente) {
        entity.getTransaction().begin();
        entity.persist(cliente);
        entity.getTransaction().commit();
        //JPAUtil.shutdown();
    }

    //editar cliente
    public void editar (Cliente cliente) {
        entity.getTransaction().begin();
        entity.merge(cliente);
        entity.getTransaction().commit();
        //JPAUtil.shutdown();
    }

    //buscar cliente
    public Cliente buscar(Long idcliente) {
        Cliente c = new Cliente();
        c = entity.find(Cliente.class,idcliente);
        //JPAUtil.shutdown();
        return c;
    }

    //eliminar cliente
    public void eliminar (Long idcliente) {
        Cliente c = new Cliente();
        c = entity.find(Cliente.class,idcliente);
        entity.getTransaction().begin();
        entity.remove(c);
        entity.getTransaction().commit();

    }


    //obtener todos los clientes
    public List<Cliente> obtenerClientes(){
        List<Cliente> listaClientes = new ArrayList<>();
        Query q = entity.createQuery("SELECT c FROM Cliente c");
        listaClientes = q.getResultList();
        return listaClientes;
    }


}

index.xhtml

<!DOCTYPE HTML>
<html lang="es" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <meta charset="utf-8" name="viewport"
      content="width=device-width, initial-scale=1"
      http-equiv="X-UA-Conpatible" />
    <title>Plantilla Básica JSF</title>
</h:head>
<h:body>
    <h2>
     <h:outputText>Lista Clientes</h:outputText> 

    </h2>
    <h:form>

    <h:commandButton action="#{clienteBean.nuevo()}" value="Nuevo Cliente"></h:commandButton>

    <h:dataTable value="#{clienteBean.obtenerClientes()}" var="cliente" border="1" >
        <h:column>
            <f:facet name="header">Id</f:facet>
            <h:outputText value="#{cliente.idcliente}"></h:outputText>
        </h:column>
        <h:column>
            <f:facet name="header">Nombre</f:facet>
            <h:outputText value="#{cliente.nombre}"></h:outputText>
        </h:column>
        <h:column>
            <f:facet name="header">Rut</f:facet>
            <h:outputText value="#{cliente.rut}"></h:outputText>
        </h:column>
        <h:column>
            <f:facet name="header">Razón Social</f:facet>
            <h:outputText value="#{cliente.razon_social}"></h:outputText>
        </h:column>
        <h:column>

        <h:column>
            <f:facet name="header">Editar</f:facet>
            <h:commandButton action="#{clienteBean.editar(cliente.idcliente)}" value="Editar"></h:commandButton>
        </h:column>
        <h:column>
            <f:facet name="header">Eliminar</f:facet>
            <h:commandButton action="#{clienteBean.eliminar(cliente.idcliente)}" value="Eliminar"></h:commandButton>
        </h:column>
    </h:dataTable>
    </h:form>

</h:body>
</html>

-----------------

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
        http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        version="3.1">

        <display-name>Archetype Created Web Application</display-name>

        <context-param>
                <param-name>javax.faces.PROJECT_STAGE</param-name>
                <param-value>Development</param-value>
        </context-param>
        <servlet>
                <servlet-name>Faces Servlet</servlet-name>
                <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
                <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
                <servlet-name>Faces Servlet</servlet-name>
                <url-pattern>/faces/*</url-pattern>
        </servlet-mapping>

        <servlet-mapping>
                <servlet-name>Faces Servlet</servlet-name>
                <url-pattern>*.jsf</url-pattern>
        </servlet-mapping>

        <session-config>
                <session-timeout>30</session-timeout>
        </session-config>

        <welcome-file-list>
                <welcome-file>index.xhtml</welcome-file>
        </welcome-file-list>
</web-app>

0 个答案:

没有答案