在默认构造函数中的AllUsersBean类中出现错误。它不能识别这一行中的subBean users.add(subBean.clone());我用subBi。clone将用户提交的信息克隆到UsersList(称为users)。谢谢你的建议。
这是错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-
compile) on project Test: Compilation failure
[ERROR] ......(here was path to this class) AllUsersBean.java:[25,18] cannot find symbol
[ERROR] symbol: variable subBean
为了解决这个问题,我试图在UserBean类中添加appBean的getter和setter,但它不起作用。
package testpkg;
/*Importing required java libraries */
import javax.inject.Named;
import javax.enterprise.context.ApplicationScoped;
import java.util.ArrayList;
import java.util.List;
@Named("appBean")
@ApplicationScoped
public class AllUsersBean {
private List<UserBean> users;
public AllUsersBean(){
users = new ArrayList<UserBean>();
users.add(subBean.clone());
}
public List<UserBean> getUsers() {
return users;
}
public void setUsers(List<UserBean> users) {
this.users = users;
}
}
package testpkg;
/*Importing required java libraries */
import java.io.Serializable;
import javax.inject.Named;
import javax.inject.Inject;
import javax.enterprise.context.SessionScoped;
@Named("subBean")
@SessionScoped
public class UserBean implements Serializable{
private String fname;
@Inject
private AllUsersBean appBean;
public UserBean() {
this.fname = null;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public AllUsersBean getAppbean() {
return appBean;
}
public void setAppbean(AllUsersBean appBean) {
this.appBean =appBean;
}
}
您是否尝试过创建一个名为subBean的字段到AllUsersBean并使您在方法中初始化注释@PostConstruct?一般来说,使用克隆并不是一个好的做法