gpt4 book ai didi

java - 如何将 Controller 与表单集成(Hibernate Spring Vaadin)

转载 作者:行者123 更新时间:2023-12-01 05:36:35 24 4
gpt4 key购买 nike

我正在开发一个应用程序(Hibernate Spring Vaadin Postgres Liferay)。我完成了该项目的所有配置(Hibernate、Spring、Postgres)并且我生成了一个数据库,但是当我执行动态公式时,我在将 Controller 与表单集成时遇到问题。

这是文章.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;
}




}

这是文章daoimpl.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();
}
});
}

}

最佳答案

您必须创建一个 BeanItem并将其设置为表单的数据源。我不太了解 Spring,但是一旦您知道如何将 bean 绑定(bind)到表单,您应该能够通过使用适当的监听器将 Controller 与表单集成。

示例:

// 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);

这意味着您不必像在 AchrefliferayApplicationinit() 方法中那样显式创建每个表单字段。

参见Binding Form to Data在《瓦丁之书》中。

ps。如果任何真正的 Vaadin 专家读到此内容:请随时纠正我的答案以遵循最佳实践:)

关于java - 如何将 Controller 与表单集成(Hibernate Spring Vaadin),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8096147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com