モデル:JavaのListメソッドで宣言されているArrayListを参照するには?
@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;
}
}
ダオは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;
}
}
マネージド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ページ:
<?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>
はそれがを参照することが可能ですpublic List getAllUsers(){}メソッドで新しいオブジェクトを追加できるように宣言した別のメソッドの?例えば :この方法で
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).
}
私は、XHTMLページにDBからデータを盗ん:
public List<User> getAllUsers(){
List<User> users= new ArrayList<>();
UserDao daoObj= new UserDao();
users= daoObj.listUsers();
return users;
}
リストは一般的ですが、リストは一般的ではありません。 –
Omore
と任意のソリューションを持っている? –
あなたはうまくいっていますが、汎用リストと非汎用リストについて学ぶ必要があります。 – Omore