---
title: Building a Web Application with Java 8 and VRaptor 4 under Wildfly
---
# Building a Web Application with Java 8 and VRaptor 4 under Wildfly
## by Rubens Saraiva
Some months ago Java 8 was lauched, with new features and APIs. And some weeks before, the VRaptor, a brazilian framework, was launched too.
VRaptor 4 was complete refactored to support CDI built-in, and now are compatible to use Java EE 7 resources.
In this post I will explain how building a VRaptor 4 Application, used new features of Java 8 and running on the WildFly 8, the new Application Server built by JBoss that implements Java EE 7. The database used by this article will be HSQLDB, available out of box by Wildfly.
The complete application code is available [here](http://github.com/rsaraiva/v4_wildfly_jta).
To start, create a maven web project using your favorite IDE. Open the Maven configuration file ,`pom.xml` and include the dependencies described in the file below.
~~~
#!xml
4.0.0com.rsaraiva.labsevents1.0wareventsUTF-8br.com.caelumvraptor4.0.0.Finalbr.com.caelum.vraptorvraptor-javatime4.0.0.Finaljstljstl1.2org.hibernatehibernate-entitymanager4.3.5.Finalprovidedjavax.injectjavax.inject1providedjavaxjavaee-web-api7.0providedorg.apache.maven.pluginsmaven-compiler-plugin3.11.81.8
~~~
The first dependency of you project is VRaptor 4, and before we need to include `vraptor-javatime` to allow VRaptor to work with new Java Date API. And the next step will include Java EE dependencies like JSTL, JPA provider and so on. And most important: we need to declare in plugins node that we want to use Java 1.8 compilation.
Now we will create our model - a simple entity to represent Events.
~~~
#!java
@Entity
public class Event implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private LocalDate date;
private String description;
public Event() { }
public Event(String description, LocalDate date) {
this.description = description;
this.date = date;
}
// gets and sets
}
~~~
As you can see, the date attribute was declared as `LocalDate`, the new class introduced in Java 8.
But for that to be possible, it's necessary create a JPA Converter, since the JPA 2.1 still not support the types of the new Date and Time API.
~~~
#!java
@Converter(autoApply = true)
public class LocalDatePersistenceConverter implements AttributeConverter {
@Override
public Date convertToDatabaseColumn(LocalDate entityValue) {
return (entityValue == null) ? null : Date.valueOf(entityValue);
}
@Override
public LocalDate convertToEntityAttribute(Date databaseValue) {
return databaseValue.toLocalDate();
}
}
~~~
This converter needs to be registered in the `persistence.xml` as describes below.
~~~
#!xml
java:jboss/datasources/ExampleDScom.rsaraiva.labs.vraptor4.jpa.LocalDatePersistenceConverter
~~~
Now we will create a `Service` , the logic class to create and list events. Notice the integration with JavaEE with the annotations `@Stateless` and `@PersistenceContext`.
~~~
#!java
@Stateless
public class EventService {
@PersistenceContext
private EntityManager em;
public void persist(Event event) {
em.persist(event);
}
public List findAll() {
return em.createQuery("select e from Event e order by e.description").getResultList();
}
}
~~~
Now we will use the CDI to inject the service in the VRaptor controller
~~~
#!java
@Controller
public class EventController implements Serializable {
@Inject
private EventService service;
@Inject
private Result result;
@Get("/events")
public void event() {
result.include("events", service.findAll());
}
@Post
public void add(Event event) {
service.persist(event);
result.redirectTo(this).event();
}
}
~~~
Our controller have two methods. The first uses `EventService` for querying all events registered and includes the list in the JSP context. The second receive the 'post' from JSP form and persists the event into database.
In the end, we must create a jsp with a events list and the register form.
~~~
#!jsp
<\%@page contentType="text/html" pageEncoding="UTF-8"\%>
<\%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" \%>
Events
Events
${item.id}
${item.description}
${item.formattedDate}
~~~
Cookbook originally posted at [rsaraiva.com](http://rsaraiva.com)