我尝试在将电子邮件放入数据库之前验证电子邮件,并检查该电子邮件是否已存在于数据库中。
我有这个控制器类:
package controller;
import model.Account;
import model.accountInterface;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
/**
* A controller. All calls to the model that are executed because of an action
*
*/
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@Stateless
public class Controller {
@PersistenceContext(unitName = "dataOne")
private EntityManager em;
private static int workload = 12;
private String salt;
private boolean done = true;
private static String adminpass = "admin";
private String adminPassHashed;
public String Login(String username, String password) { // tar in två strings
boolean password_verified = false;
accountInterface account = em.find(Account.class, username);
if(account == null) {
//throw new EntityNotFoundException("No such account");
return "Wrong username or password";
}
if(null == account.getPassword() || !account.getPassword().startsWith("$2a$"))
{
return "Wrong username or password";
}
/*
if(!account.getPassword().equals(password)) {
//throw new EntityNotFoundException("Wrong username or password");
return "Wrong username or password";
}*/
password_verified = BCrypt.checkpw(password, account.getPassword());
if(password_verified== true){
return "Done";
}
if(password_verified== false){
return "Wrong username or password";
}
return null;
}
public String newAccount(String username, String password,String name,String surname, String ssn,String email) {
accountInterface account = em.find(Account.class, username);// letar efter primary key i data bassen med entety manger
/* accountInterface emailtest = em.getReference(Account.class, username);
accountInterface account2 = em.find(Account.class, username);
String test = emailtest.getEmail();
*/
if (account != null) {
//throw new EntityNotFoundException("Account already exist");
return "Account already exist";
}
salt=BCrypt.gensalt(workload);
String hased_password = BCrypt.hashpw(password, salt);
long b =2;
account = new Account(username,hased_password,b, name, surname,ssn,email);
em.persist(account); // persist till databasen
return "Done";
}
public List<Account> getAccounts(){
List<Account> account = em.createQuery("from Account m", Account.class).getResultList();
return account;
}
public accountInterface getAccount(String name) {
return em.find(Account.class, name);
}
// Denna metod lägger in admin först i databasen och blir anropad i viewn
public void init() {
salt=BCrypt.gensalt(workload);
adminPassHashed= BCrypt.hashpw(adminpass, salt);
if (done) {
long a =1;// skickar en 1 för att admin ska vara först in i databasen.
em.persist(new Account("admin", adminPassHashed,a,"admin","adminson","9205143218","admin@gmail.com"));
done = false;
}
}
}
我将帐户发送到数据库:
long b =2;
account = new Account(username, password,b, name, surname,ssn,email);
em.persist(account); // persist till databasen
在此之前,我首先使用此方法在数据库中创建管理员:
public void init() {
salt=BCrypt.gensalt(workload);
adminPassHashed= BCrypt.hashpw(adminpass, salt);
if (done) {
long a =1;// skickar en 1 för att admin ska vara först in i databasen.
em.persist(new Account("admin", adminPassHashed,a,"admin","adminson","9205143218","admin@gmail.com"));
done = false;
}
}
}
这是在使用视图
调用方法的xhtml中<h:outputText value="#{msg['email']}" />
<h:inputText id ="email" value="#{acct.email}" required="true"/>
</p>
<p>
<h:commandButton value ="new Account" action ="#{acct.newAccount()}"/>
</p>
此方法位于视图中的accManager类中,当用户想要创建新帐户时,该方法将从xhtml调用。它调用类控制器中的newAccount方法
public String newAccount() {
try {
startConversation();
transactionFailure = null;
succes = false;
admin = false;
logout = false;
account = null;
// ------calls the method newAccount in controller class-----
result = controller.newAccount(username, password,name, surname, ssn,email);
if (result.equals("Done")) {
account = controller.getAccount(username);
}
if (account != null && account.getUsername().equals("admin")) {//man kollar ifall man är admin här
admin = true;
} else if (account != null) {
succes = true;
}
} catch (Exception e) {
handleException(e);
}
return jsf22Bugfix();
}
最后,这是模型中实体类Account的相关部分。
package model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Account implements Serializable, accountInterface {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String username;
@Column(unique=true) //I made so the email is unique in the database
String email;
@OneToOne()
@JoinColumn(name = "ROLE_ID")
private Role role_id;
public Role getRole_id() {
return role_id;
}
public void setRole_id(Role role_id) {
this.role_id = role_id;
}
public Account(){
}
public Account(String username, String password, Long id, String name, String surname, String ssn, String email){
this.username = username;
this.password = password;
this.id = id;
this.name = name;
this.surname = surname;
this.ssn = ssn;
this.email = email;
}
@Override
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
我在实体类帐户
中添加了这一行 @Column(unique=true) String email;
现在,当用户输入相同的电子邮件时,您会收到来自glassfish服务器的警告:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: The
statement was aborted because it would have caused a duplicate key value in a
unique or primary key constraint or unique index identified by
'SQL180311142954500'在'ACCOUNT'上定义。
有没有办法尝试在Web界面中向用户显示该异常,但使用不同的文本,例如电子邮件已经注册?