システムのユーザーUserControl
のSessionScope
後register
、彼/彼女はシステムにログインしようとしますよね?
同じ Bean が使用されますが、2 回目の試行で (たとえば間違ったパスワードで) 1 回ログインを試行した後、私の Beannull
がなぜ ? であるという例外が表示されます。
私はとcomposition
で同じものを使用します:register form
login form
<!DOCTYPE html>
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
E-Mail:<h:message id="m_email" for="email" styleClass="red" /><br/>
<h:inputText id="email" value="#{userC.user.email}" maxlength="45" validatorMessage="e-mail inválido" >
<f:validateLength minimum="6" maximum="45" />
<f:ajax event="blur" render="m_email" />
</h:inputText>
<br/>
</ui:composition>
最初は動作しますが、2 回目は動作しません。この例外を考えると:
/resources/jsf/composition/body/user/form/email.xhtml @9,108 value="#{userC.user.email}": Target Unreachable, 'null' returned null
これは私の豆ですUserControl
:
@ManagedBean(name="userC")
@SessionScoped
public class UserControl implements Serializable{
private static final long serialVersionUID = 7708365499838642904L;
@EJB UserEAO userEAO;
@EJB AddressEAO addressEAO;
private User user;
private Message message;
/*
* Variable
*/
private boolean logged;
public UserControl(){
user = new User();
user.setAddressCity(new AddressCity());
user.setAddressState(new AddressState());
user.setAddressCountry(new AddressCountry());
logged = false;
}
.. methods
}
で私objects
の開始が間違っていconstructor
ますか?
の完全なコードを更新UserControl
:
package com.hi.mvc.controller;
import java.io.IOException;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import com.hi.mvc.eao.AddressEAO;
import com.hi.mvc.eao.UserEAO;
import com.hi.mvc.model.AddressCity;
import com.hi.mvc.model.AddressCountry;
import com.hi.mvc.model.AddressState;
import com.hi.mvc.model.User;
import com.hi.utility.Crypto;
import com.hi.utility.Email;
import com.hi.utility.Message;
@ManagedBean(name="userC")
@SessionScoped
public class UserControl implements Serializable{
private static final long serialVersionUID = 7708365499838642904L;
@EJB UserEAO userEAO;
@EJB AddressEAO addressEAO;
private User user;
private Message message;
/*
* Variable
*/
private boolean logged;
public UserControl(){
user = new User();
user.setAddressCity(new AddressCity());
user.setAddressState(new AddressState());
user.setAddressCountry(new AddressCountry());
logged = false;
}
/*
* CRUD
*/
public String register(){
if (userEAO.find(user.getEmail()) == null){
defineUser();
message = userEAO.create(user);
//sending user email confirmation
if (message.getCode() == 0) {
message = new Message(1);
message.setEmail(user.getEmail());
sendConfirmationEmail(user);
return "/pages/system/message.xhtml?faces-redirect=true";
}else
//user already registered and trying to register again
message = new Message(-1002);
}
addMessage();
return null;
}
public String update() {
message = userEAO.update(user);
this.addMessage();
return null;
}
public String delete(){
message = userEAO.delete(user);
if (message.getCode() >= 0)
try {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.invalidateSession();
message = new Message(3001);
addMessage();
ec.redirect(ec.getRequestContextPath() + "/pages/system/message.xhtml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
addMessage();
return null;
}
/*
* Log in/off
*/
public String login(){
String password = user.getPassword();
user = userEAO.find(user.getEmail());
// if user was found
if ( user != null ){
if ( user.getEmailConfirmed()){
// with facebook registration, password is empty, so we avoid login empty and check the password
if ( !user.getPassword().isEmpty() && Crypto.check(password, user.getPassword())){
//user = addressEAO.findByUser(user.getId());
logged = true;
return "/index.xhtml?faces-redirect=true";
}else
message = new Message(-2000);
}else
message = new Message(-1003);
}else
message = new Message(-2000);
addMessage();
return null;
}
public void logout(){
try {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.invalidateSession();
ec.redirect(ec.getRequestContextPath() + "/index.xhtml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* Functions
*/
public void defineUser(){
encryptPassword(user);
user.setMemberSince(new Date());
user.setAcceptanceRate(0);
user.setResponseRate(0);
// user do not confirm email yet
user.setEmailConfirmed(false);
// set address to be empty
user.getAddressCity().setCity(" ");
user.getAddressState().setState(" ");
user.getAddressCountry().setCode(" ");
defineAddress();
}
public void defineAddress(){
AddressCity addressCity = addressEAO.findCity(user.getAddressCity().getCity());
if (addressCity == null){
addressCity = new AddressCity();
addressCity.setCity(user.getAddressCity().getCity());
addressEAO.createCity(addressCity);
}
user.setAddressCity(addressCity);
AddressState addressState = addressEAO.findState(user.getAddressState().getState());
if (addressState == null){
addressState = new AddressState();
addressState.setState(user.getAddressState().getState());
addressEAO.createState(addressState);
}
user.setAddressState(addressState);
AddressCountry addressCountry = addressEAO.findCountry(user.getAddressCountry().getCode());
if (addressCountry == null){
addressCountry = new AddressCountry();
addressCountry.setCode(user.getAddressCountry().getCountry());
addressEAO.createCountry(addressCountry);
}
user.setAddressCountry(addressCountry);
}
/*
* Date
*/
public Date convertDate(String date){
try {
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
return (Date)formatter.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/*
* E-Mail
*/
public void sendConfirmationEmail(User u){
Email email = new Email();
email.setUser(u);
email.setSubject("Hotel Informal - Por favor, confirme seu endereço de email");
email.simpleEmail();
}
public void sendWelcomeEmail(User u){
Email email = new Email();
email.setUser(u);
email.setSubject("Hotel Informal - Olá, seja bem-vindo(a)");
email.simpleEmail();
}
public String resendEmailConfirmation(){
sendConfirmationEmail(user);
message = new Message(2);
message.setEmail(this.user.getEmail());
return "/pages/system/message.xhtml?faces-redirect=true";
}
/*
* Password
*/
public void encryptPassword(User u) {
if (u.getPassword() != null){
u.setPassword(Crypto.encrypt(u.getPassword()));
}
}
public String changePassword(){
encryptPassword(user);
message = userEAO.update(user);
if (message.getCode() >= 0)
message = new Message(2002);
addMessage();
return null;
}
public Message checkPassword(User user) {
if (user == null)
return new Message(-3000);
if (Crypto.check(user.getPassword(), user.getPassword())) {
return new Message(1);
} else
return new Message(-2000);
}
public String forgotPassword(){
user = userEAO.find(user.getEmail());
if (user != null){
Email email = new Email();
email.setUser(user);
email.setSubject("Hotel Informal - Redefinir senha");
email.simpleEmail();
message = new Message(6);
message.setEmail(user.getEmail());
}else
message = new Message(-1004);
addMessage();
return "/pages/system/message.xhtml?faces-redirect=true";
}
public String resetPassword(){
encryptPassword(user);
userEAO.update(user);
Email email = new Email();
email.setUser(user);
email.setSubject("Hotel Informal - Senha redefinida");
email.simpleEmail();
message.setEmail(user.getEmail());
message = new Message(2001);
addMessage();
return "/pages/system/message.xhtml?faces-redirect=true";
}
private void addMessage(){
FacesMessage fMessage = new FacesMessage(message.getMessage());
FacesContext.getCurrentInstance().addMessage(null, fMessage);
}
// get's & set's
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public boolean isLogged() {
return logged;
}
public void setLogged(boolean logged) {
this.logged = logged;
}
}