我正在开发一个应用程序(Hibernate Spring Vaadin Postgres Liferay)。 我做了项目的所有配置(Hibernate,Spring,Postgres)和 我生成了一个数据库,但是当我进行动态公式化时,我在将控制器与表单集成时遇到了问题。
这是article.java:
package com.bd.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Articlet")
public class Article {
int id;
String nom;
String type;
int qte;
public Article() {
super();
// TODO Auto-generated constructor stub
}
@Id
@GeneratedValue
@Column(name="ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="Nom")
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
@Column(name="Type")
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Column(name="Qunatite")
public int getQte() {
return qte;
}
public void setQte(int qte) {
this.qte = qte;
}
}
这是articledaoimpl.java:
package com.bd.dao;
import java.util.Collection;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.bd.entity.Article;
@Repository
@Configuration
@Transactional
public class ArticleDaoImp implements ArticleDao {
@Autowired
SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
public List<Article> getAll() {
return sessionFactory.getCurrentSession().createQuery("from Article")
.list();
}
@Transactional(readOnly = true)
public Article getById(int articleId) {
return (Article) sessionFactory.getCurrentSession().get(Article.class,
articleId);
}
@Override
public void save(Article article) {
sessionFactory.getCurrentSession().merge(article);
}
@Override
public void delete(Article article) {
sessionFactory.getCurrentSession().delete(article);
}
}
这是articleserviceimpl.java:
package com.bd.service;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import com.bd.dao.ArticleDao;
import com.bd.entity.Article;
@Service
@Configuration
@ComponentScan("com.bd.dao")
public class ArticleServiceImp implements ArticleService {
@Autowired
private ArticleDao articledao;
@Override
public Article getArticleById(int articleId) {
// TODO Auto-generated method stub
return articledao.getById(articleId);
}
@Override
public void saveArticle(Article article) {
// TODO Auto-generated method stub
articledao.save(article);
}
@Override
public void deleteArticle(Article article) {
// TODO Auto-generated method stub
articledao.delete(article);
}
@Override
public List<Article> getAllArticles() {
// TODO Auto-generated method stub
return articledao.getAll();
}
}
这是文章控制者:
package com.bd.controleur;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import com.bd.entity.Article;
import com.bd.service.ArticleService;
@Controller
@Configuration
@ComponentScan("com.bd.service")
public class ArticleControleur {
@Autowired
ArticleService articleService;
public void addarticle(Article article){
articleService.saveArticle(article);
}
}
形式如下:
package achrefliferay;
import org.springframework.beans.factory.annotation.Autowired;
import com.bd.entity.Article;
import com.bd.service.ArticleService;
import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
public class AchrefliferayApplication extends Application {
@Autowired
ArticleService articleService;
public void init() {
Article article;
Window w = new Window("Subscribe Newsletter");
setMainWindow(w);
w.setContent(new VerticalLayout());
TextField name = new TextField("Name");
TextField type = new TextField("Type");
TextField qte = new TextField("Quantité");
Button subscribeBtn = new Button("saisi");
w.addComponent(name);
w.addComponent(type);
w.addComponent(qte);
w.addComponent(subscribeBtn);
subscribeBtn.addListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
articleService.saveArticle();
}
});
}
}
答案 0 :(得分:0)
您必须创建BeanItem并将其设置为表单的数据源。我不太了解Spring,但是一旦你知道如何将bean绑定到表单,你应该能够通过使用适当的监听器将控制器与表单集成。
示例:
// Create a form and use FormLayout as its layout.
final Form form = new Form();
// Set form caption text
form.setCaption("Subscribe Newsletter");
// Create a bean item that is bound to the bean.
final BeanItem item = new BeanItem(new Article());
// Bind the bean item as the data source for the form.
form.setItemDataSource(item);
// Add the subscribe button
HorizontalLayout formFooter = new HorizontalLayout();
Button subscribeButton = new Button("saisi");
subscribeButton.addListener(new Button.ClickListener() {
void buttonClick(Button.ClickEvent event) {
form.commit();
controller.addArticle(item.getBean());
}
});
formFooter.addComponent(subscribeButton);
form.setFooter(formFooter);
这意味着您不必像在AchrefliferayApplication
的{{1}}方法中那样明确创建每个表单字段。
请参阅Book of Vaadin中的Binding Form to Data。
PS。如果有任何实际的Vaadin专家读到这个:随意纠正我的答案,遵循最佳实践:)