简体   繁体   English

如何将控制器与表单集成(Hibernate Spring Vaadin)

[英]How to integrate the controller with form (Hibernate Spring Vaadin)

I'm busing developing an application (Hibernate Spring Vaadin Postgres Liferay). 我正在开发应用程序(Hibernate Spring Vaadin Postgres Liferay)。 I did all of the configurations for the project (Hibernate, Spring, Postgres) and I generated a database, but when I do a dynamic formulaire I have a problem integrating the controller with the form. 我完成了该项目的所有配置(Hibernate,Spring,Postgres),并生成了一个数据库,但是当我执行动态公式时,将控制器与表单集成时会遇到问题。

This is the article.java: 这是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;
    }




}

This is articledaoimpl.java: 这是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);

    }

}

This is articleserviceimpl.java: 这是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();
    }

}

This is the article controleur: 这是文章控制者:

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

This the form: 该表格:

    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();   
            }
        });
    }

}

You'll have to create a BeanItem and set it as the data source for your form. 您必须创建一个BeanItem并将其设置为表单的数据源。 I don't know Spring that well, but once you know how to bind a bean to the form, you should be able to integrate the controller with the form by using the appropriate listeners. 我不太了解Spring,但是一旦您知道如何将bean绑定到表单,就应该能够通过使用适当的侦听器将控制器与表单集成在一起。

Example: 例:

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

This means you don't have to explicitly create every form field like you did in your AchrefliferayApplication 's init() method. 这意味着您不必像在AchrefliferayApplicationinit()方法中那样显式创建每个表单字段。

See Binding Form to Data in Book of Vaadin. 请参阅《 Vaadin手册》中的将表格绑定到数据

ps. ps。 If anyone actual Vaadin expert reads this: feel free to correct my answer to follow the best practices :) 如果有人真正的Vaadin专家阅读了这篇文章:请随时更正我的答案以遵循最佳实践

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM