Geertjan's Blog

  • October 31, 2014

Tools for MVC in Java EE 8 (Part 1)

Geertjan Wielenga
Product Manager

Look at the screenshot in yesterday's blog entry. It shows a very basic Java EE 8 MVC project structure. Now imagine you need to create tools to support those who will be creating those kinds of project structures. For NetBeans IDE, there are several approaches you can take:

  • Create several file templates, for each of those files. Or a file template at the end of which all those files, the Java source file providing the Bean, as well as the HTML files, are created. Doable and very simple, though it is more idiomatic to create these kinds of initial files (i.e., everyone doing MVC projects will need these) in NetBeans IDE as part of the project creation process within project templates.

  • Create a project template from the project and let the user select that project template in the New Project wizard as the basis of their own application. I.e., here you're creating a sample project, which is very easy to do. The downside is that, via the New Project Template wizard, you end up with a very basic iterator that can't let the user define the artifact ID, nor any of the Maven properties, nor can the user specify the server to which the application will be deployed.

  • Create a Maven-based project wizard, based on the same iterator as used by the existing Maven-based web application project wizard. This gives you maximum control, together with all the Maven-oriented features of the Maven iterator. Quite a bit of digging in the NetBeans sources is needed, as well as depending on implementation code, since a lot of the classes you'll be referencing are not public APIs. 

I took the third approach above and it works. You can add your own files into the iterator and, as you can see above, the only downside is that you're depending on implementation code here and there.

In the above, notice "MVCSettingsWizardPanel", this is an additional panel added on to the end of the wizard, i.e., in addition to the standard Maven web application panels, for the settings that are relevant to MVC projects, e.g., to let the user provide the names of the MVC artifacts that will be created. 

Near the end of the above, you see this statement, which works, i.e., I can add my own files into the process. Ultimately, once I know which exact artifacts are needed, I can change the below with the names of the files that need to be created, e.g., from Freemarker templates those can be created, same as for all other files created via templates in NetBeans IDE:


The module described in this blog entry looks like this, i.e., I now have a framework for creating my own custom new Maven-based web applications:

Best would be if the project template were to be an official Maven Archetype that could be accessed via the "Project from Archetype" wizard. But a downside to that approach would be that it is a generic wizard, i.e., the MVC-specific artifacts can't be defined in that wizard and would simply be generated into the right places in the structure. What I'm trying to achieve, instead of that, is a way to let the user customize the basic artifacts of the project prior to their creation and the above approach makes that possible.

Join the discussion

Comments ( 1 )
  • guest Monday, November 17, 2014

    Sound good, thanks for sharing with us.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.