English 中文(简体)
Easiest way to deal with sample data in Java web apps?
原标题:

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL

For the moment, whilst creating the pages and general layout, how can I mock up some data easily? For example I don t want to be messing around with a backend database at this stage, is there a solution where I could have some sample data in an XML file?

最佳回答

I use DAO interfaces, so that I can implement both a real DAO and a test DAO. For example, this is the interface:

public interface PersonDAO {
   public List<Person> findAll();
}

Then I ll have 2 implementations of this interface:

public class PersonHibernateDAO implements PersonDAO {
   public List<Person> findAll() {
      // use Hibernate to find and return all the Person objects
   }
}

public class PersonTestDAO implements PersonDAO {
   public List<Person> findAll() {
      List<Person> testData = new ArrayList<Person>();
      testData.add(new Person("Bob");
      testData.add(new Person("Steve");
      return testData;
   }
}

The controller itself uses a PersonDAO, and you can supply either the Hibernate implementation (when in production or testing against the database), or the Test implementation (when unit testing or playing around before the database is set up).

问题回答

You might want to take a look at creating DataTables or using data tables with JTable. Basically you would just mock up the structure of a database table, create a number of rows of data and then use that to bind to your data bound controls.

Creating data table in java

A Simple Interactive JTable Tutorial

Have fun and hope this helps some.

You could use XStream to read XML into your Java objects. You can even use JSON instead of XML, which might be less verbose and quicker for the purposes of your tests.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签