Because this is a question about another forum user’s sample code, it really should go on the original thread so that the original user can clarify his code.
In any case, the MyPojo class, the provider service, and the method are all completely made up to demonstrate that you can use Java generics with an arbitrary provider of an arbitrary generic type. Before Vaadin 8, you had to take your collection of items, configure and populate a container (e.g. BeanItemContainer), and then attach it to the component (e.g. Grid), sacrificing type safety along the way.
I did not continue with the original thread, as the question is something very basic, that would take someone that knows a few seconds to answer, but someone that does not know – a lot of trial and error and searching.
Thanks for the code. First thought is that this class is not a bean, nor really a DAO. This class needs a method that returns a collection of objects that have the data that you need. So, I recommend that you create an actual bean type that represents a single row in your product table, and will represent a single grid row. Note that I can’t tell from your code what the actual column names are since you are using
select * .
public class Product {
private final String field1;
private final String field2;
private final String field3;
public Product(String field1, String field2, String field3) {
this.field1 = field1;
this.field2 = field2;
this.field3 = field3;
}
public String getField1() { return field1; }
public String getField2() { return field2; }
public String getField3() { return field3; }
}
Next step, add a method to your DAO class that performs the database query and populates a Collection class, e.g. ArrayList().
public class SqlDAO {
public List<Product> getAllProducts() {
// ... your DB code here
// Populate a list of products
List<Product> products = new ArrayList<>();
while(result.next() {
products.add(new Product(result.getString(1), result.getString(2), result.getString(3)));
}
return products;
}
}
Finally, call this method and pass the results to the
setItems() method on your
Grid .
In a Vaadin 8 Archetype that I just created, I have placed my JSF java source files in the Project but do not know how to place the connection data. In the JSF, the connection data is in the context.xml (Resource) and web.xml (username, etc…)
The connection data is in the Project but is not being found.
I’m not sure what isn’t working, but I can give you a few pointers to get you started.
If you have m2e and m2e-wtp installed in Eclipse, then you can add the Dynamic Web Module facet to your Vaadin archetype project (right click your project, select Properties, choose Project Facets from the left pane). If your project is not already in faceted form, converting it and adding Dynamic Web Module will give you a WebContent folder with a WEB-INF and a META-INF, so you now have a destination for your web.xml and context.xml.
Your deployment assembly (also under project properties) must include the following mappings:
I’ve attached an image that shows you the mappings I have for one of my projects that deploys to a server (instead of using the jetty-maven plugin). Note the options in the “New Assembly Directive” dialog box. If you don’t see these options (e.g. Java Build Path Entries), then make sure m2e and m2e-wtp are installed correctly.
Finally, I recommend making your mysql jdbc connector 5.1.40 a maven dependency instead of a referenced library. You can do that by adding the following lines to your pom.xml in the section:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
I matched the version you are using in the video; there are
more recent releases than 5.1.40.
choose Project Facets from the left pane). If your project is not already in faceted form, converting it and adding Dynamic Web Module will give you a WebContent folder with a WEB-INF and a META-INF, so you now have a destination for your web.xml and context.xml.
It is already selected but there is NO Web Content folder created. I had copied the Web Content folder from the JSF Project and Pasted it into the Vaadin 8 Archetype Project
Does that image show the facets for your Vaadin archetype project? I don’t think JSF should be selected, just Dynamic Web Module and Java. Regardless, what matter most are the deployment assembly mappings.