我使用JavaEE
和Vaadin
显示页面上的所有产品数据,除了显示整个对象而不仅仅是类别名称的“类别”列以外。
我正在根据product
列加入两个表category
和category_id
,并从category
表中提取并显示类别名称。查询在下面的ProductDaoImpl
课程中提到。
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 {
}
}
Product.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 + '}';
}
}
Category.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);
}
}
}
}
}
答案 0 :(得分:1)
原因是(因为你使用BeanItemContainer
)Vaadin会调用Product
类中的每个getter并尝试将每个返回值强制转换为String
*。当它到达Category
getter时,它会在返回的值上调用toString()
。由于您已在toString()
中覆盖了Category
方法,因此Vaadin会调用它并在Grid
中显示返回的值。如果您在toString()
课程中保持Category
方法不变,则会显示Object.toString()
返回的值。
解决此特定问题的简单方法是在Product
类中添加此getter:
public String getCategoryName() {
return category.getName();
}
并删除&#39;类别&#39;列。
* 当我们考虑Converters
的{{1}}时,情况并非总是如此,但在这种情况下它已经足够了