Hello大家现在我使用JSF 2.0和EJB3并使用Primefaces显示错误消息或成功消息,我有两个问题是 一个当我把按钮sumit(JSF命令按钮)下一页错误显示
/register.xhtml @ 26,172 value =“#{userController.user.username}”:目标无法访问,'null'返回null
我的代码是
用户实体
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.demoejb.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.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
*
* @author KencyWindy
*/
@Entity
@Table(name = "iuser")
@NamedQueries({
@NamedQuery(name = "Iuser.findAll", query = "SELECT i FROM Iuser i"),
@NamedQuery(name = "Iuser.findByUid", query = "SELECT i FROM Iuser i WHERE i.uid = :uid"),
@NamedQuery(name = "Iuser.findByUsername", query = "SELECT i FROM Iuser i WHERE i.username = :username"),
@NamedQuery(name = "Iuser.findByPassword", query = "SELECT i FROM Iuser i WHERE i.password = :password"),
@NamedQuery(name = "Iuser.findByPnum", query = "SELECT i FROM Iuser i WHERE i.pnum = :pnum"),
@NamedQuery(name = "Iuser.findByZipcode", query = "SELECT i FROM Iuser i WHERE i.zipcode = :zipcode"),
@NamedQuery(name = "Iuser.findByState", query = "SELECT i FROM Iuser i WHERE i.state = :state"),
@NamedQuery(name = "Iuser.findByDob", query = "SELECT i FROM Iuser i WHERE i.dob = :dob"),
@NamedQuery(name = "Iuser.findByEmail", query = "SELECT i FROM Iuser i WHERE i.email = :email"),
@NamedQuery(name = "Iuser.findByLastlogin", query = "SELECT i FROM Iuser i WHERE i.lastlogin = :lastlogin"),
@NamedQuery(name = "Iuser.findByRegdate", query = "SELECT i FROM Iuser i WHERE i.regdate = :regdate"),
@NamedQuery(name = "Iuser.findByAddress", query = "SELECT i FROM Iuser i WHERE i.address = :address"),
@NamedQuery(name = "Iuser.findByCity", query = "SELECT i FROM Iuser i WHERE i.city = :city")})
public class Iuser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "uid")
private Integer uid;
@Basic(optional = false)
@Column(name = "username")
private String username;
@Basic(optional = false)
@Column(name = "password")
private String password;
@Basic(optional = false)
@Column(name = "pnum")
private int pnum;
@Basic(optional = false)
@Column(name = "zipcode")
private int zipcode;
@Basic(optional = false)
@Column(name = "state")
private String state;
@Basic(optional = false)
@Column(name = "dob")
@Temporal(TemporalType.TIMESTAMP)
private Date dob;
@Basic(optional = false)
@Column(name = "email")
private String email;
@Column(name = "lastlogin")
@Temporal(TemporalType.TIMESTAMP)
private Date lastlogin;
@Column(name = "regdate")
@Temporal(TemporalType.TIMESTAMP)
private Date regdate;
@Basic(optional = false)
@Column(name = "address")
private String address;
@Basic(optional = false)
@Column(name = "city")
private String city;
@JoinColumn(name = "group", referencedColumnName = "g_id")
@ManyToOne(optional = false)
private Igroup igroup;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "iuser")
private List<Irent> irentList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "iuser")
private List<Ipayment> ipaymentList;
public Iuser() {
}
public Iuser(Integer uid) {
this.uid = uid;
}
public Iuser(Integer uid, String username, String password, int pnum, int zipcode, String state, Date dob, String email, String address, String city) {
this.uid = uid;
this.username = username;
this.password = password;
this.pnum = pnum;
this.zipcode = zipcode;
this.state = state;
this.dob = dob;
this.email = email;
this.address = address;
this.city = city;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
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 int getPnum() {
return pnum;
}
public void setPnum(int pnum) {
this.pnum = pnum;
}
public int getZipcode() {
return zipcode;
}
public void setZipcode(int zipcode) {
this.zipcode = zipcode;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getLastlogin() {
return lastlogin;
}
public void setLastlogin(Date lastlogin) {
this.lastlogin = lastlogin;
}
public Date getRegdate() {
return regdate;
}
public void setRegdate(Date regdate) {
this.regdate = regdate;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Igroup getIgroup() {
return igroup;
}
public void setIgroup(Igroup igroup) {
this.igroup = igroup;
}
public List<Irent> getIrentList() {
return irentList;
}
public void setIrentList(List<Irent> irentList) {
this.irentList = irentList;
}
public List<Ipayment> getIpaymentList() {
return ipaymentList;
}
public void setIpaymentList(List<Ipayment> ipaymentList) {
this.ipaymentList = ipaymentList;
}
@Override
public int hashCode() {
int hash = 0;
hash += (uid != null ? uid.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 Iuser)) {
return false;
}
Iuser other = (Iuser) object;
if ((this.uid == null && other.uid != null) || (this.uid != null && !this.uid.equals(other.uid))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.demoejb.entity.Iuser[uid=" + uid + "]";
}
}
会话Bean类
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.demoejb.DAO;
import com.demoejb.entity.Igroup;
import com.demoejb.entity.Iuser;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
/**
*
* @author KencyWindy
*/
@Stateless
@LocalBean
public class UserSessionBean {
@PersistenceContext(unitName = "DemoEJB3-ejbPU")
private EntityManager em;
// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
public void persist(Object object) {
em.persist(object);
}
public Object persistEntity(Object entity) {
em.persist(entity);
return entity;
}
public Object updateEntity(Object entity){
return em.merge(entity);
}
public void removeUser(Iuser user){
user = em.find(Iuser.class, user.getUid());
}
public List<Iuser> getAllUser(){
return em.createNamedQuery("Iuser.findAll").getResultList();
}
/*Check username and email if new user enter into form
* if exist throw exception, this feature will be used in controller
*/
public Iuser checkExistUser(String email,String username){
Iuser entity = null;
try {
javax.persistence.Query query = em.createQuery("select u from iuser u where iuser.email = : email or iuser.username = :username");
query.setParameter("email", email);
query.setParameter("username", username);
entity = (Iuser) query.getResultList();
} catch (Exception e) {
}
return entity;
}
public Iuser addUser(Iuser user){
persist(user);
return user;
}
public void persist1(Object object) {
em.persist(object);
}
/*
* Get all group in database
*/
public List<Igroup> getGroup() {
return em.createNamedQuery("Igroup.findAll").getResultList();
}
}
JSF Managed Bean类
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.demo.controller;
import com.demoejb.DAO.UserSessionBean;
import com.demoejb.entity.Igroup;
import com.demoejb.entity.Iuser;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
/**
*
* @author KencyWindy
*/
public class UserController {
@EJB
private UserSessionBean userSessionBean;
private Iuser user;
/** Creates a new instance of UserController */
public UserController() {
}
public Iuser getUser() {
return user;
}
public void setUser(Iuser user) {
this.user = user;
}
public String addUser(){
String toReturn = "false";
String email = user.getEmail();
String username = user.getUsername();
try {
if(userSessionBean.checkExistUser(email, username) != null){
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
"Error", "username or email id has been exist!"));
toReturn = "False";
}
else {
Igroup g = new Igroup();
Date date = new Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String getDate = sdf.format(date);
Date regDate = sdf.parse(getDate);
user.setRegdate(regDate);
user.setLastlogin(regDate);
g.setGId(1);
user.setIgroup(g);
user = userSessionBean.addUser(user);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
"Sucessful", "New user has been created!"));
}
} catch (Exception e) {
}
return toReturn;
}
/*
* Create list group for user can choose to edit
* warning !!!! this feature it can posible for administrator
*/
public javax.faces.model.SelectItem[] getGrouplist(){
SelectItem[] options = null;
List<Igroup> lGroups = userSessionBean.getGroup();
if(lGroups != null && lGroups.size() > 0){
int i = 0 ;
options = new SelectItem[lGroups.size()];
for (Igroup iGroup : lGroups){
options[i++] = new SelectItem(iGroup.getGId(), iGroup.getGName());
}
}
return options;
}
}
Face- config
<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<managed-bean>
<description>Controller for enntire User module</description>
<managed-bean-name>userController</managed-bean-name>
<managed-bean-class>com.demo.controller.UserController</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
第二个问题是我没有用h:selectOne
将组表放入组合框中<h:outputLabel value="Igroup:" for="igroup" />
<h:selectOneMenu id="igroup" value="#{userController.user.igroup}" title="Igroup" required="true" requiredMessage="The Igroup field is required.">
<!-- TODO: update below reference to list of available items-->
<f:selectItems value="#{userController.grouplist}"/>
</h:selectOneMenu>
当我使用错误时,我不知道为什么会出现错误?我从JSFManageBean编写getGrouplist并在数据库中有1个方法getAllGroup当前表。
答案 0 :(得分:6)
“私有Iuser用户;”
到
“private Iuser user = new Iuser();”
UserController类中的。
由于我自己对EE很陌生,我无法解释原因,但它对我有帮助。希望它也能解决你的问题...
答案 1 :(得分:0)
request
范围意味着在每个请求上重新创建控制器,原始值 - 丢失。除了该注释,您还可以使用JSF 2.0注释。所以你可以把
@ViewScoped
@ManagedBean
public class UserController {..}
有关详细信息,请参阅this。
答案 2 :(得分:0)
您必须为“group”创建一个转换器类,以便在selectOnemenu中使用,例如
selectonemenu jsf on objects with converter`
并使用如下:
<h:selectOneMenu id="igroup" value="#{userController.user.igroup}" title="Igroup" required="true" requiredMessage="The Igroup field is required.">
<!-- TODO: update below reference to list of available items-->
<f:selectItems value="#{userController.grouplist}"/>
<f:converter converterId="groupConverter" />
</h:selectOneMenu>
答案 3 :(得分:0)
在您的ManagedBean中,而不是下面:
private UserSessionBean userSessionBean = new UserSessionBean;
并解决它。