gpt4 book ai didi

java - 在 Vaadin 中如何显示连接表中的数据?

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

我使用 JavaEE 在页面上显示所有产品数据和Vaadin除了类别列显示整个对象而不仅仅是类别名称之外,它正在工作。

我正在连接两个表 productcategory基于category_id列并从 category 获取并显示类别名称 table 。 ProductDaoImpl中提到了查询下面的类(class)。

Products In Vaadin

POM 中的 Vaadin 版本

<vaadin.version>7.6.5</vaadin.version>

ProductsUI.java

@Title("Home")
@Theme("mytheme")
@Widgetset("com.study.crud.MyAppWidgetset")
public class ProductsUI extends UI {

private static final Logger LOG = Logger.getLogger(ProductsUI.class);
private final Grid productsGrid = new Grid();
private DataSource dataSource;

@Override
protected void init(VaadinRequest vaadinRequest) {
getDataSource(vaadinRequest);
configureComponents();
buildLayout();
}

private void getDataSource(VaadinRequest vaadinRequest) {
VaadinServletRequest req = (VaadinServletRequest) vaadinRequest;
this.dataSource = (DataSource) req.getServletContext().getAttribute("dataSource");
}

private void configureComponents() {
// get all products
ProductDao productDao = new ProductDaoImpl(dataSource);
List<Product> products = productDao.getProducts();

//set all the products in grid
productsGrid.setContainerDataSource(new BeanItemContainer<>(Product.class, products));
productsGrid.setColumnOrder("productId", "name", "image", "listPrice", "category", "active");
productsGrid.removeColumn("description");
productsGrid.removeColumn("createdBy");
productsGrid.removeColumn("expiryDate");
productsGrid.removeColumn("modifiedOn");
}

private void buildLayout() {
VerticalLayout layout = new VerticalLayout();

//add products grid to the main layout
layout.addComponent(productsGrid);
productsGrid.setSizeFull();
layout.setSizeFull();
layout.setMargin(true);
layout.setSpacing(true);

setContent(layout);
}

@WebServlet(urlPatterns = "/*", name = "ProductsUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = ProductsUI.class, productionMode = false)
public static class ProductsUIServlet extends VaadinServlet {
}

}

产品.java

public class Product implements Serializable {

private int productId;
private String name;
private String description;
private String image;
private BigDecimal listPrice;
private Category category;
private Date expiryDate;
private String createdBy;
private Date modifiedOn;
private String active;

public Product() {
}

public Product(int productId) {
this.productId = productId;
}

public Product(int productId, String name, String description, String image, BigDecimal listPrice, Category category, Date expiryDate, String createdBy, Date modifiedOn, String active) {
this.productId = productId;
this.name = name;
this.description = description;
this.image = image;
this.listPrice = listPrice;
this.category = category;
this.expiryDate = expiryDate;
this.createdBy = createdBy;
this.modifiedOn = modifiedOn;
this.active = active;
}

public int getProductId() {
return productId;
}

public void setProductId(int productId) {
this.productId = productId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

public BigDecimal getListPrice() {
return listPrice;
}

public void setListPrice(BigDecimal listPrice) {
this.listPrice = listPrice;
}

public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}

public Date getExpiryDate() {
return expiryDate;
}

public void setExpiryDate(Date expiryDate) {
this.expiryDate = expiryDate;
}

public String getCreatedBy() {
return createdBy;
}

public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}

public Date getModifiedOn() {
return modifiedOn;
}

public void setModifiedOn(Date modifiedOn) {
this.modifiedOn = modifiedOn;
}

public String getActive() {
return active;
}

public void setActive(String active) {
this.active = active;
}

@Override
public String toString() {
return "Product{" + "productId=" + productId + ", name=" + name + ", description=" + description + ", image=" + image + ", listPrice=" + listPrice + ", category=" + category + ", expiryDate=" + expiryDate + ", createdBy=" + createdBy + ", modifiedOn=" + modifiedOn + ", active=" + active + '}';
}

}

类别.java

public class Category implements Serializable {

private int categoryId;
private String name;
private String description;
private Date expiryDate;
private String createdBy;
private Date modifiedOn;
private String active;

public Category() {
}

public Category(int categoryId) {
this.categoryId = categoryId;
}

public Category(String name) {
this.name = name;
}

public Category(int categoryId, String name) {
this.categoryId = categoryId;
this.name = name;
}

public Category(int categoryId, String name, String description, Date expiryDate, String createdBy, Date modifiedOn, String active) {
this.categoryId = categoryId;
this.name = name;
this.description = description;
this.expiryDate = expiryDate;
this.createdBy = createdBy;
this.modifiedOn = modifiedOn;
this.active = active;
}

public int getCategoryId() {
return categoryId;
}

public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Date getExpiryDate() {
return expiryDate;
}

public void setExpiryDate(Date expiryDate) {
this.expiryDate = expiryDate;
}

public String getCreatedBy() {
return createdBy;
}

public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}

public Date getModifiedOn() {
return modifiedOn;
}

public void setModifiedOn(Date modifiedOn) {
this.modifiedOn = modifiedOn;
}

public String getActive() {
return active;
}

public void setActive(String active) {
this.active = active;
}

@Override
public String toString() {
return "Category{" + "categoryId=" + categoryId + ", name=" + name + ", description=" + description + ", expiryDate=" + expiryDate + ", createdBy=" + createdBy + ", modifiedOn=" + modifiedOn + ", active=" + active + '}';
}

}

ProductDaoImpl.java

public class ProductDaoImpl implements ProductDao {

private final DataSource dataSource;

public ProductDaoImpl(DataSource dataSource) {
this.dataSource = dataSource;
}


@Override
public List<Product> getProducts() {
String sql = "select a.*, b.name as category_name from product a, category b where a.category_id = b.category_id";
LOG.debug(sql);

Statement stmt = null;
ResultSet rs = null;
Connection cn = null;
List<Product> products = new ArrayList<>();
try {
cn = dataSource.getConnection();
stmt = cn.createStatement();
rs = stmt.executeQuery(sql);
while (rs != null && rs.next()) {
int productId = new Integer(StringUtils.defaultString(rs.getString("PRODUCT_ID")));
String name = StringUtils.defaultString(rs.getString("NAME"));
String description = StringUtils.defaultString(rs.getString("DESCRIPTION"));
String image = StringUtils.defaultString(rs.getString("IMAGE"));
BigDecimal listPrice = new BigDecimal(StringUtils.defaultString(rs.getString("LIST_PRICE")));
int categoryId = new Integer(StringUtils.defaultString(rs.getString("CATEGORY_ID")));
String categoryName = StringUtils.defaultString(rs.getString("CATEGORY_NAME"));
Category category = new Category(categoryId, categoryName);
Date expiryDate = rs.getDate("EXPIRY_DATE");
String createdBy = StringUtils.defaultString(rs.getString("CREATED_BY"));
Date modifiedOn = rs.getDate("MODIFIED_ON");
String active = StringUtils.defaultString(rs.getString("ACTIVE"));
Product product = new Product(productId, name, description, image, listPrice, category, expiryDate, createdBy, modifiedOn, active);
products.add(product);
}
LOG.debug("products = " + products);
LOG.debug("products.size() = " + products.size());
return products;
} catch (SQLException | NumberFormatException ex) {
LOG.error("Exception while getting products....", ex);
return null;
} //close resources
finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (cn != null) {
cn.close();
}
} catch (SQLException ex) {
LOG.error("Exception while closing DB resources rs, stmt or cn.......", ex);
} finally {
try {
if (cn != null) {
cn.close();
}
} catch (SQLException ex) {
LOG.error("Exception while closing cn.......", ex);
}
}
}
}
}

最佳答案

原因是(因为您使用 BeanItemContainer)Vaadin 调用 Product 类中的每个 getter 并尝试将每个返回值转换为 String*。当它到达 Category getter 时,它会对返回值调用 toString()。由于您已重写 Category 中的 toString() 方法,Vaadin 会调用该方法并在 Grid 中显示返回值。如果您在 Category 类中保持 toString() 方法不变,它将显示 Object.toString() 返回的值。

解决此特定问题的简单方法是在您的 Product 类中添加此 getter:

public String getCategoryName() {
return category.getName();
}

并删除“类别”列。

*当我们考虑转换器时,情况并不总是如此,但在这种情况下它已经足够好了

关于java - 在 Vaadin 中如何显示连接表中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36780748/

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