MVC (JSR-371)

Florian Hirsch (dwins GmbH)

github.com/leflohtwitter.com/lefloh

Why?

Results from the Java EE 8 Community Survey Java EE 8 Survey

Current Java EE Options?

Servlets & JSPs?

JSF?

Portlets?

JAX-RS?

ROCA?

Resource-oriented client architecture

roca-style.org

Goals

  • Leverage existing Java EE technologies
  • Integrate with CDI and Bean Validation
  • Define a solid core to build MVC applications without necessarily supporting all the features in its first version
  • Explore layering on top of JAX-RS for the purpose of re-using its matching and binding layers
  • Provide built-in support for JSPs and Facelets view languages
MVC Specification (Early Draft Review 2)

Non Goals

  • Define a new view (template) language and processor
  • Support standalone implementations of MVC running outside of Java EE
  • Support REST services not based on JAX-RS
  • Provide built-in support for view languages that are not part of Java EE
MVC Specification (Early Draft Review 2)

Ozark View Engines

  • AsciiDoc
  • Freemarker
  • Handlebars
  • Jade
  • JSR-223
  • Mustache
  • StringTemplate
  • Thymeleaf
  • Velocity
  • (React)

Status?

Contributions (Spec + Ozark)

Hands on!

Thanks to Libor Kramoliš

Redirects

@RedirectScoped
public class Messages implements Serializable {

    ...

}

public String get() {
    return "redirect:/todos/" + id;
}

Events

@ApplicationScoped
public class EventObserver {

    private static final Logger LOG =
        LoggerFactory.getLogger(EventObserver.class);

    public void beforeCtrl(@Observes BeforeControllerEvent e) {
        LOG.info("Resource {} will be handled by {}#{}",
            e.getUriInfo().getRequestUri(),
            e.getResourceInfo().getResourceClass().getName(),
            e.getResourceInfo().getResourceMethod().getName());
    }

}

CSRF Protection

@POST
@CsrfValid
public String post(@BeanParam Todo todo) {
   ...
}

<input type="hidden"
       name="{{mvc.csrf.name}}" value="{{mvc.csrf.token}}" />

i18n / l10n

public interface LocaleResolver {

    Locale resolveLocale(LocaleResolverContext context);

}

i18n / l10n

@FormParam("dob")
@ConvertWith(DateConverter.class)
private LocaleDate dob;

MvcUriBuilder

@Controller
public class TodoController {

    @GET
    @Path("todo/{id}")
    @UriRef("todo")
    public String getOne(@PathParam("id") long id) {
        ...
    }

}

{{ mvc.uri('todo', [4711] }}
{{ mvc.uri('todo', {id: 4711} }}
{{ mvc.uri('TodoController#getOne', {id: 4711} }}
      

Thanks