型号:
@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;
}
}
道先生:
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页面:
<?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>
是否可以从另一个方法中引用List<User> users= new ArrayList<>();
,在公共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).
}
使用这种方法,我将数据从DB转发到xhtml页面:
public List<User> getAllUsers(){
List<User> users= new ArrayList<>();
UserDao daoObj= new UserDao();
users= daoObj.listUsers();
return users;
}
答案 0 :(得分:0)
是的,这可以用java泛型概念。
public class GetInfo extends AsyncTask<ReisData, Integer, String> {
Reisplanner reisPlanner;
View view;
public GetInfo(Reisplanner main) {
this.reisPlanner = main;
view = reisPlanner.getView();
}
@Override
protected void onPreExecute() {
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@Override
protected String doInBackground(ReisData... params) {
try {
// Return XML as string with route options
return HttpRequestHelper.downloadFromServer(params);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
DocumentBuilder builder;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document doc = null;
try {
builder = factory.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(result)));
} catch (Exception e) {
e.printStackTrace();
}
// TODO: Get 'ReisData' object
if (doc != null) {
XMLParser.parse(doc);
}
// Start Reisadvies with ReisData class
Fragment fragment = new Reisadvies();
// TODO: Put ReisData class into Bundle
this.reisPlanner.startFragment(fragment);
}
答案 1 :(得分:0)
我相信您要做的就是从List<User> users = new ArrayList<>();
中获取另一种方法public List<User> getAllUsers()
方法中存在的public void addNewUser()
。
上述问题的答案是不,你不能,,除非您在代码中更改了某些内容。
修改强>
如果你想保留本地声明,你可以像这样创建它。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);
}
}
您是否在添加新用户后将用户保存到DAO?