Model:
@Entity(name= "User")
@Table(name= "Registration")
public class User {
private int id;
private String orderNo;
private String productName;
private BigDecimal price;
private Integer qty;
private Boolean editable;
public User(){
}
public User(int id, String orderNo, String productName, BigDecimal price, Integer qty){
this.id= id;
this.orderNo= orderNo;
this.productName= productName;
this.price= price;
this.qty= qty;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getQty() {
return qty;
}
public void setQty(Integer qty) {
this.qty = qty;
}
public Boolean getEditable() {
return editable;
}
public void setEditable(Boolean editable) {
this.editable = editable;
}
}
Dao metod:
public class UserDao implements UserInterface{
private static final Logger logger= LoggerFactory.getLogger(UserDao.class);
Session session= HibernateUtil.getSessionFactory().openSession();
public void save(User newUser){
// Session session= HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(newUser);
session.getTransaction().commit();
session.close();
}
@SuppressWarnings("unchecked")
public Integer getId() {
String hql = "SELECT MAX (user.id) FROM User user";
Query<Integer> query = session.createQuery(hql);
List<Integer> results = query.list();
Integer userId = 1;
if(results.get(0)!= null) {
userId= results.get(0)+ 1;
}
return userId;
}
@SuppressWarnings("unchecked")
@Override
public List<User> listUsers() {
// TODO Auto-generated method stub
Session session= HibernateUtil.getSessionFactory().openSession();
List<User> userList= session.createQuery("FROM User").list();
for(User u: userList){
logger.info("Users list:: "+ u);
}
return userList;
}
}
Managed Bean:
@ManagedBean(name= "myBean", eager= true)
@RequestScoped
public class UserDrivingBean {
private String orderNo;
private String productName;
private BigDecimal price;
private Integer qty;
List<User> users;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public String saveUser(){
UserDao userDao= new UserDao();
Integer userId= userDao.getId();
User user= new User(userId, orderNo, productName, price, qty);
userDao.save(user);
System.out.println("User successfully saved!");
return "output";
}
public void addUser(){
UserDao userDao= new UserDao();
Integer userId= userDao.getId();
User newUser= new User(userId, orderNo, productName, price, qty);
newUser.setEditable(true);
userDao.addCustomer(newUser);
}
public void plusNewUser(){
UserDao daoObj= new UserDao();
List<User> users= getAllUsers();
Integer userId= daoObj.getId();
User addUser= new User();
addUser.setEditable(true);
users.add(addUser);
}
public List<User> getAllUsers(){
users= new ArrayList<>();
UserDao daoObj= new UserDao();
users= daoObj.listUsers();
/* Integer userId= daoObj.getId();
User addUser= new User(userId, orderNo, productName, price, qty);
addUser.setEditable(true);
users.add(addUser); */ //!!!This works, when added directly to method!
return users;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getQty() {
return qty;
}
public void setQty(Integer qty) {
this.qty = qty;
}
xhtml page:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Retreiving func.</title>
</h:head>
<h:body>
<f:view>
<h:form id="adminInf">
<center>
<h:dataTable border="1" id="table" var="y" value="#{myBean.allUsers}">
<h:column>
<f:facet name="header">
<h:outputLabel value="Id: "/>
</f:facet>
<h:outputText value="#{y.id}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="Order No: "/>
</f:facet>
<h:inputText value="#{y.orderNo}" rendered="#{y.editable}"></h:inputText>
<h:outputText value="#{y.orderNo}" rendered="#{!y.editable}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="Product: "/>
</f:facet>
<h:inputText value="#{y.productName}" rendered="#{y.editable}"></h:inputText>
<h:outputText value="#{y.productName}" rendered="#{!y.editable}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="Price: "/>
</f:facet>
<h:inputText value="#{y.price}" rendered="#{y.editable}"></h:inputText>
<h:outputText value="#{y.price}" rendered="#{!y.editable}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="Quantity: "/>
</f:facet>
<h:inputText value="#{y.qty}" rendered="#{y.editable}"></h:inputText>
<h:outputLabel value="#{y.qty}" rendered="#{!y.editable}"/>
</h:column>
<h:column id="insertBtn">
<h:commandButton value="Insert" action="#{myBean.addUser}" rendered="#{y.editable}">
<f:ajax execute="@form" render="@form"/>
</h:commandButton>
</h:column>
</h:dataTable>
<h:commandButton value="AddNew" action="#{myBean.plusNewUser}" update="table"/>
<h:commandLink value="Home" action="input"/>
</center>
</h:form>
</f:view>
</h:body>
</html>
Is it possible to refer to List<User> users= new ArrayList<>();
from another method, which declared in public List getAllUsers(){} method to be able to add new objects in it? for example:
public void addNewUser(){
User addUser= new User(userId, orderNo, productName, price, qty);//Create new object
addUser.setEditable(true);// Open new iput fields(<h:inputText>)on xhtml page
users.add(addUser);//refer to ArrayList from getAllUsers() method and add new object in it(new User).
}
With this method I retreive data from DB into xhtml page:
public List<User> getAllUsers(){
List<User> users= new ArrayList<>();
UserDao daoObj= new UserDao();
users= daoObj.listUsers();
return users;
}
yes it is possible with java generics concept.
List<User> users= new ArrayList<User>();
public class SomeClass {
List<User> users= new ArrayList<User>();
public List<User> getAllUsers(){
// here just use the global variable, no need to add List<User> again
users = new ArrayList<>();
UserDao daoObj = new UserDao();
users = daoObj.listUsers();
return users;
}
public void addNewUser() {
users = getAllUsers();
User addUser = new User(userId, orderNo, productName, price, qty);//Create new object
addUser.setEditable(true);// Open new input fields(<h:inputText>)on xhtml page
users.add(addUser);//refer to ArrayList from getAllUsers() method and add new object in it(new User).
// save user to DOA, depend on how you create your DAO, it maybe something like
UserDao daoObj = new UserDao();
daoObj.insertUser(addUser);
}
}
I believe what you want to do is get List<User> users = new ArrayList<>();
which is exist in public List<User> getAllUsers()
method from another method like in public void addNewUser()
.
The answer to question above is No you can't , unless you change something in your code.
EDIT
you can create it like this if you want to keep the local declaration.
public class SomeClass {
public List<User> getAllUsers(){
// here just use the global variable, no need to add List<User> again
List<User> users = new ArrayList<>();
UserDao daoObj = new UserDao();
users = daoObj.listUsers();
return users;
}
public void addNewUser() {
List<User> users = getAllUsers();
User addUser = new User(userId, orderNo, productName, price, qty);//Create new object
addUser.setEditable(true);// Open new input fields(<h:inputText>)on xhtml page
users.add(addUser);//refer to ArrayList from getAllUsers() method and add new object in it(new User).
// save user to DOA, depend on how you create your DAO, it maybe something like
UserDao daoObj = new UserDao();
daoObj.insertUser(addUser);
}
}
And have you save the users to DAO after add new user?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.