Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ryc.dao;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.EntityTransaction;
- import javax.persistence.NoResultException;
- import javax.persistence.Persistence;
- import javax.persistence.TypedQuery;
- import com.ryc.entities.User;
- public class UserDAO {
- private static final String PERSISTENCE_UNIT_NAME = "TestPersistence";
- private static EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
- private static EntityManager entityMgrObj = entityManagerFactory.createEntityManager();
- private static EntityTransaction entityTransaction = entityMgrObj.getTransaction();
- public static User login(User user){
- User userFound = null;
- try{
- TypedQuery<User> query = entityMgrObj.createQuery("SELECT u FROM User u WHERE u.userName = :userName and u.password = :password", User.class);
- query.setParameter("userName", user.getUserName());
- query.setParameter("password", user.getPassword());
- userFound = (User)query.getSingleResult();
- }catch(NoResultException e){
- }
- return userFound;
- }
- }
- import java.io.IOException;
- import javax.faces.bean.ManagedBean;
- import javax.faces.context.ExternalContext;
- import javax.faces.context.FacesContext;
- import javax.faces.view.ViewScoped;
- import com.ryc.dao.UserDAO;
- import com.ryc.entities.User;
- @ManagedBean(name = "user")
- @ViewScoped
- public class UserController {
- boolean isUsernameValid = false;
- boolean validationComplete = false;
- boolean isPasswordValid = false;
- String userName;
- String password;
- UserDAO userDAO;
- public void login() {
- User userFound = null;
- User userRequest = new User();
- userDAO = new UserDAO();
- ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
- userRequest.setUserName(userName);
- userRequest.setPassword(password);
- userFound = UserDAO.login(userRequest);
- if(userFound != null){
- try {
- ec.redirect(ec.getRequestContextPath() + "/success.xhtml");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } else {
- try {
- ec.redirect(ec.getRequestContextPath() + "/login.xhtml");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- /** * @return the isUsernameValid */
- public boolean getIsUsernameValid() {
- return isUsernameValid;
- }
- /** * @paramisUsernameValid the isUsernameValid to set */
- public void setUsernameValid(boolean isUsernameValid) {
- this.isUsernameValid = isUsernameValid;
- }
- /** * @return the isPasswordValid */
- public boolean getIsPasswordValid() {
- return isPasswordValid;
- }
- /** * @paramisPasswordValid the isPasswordValid to set */
- public void setPasswordValid(boolean isPasswordValid) {
- this.isPasswordValid = isPasswordValid;
- }
- /** * @return the validationComplete */
- public boolean getValidationComplete() {
- return validationComplete;
- }
- /** * @paramvalidationComplete the validationComplete to set */
- public void setValidationComplete(boolean validationComplete) {
- this.validationComplete = validationComplete;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
- package com.ryc.entities;
- import java.io.Serializable;
- import java.util.Date;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.Table;
- @Entity
- @Table(name="user")
- public class User implements Serializable{
- private static final long serialVersionUID = 1L;
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private int id;
- private String userName;
- private String email;
- private String password;
- private String passwordSalt;
- private Date creationDate;
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getPasswordSalt() {
- return passwordSalt;
- }
- public void setPasswordSalt(String passwordSalt) {
- this.passwordSalt = passwordSalt;
- }
- public Date getCreationDate() {
- return creationDate;
- }
- public void setCreationDate(Date creationDate) {
- this.creationDate = creationDate;
- }
- @Override
- public String toString() {
- StringBuilder builder = new StringBuilder();
- builder.append("User [id=");
- builder.append(id);
- builder.append(", userName=");
- builder.append(userName);
- builder.append(", email=");
- builder.append(email);
- builder.append(", password=");
- builder.append(password);
- builder.append(", passwordSalt=");
- builder.append(passwordSalt);
- builder.append(", creationDate=");
- builder.append(creationDate);
- builder.append("]");
- return builder.toString();
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <persistence version="2.1"
- 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">
- <persistence-unit name="TestPersistence"
- transaction-type="RESOURCE_LOCAL">
- <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
- <class>com.ryc.entities.User</class>
- <properties>
- <property name="javax.persistence.jdbc.driver"
- value="com.mysql.jdbc.Driver" />
- <property name="javax.persistence.jdbc.url"
- value="jdbc:mysql://localhost:3306/jsftest" />
- <property name="javax.persistence.jdbc.user" value="root" />
- <property name="javax.persistence.jdbc.password" value="" />
- </properties>
- </persistence-unit>
- </persistence>
Add Comment
Please, Sign In to add comment