MVC (JSR-371)

Florian Hirsch (dwins GmbH)

/ lefloh

The reports of my death
are greatly exaggerated

— MVC

Java EE 8 Update – Linda DeMichiel – JavaOne 2016
Results from the Java EE 8 Community Survey Java EE 8 Survey
David Delabassee, Java EE Evangelist - Oracle, Devoxx Belgium 2016

Single Page Apps FTW!

Progressive Enhancement

SEO?

Performance?

53% of mobile site visits are abandoned if pages take longer than 3 seconds to load.

Need for mobile speed (DoubleClick)

time to first tweet matters!

— twitter, 2012

Isomorphic JavaScript

The future of JSR-371?

Option 1

… to be continued

Option 2

MVC

★ 2014 – ✝ 2016

Option 3

MVC

© ORACLE The community

Hands on!

Thanks to Libor Kramoliš

A controller

@Controller @Path("todo")
public class TodoController {

    @Inject Models models;
    @Inject BindingResult br;
    @Inject TodoService todoService;

    @POST
    public String post(@Valid @BeanParam Todo todo) {
        if (br.isFailed()) {
            models.put("errors", br.getAllMessages());
            return "todo-form.mustache";
        }
        todoService.add(todo);
        return "redirect:todos";
    }

}

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

Fallback

Current Java EE Options?

Servlets & JSPs?

JSF?

Portlets?

JAX-RS?

ROCA

Resource-oriented client architecture

roca-style.org