It is a widely held belief that Java unnecessarily penalizes simple web applications. This post demonstrates that by using EasyWeb4J, you can "almost" achieve the productivity of Rails without missing Java's raw performance.
This tutorial is inspired by this article on Nettuts+. I have included the stylesheets and images from Dan's post to retain the look and feel of the final result.
Download the complete source code for this application.
Create a new EasyWeb4J Hibernate Project as described in the tutorial, with settings as shown below.
MySQL Setup
Run this script to create a database for shoutbox
Edit your pom.xml and add a dependency for MySQL JDBC driver as below.
Point Hibernate to the newly created database as below.
src/main/resources/hibernate.xml
Model
The model specifies the validations for each shout and also generates an MD5 hash of the email used in the shout.
src/main/java/org/shoutbox/models/Shout.java
Repository
Unlike Rails EasyWeb4J uses repositories to load models from database. However, for this simple app, the repository can be an empty class.
src/main/java/org/shoutbox/repositories/ShoutRepository.java
Controller
We create a separate method to load all available shouts as it is needed both in the home action and if an error occurs during shout creation. Edit web.xml and specify Shouts as the ROOT_CONTROLLER.
src/main/java/org/shoutbox/controllers/ShoutsController.java
Layout
Just like Rails, EasyWeb4J also supports layouts. To know more on EasyWeb4J layouts, see this post. We specify the top level elements and include the stylesheet in our layout.
src/main/webapp/WEB-INF/layouts/Application.jsp
View
EasyWeb4J applications use JSTL to loop through elements and errors to be displayed to the user.
src/main/webapp/WEB-INF/views/Shouts/index.jsp
Where EasyWeb4J differs from Rails?
This tutorial is inspired by this article on Nettuts+. I have included the stylesheets and images from Dan's post to retain the look and feel of the final result.
Download the complete source code for this application.
Create a new EasyWeb4J Hibernate Project as described in the tutorial, with settings as shown below.
MySQL Setup
Run this script to create a database for shoutbox
Edit your pom.xml and add a dependency for MySQL JDBC driver as below.
Point Hibernate to the newly created database as below.
src/main/resources/hibernate.xml
Model
The model specifies the validations for each shout and also generates an MD5 hash of the email used in the shout.
src/main/java/org/shoutbox/models/Shout.java
Repository
Unlike Rails EasyWeb4J uses repositories to load models from database. However, for this simple app, the repository can be an empty class.
src/main/java/org/shoutbox/repositories/ShoutRepository.java
Controller
We create a separate method to load all available shouts as it is needed both in the home action and if an error occurs during shout creation. Edit web.xml and specify Shouts as the ROOT_CONTROLLER.
src/main/java/org/shoutbox/controllers/ShoutsController.java
Layout
Just like Rails, EasyWeb4J also supports layouts. To know more on EasyWeb4J layouts, see this post. We specify the top level elements and include the stylesheet in our layout.
src/main/webapp/WEB-INF/layouts/Application.jsp
View
EasyWeb4J applications use JSTL to loop through elements and errors to be displayed to the user.
src/main/webapp/WEB-INF/views/Shouts/index.jsp
Where EasyWeb4J differs from Rails?
- EasyWeb4J uses the Respository pattern (through Hibernate/JPA) instead of ActiveRecord for persistence.
- EasyWeb4J does not provide migrations as DDLs need to be optimized anyway in any non-trivial application.
- EasyWeb4J needs a bit of XML for config (but way less than many other Java frameworks).
- EasyWeb4J allows you to use standard HTML for forms fields and for referencing stylesheets by offering few convenient request parameters.
- And yeah, EasyWeb4J app would run way faster than the Rails version. ;)
0 comments:
Post a Comment