我有一个UserControl
的bean SessionScope
,在系统中的用户register
之后,他/她会尝试登录系统,对吗?
使用相同的bean,但在第二次尝试登录一次(例如使用错误的passwod)之后,出现了我的bean为null
的异常,为什么?
我在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>
第一次工作但第二次,不要。 给我这个例外:
/resources/jsf/composition/body/user/form/email.xhtml @9,108 value="#{userC.user.email}": Target Unreachable, 'null' returned null
这是我的bean,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;
}
}
答案 0 :(得分:0)
根据maple_shaft
和kolossun
的建议不足以解决我的问题,但在this建议之后,检查user
是否为null
在public User getUser()
方法中是。
@PostConstruct
public void init(){
if (user == null){
user = new User();
logged = false;
}
if (user != null && user.getAddressCity() == null)
user.setAddressCity(new AddressCity());
if (user != null && user.getAddressState() == null)
user.setAddressState(new AddressState());
if (user != null && user.getAddressCountry() == null)
user.setAddressCountry(new AddressCountry());
}
并且:
public User getUser() {
init();
return user;
}
这样我确保始终初始化对象user
和其他类。
我仍然不明白为什么在null
进行@PostConstruct
检查是不够的,它一直给我同样的错误。