2

我有一个带有 EJB 和 Web 模块的 Java EE 应用程序。我的工作基于本教程:http ://wiki.netbeans.org/DevelopJavaEE6App ,但我的项目由书店组成。

我已经创建了所有的选择功能(在首页显示产品,在数据库中搜索书籍),但现在我想制作一个注册表单。

我想我忘记了代码中的某些内容,但我不知道是什么。

我有一个实体、EJB、register.xhtml、managedBean 和 sessionBean。如果有人可以帮助我,请这样做。

Clients我的表的实体:

package com.boutique.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Barthelemius
 */
@Entity
@Table(name = "clients")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Clients.findAll", query = "SELECT c FROM Clients c"),
    @NamedQuery(name = "Clients.findByIdClient", query = "SELECT c FROM Clients c WHERE c.idClient = :idClient"),
    @NamedQuery(name = "Clients.findByEmail", query = "SELECT c FROM Clients c WHERE c.email = :email"),
    @NamedQuery(name = "Clients.findByNom", query = "SELECT c FROM Clients c WHERE c.nom = :nom"),
    @NamedQuery(name = "Clients.findByPassword", query = "SELECT c FROM Clients c WHERE c.password = :password"),
    @NamedQuery(name = "Clients.findByPrenom", query = "SELECT c FROM Clients c WHERE c.prenom = :prenom"),
    @NamedQuery(name = "Clients.findByAdresse", query = "SELECT c FROM Clients c WHERE c.adresse = :adresse"),
    @NamedQuery(name = "Clients.findByCp", query = "SELECT c FROM Clients c WHERE c.cp = :cp"),
    @NamedQuery(name = "Clients.findByVille", query = "SELECT c FROM Clients c WHERE c.ville = :ville"),
    @NamedQuery(name = "Clients.findByTel", query = "SELECT c FROM Clients c WHERE c.tel = :tel"),
    @NamedQuery(name = "Clients.findByMobile", query = "SELECT c FROM Clients c WHERE c.mobile = :mobile"),
    @NamedQuery(name = "Clients.findByCarteFidelite", query = "SELECT c FROM Clients c WHERE c.carteFidelite = :carteFidelite"),
    @NamedQuery(name = "Clients.findByDateNaissance", query = "SELECT c FROM Clients c WHERE c.dateNaissance = :dateNaissance"),
    @NamedQuery(name = "Clients.findByDateCompte", query = "SELECT c FROM Clients c WHERE c.dateCompte = :dateCompte")})
public class Clients implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "ID_CLIENT")
    private Integer idClient;
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
    @Size(max = 233)
    @Column(name = "EMAIL")
    private String email;
    @Size(max = 233)
    @Column(name = "NOM")
    private String nom;
    @Size(max = 233)
    @Column(name = "PASSWORD")
    private String password;
    @Size(max = 233)
    @Column(name = "PRENOM")
    private String prenom;
    @Size(max = 233)
    @Column(name = "ADRESSE")
    private String adresse;
    @Size(max = 233)
    @Column(name = "CP")
    private String cp;
    @Size(max = 233)
    @Column(name = "VILLE")
    private String ville;
    @Column(name = "TEL")
    private Integer tel;
    @Column(name = "MOBILE")
    private Integer mobile;
    @Column(name = "CARTE_FIDELITE")
    private Boolean carteFidelite;
    @Basic(optional = false)
    @NotNull
    @Column(name = "DATE_NAISSANCE")
    @Temporal(TemporalType.DATE)
    private Date dateNaissance;
    @Basic(optional = false)
    @NotNull
    @Column(name = "DATE_COMPTE")
    @Temporal(TemporalType.DATE)
    private Date dateCompte;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "idClient")
    private Factures factures;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "clients")
    private List<LignesFacture> lignesFactureList;

    public Clients() {
    }

    public Clients(Integer idClient) {
        this.idClient = idClient;
    }

    public Clients(Integer idClient, Date dateNaissance, Date dateCompte) {
        this.idClient = idClient;
        this.dateNaissance = dateNaissance;
        this.dateCompte = dateCompte;
    }

    public Integer getIdClient() {
        return idClient;
    }

    public void setIdClient(Integer idClient) {
        this.idClient = idClient;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public String getAdresse() {
        return adresse;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

    public String getCp() {
        return cp;
    }

    public void setCp(String cp) {
        this.cp = cp;
    }

    public String getVille() {
        return ville;
    }

    public void setVille(String ville) {
        this.ville = ville;
    }

    public Integer getTel() {
        return tel;
    }

    public void setTel(Integer tel) {
        this.tel = tel;
    }

    public Integer getMobile() {
        return mobile;
    }

    public void setMobile(Integer mobile) {
        this.mobile = mobile;
    }

    public Boolean getCarteFidelite() {
        return carteFidelite;
    }

    public void setCarteFidelite(Boolean carteFidelite) {
        this.carteFidelite = carteFidelite;
    }

    public Date getDateNaissance() {
        return dateNaissance;
    }

    public void setDateNaissance(Date dateNaissance) {
        this.dateNaissance = dateNaissance;
    }

    public Date getDateCompte() {
        return dateCompte;
    }

    public void setDateCompte(Date dateCompte) {
        this.dateCompte = dateCompte;
    }

    public Factures getFactures() {
        return factures;
    }

    public void setFactures(Factures factures) {
        this.factures = factures;
    }

    @XmlTransient
    public List<LignesFacture> getLignesFactureList() {
        return lignesFactureList;
    }

    public void setLignesFactureList(List<LignesFacture> lignesFactureList) {
        this.lignesFactureList = lignesFactureList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idClient != null ? idClient.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Clients)) {
            return false;
        }
        Clients other = (Clients) object;
        if ((this.idClient == null && other.idClient != null) || (this.idClient != null && !this.idClient.equals(other.idClient))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.boutique.entity.Clients[ idClient=" + idClient + " ]";
    }

}

Clients我的班级的 SessionBean :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.boutique.ejb;

import com.boutique.entity.Articles;
import com.boutique.entity.Clients;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Barthelemius
 */
@Stateless
@LocalBean
public class UtilisateurSession {
    @Resource(mappedName = "jms/NotificationQueu")
    private Queue notificationQueu;
    @Resource(mappedName = "jms/NotificationQueuFactory")
    private ConnectionFactory notificationQueuFactory;

    @PersistenceContext(unitName = "booxstore-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    public List<Clients> Add(String IDClient ,String nom, String prenom, String email, String motDePasse, String adresse, String CP, String ville, String tel, String mobile, boolean carteFidelite,String dateNaissance, String dateCompte) {
        //Query query = em.createQuery("INSERT INTO Clients " + IDClient + "," + email + "," + nom + "," + motDePasse + "," + prenom + "," + adresse + "," + CP + "," + ville + "," + tel + "," + mobile + "," + carteFidelite + "," + dateNaissance + "," + dateCompte);
        Query query = em.createNamedQuery("INSERT INTO Clients (" +IDClient + "," +email + "," + nom + "," + motDePasse + "," + prenom + "," + adresse + "," + CP + "," + ville + "," + tel + "," + mobile + "," + carteFidelite + "," + dateNaissance + "," + dateCompte+ ")"+"SELECT ID_CLIENT, EMAIL, NOM, PASSWORD, PRENOM, ADRESSE, CP, VILLE, TEL, MOBILE, CARTE_FIDELITE, DATE_NAISSANCE, DATE_COMPTE");
        return query.getResultList();

    }

    public Clients Update(Clients clients)   {
        Clients updated = em.merge(clients);
        try
        {
            sendJMSMessageToNotificationQueu(updated);
        }
        catch (JMSException ex)
        {
            Logger.getLogger(UtilisateurSession.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("utilisateur updated in UtilisateurSession!");
        return updated;
    }

    private Message createJMSMessageForjmsNotificationQueu(Session session, Object messageData) throws JMSException {
        // TODO create and populate message to send
        ObjectMessage tm = session.createObjectMessage();
        tm.setObject((Serializable)messageData);
        return tm;
    }

    private void sendJMSMessageToNotificationQueu(Object messageData) throws JMSException {
        Connection connection = null;
        Session session = null;
        try {
            connection = notificationQueuFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(notificationQueu);
            messageProducer.send(createJMSMessageForjmsNotificationQueu(session, messageData));
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
                }
            }
            if (connection != null) {
                connection.close();
            }
        }
    }

    public List<Clients> Retrieve() {
        Query query = em.createNamedQuery("Clients.findAll");
        return query.getResultList();
    }


}

客户端的 ManagedBean:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.boutique.web;

import com.boutique.ejb.UtilisateurSession;
import com.boutique.entity.Clients;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.Query;

/**
 *
 * @author Barthelemius
 */
@ManagedBean(name = "clients")
@SessionScoped
public class ClientsMBean {
    @EJB
    private UtilisateurSession utilisateurSession;
    private Clients clients;

    public ClientsMBean() {
    }

    public List<Clients> getClients()
    {
        return utilisateurSession.Retrieve();
    }

    public Clients getDetails()
    {

        return clients;
    }

    public String showDetails(Clients clients)
    {
        this.clients = clients;
        return "DETAILS";
    }

    public String update()
    {
        System.out.println("###UPDATE###");
        clients = utilisateurSession.Update(clients);
        return "SAVED";
    }

    public String list()
    {
        System.out.println("###LIST###");
        return "LIST";
    }
}

register.xhtml:(如果我的 managedBean 和 session 不起作用,我不知道我应该给她放什么)

<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"  
      xmlns:f="http://java.sun.com/jsf/core"  
      xmlns:p="http://primefaces.org/ui"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">  
    <h:head>
        <title>BooxStore</title>
    </h:head>
    <h:body>
        <ui:insert name="header" >
            <ui:include src="header.xhtml" />
        </ui:insert>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="span2">
                    <div class="bs-docs-example">
                        <div class="well" style="max-width: 340px; padding: 8px 0; margin-top: 5px;">
                            <ul class="nav nav-list">
                                <li class="nav-header">Catégories</li>
                                <li class="active"><a href="#">Tous</a></li>
                                <li><a href="#">Nothing</a></li>
                                <li><a href="#">test</a></li>
                                <li><a href="#">try</a></li>
                            </ul>
                        </div>
                    </div> </div>       
                <div class="span10" style="margin-top: 5px;">

                    <div id="search">
                        <h:form class="form-search">
                            <h:inputText class="input-medium search-query" value="" />
                            <h:commandButton value="Rechercher" class="btn" action="search" />
                        </h:form> 
                    </div>
                    <h2>Inscription</h2>


                    <f:view>
                        <p:dataGrid value="#{#####}" var="item" columns="1">
                        ####DON'T WORK
                        </p:dataGrid>
                    </f:view>
                </div>
            </div>
        </div>
        <ui:insert name="footer" >
            <ui:include src="footer.xhtml" />
        </ui:insert>
    </h:body>
</html>

对不起,很长的帖子,但我有这个问题,不知道如何解决。

4

1 回答 1

0

那么……你的问题是什么?

有关如何使用 JSF 制作注册表单的教程可在此处此处此处获得

于 2012-12-04T16:32:11.433 回答