Friday, 15 June 2012

How to use MVC in Java EE 6/7 without using other frameworks? -



How to use MVC in Java EE 6/7 without using other frameworks? -

i have implemented mvc pattern using spring framework. in spring there @controller , @requestmapping annotations in controller class , framework doesn't utilize httpservlet.

i want exact construction in java ee (but without using other framework).

so question is: how can utilize mvc in java ee without using other frameworks? note: don't want utilize httpservlet class

(1)

you can utilize jax-rs , cdi respond http requests, using annotations such @path , @requestscoped. might need jax-rs extensions or utilities convenience, improve handling of parameters (e.g. resteasy's @form) , forwards views (e.g. jersey's viewable).

(2)

you can write single httpservlet (i hope that's not problem) deed front end controller, instantiating controller each request. using cdi, can annotate controllers @model (which same @requestscoped , @named), , instantiate them in front end controller with:

@inject @any private instance<controllerbaseclass> controllerinstance; (...) controllerbaseclass controller = controllerinstance.select(«something»).get(); controller.processrequest(); // or execute() or want. request.getrequestdispatcher(«page»).forward(request, response); // or redirect if it's destructive operation.

what need pass select(«something»)? can select instance in cdi using annotation literals (consult cdi documentation) or classes (class.forname(...)), depending on request parameters.

using cdi can create controllers @applicationscoped instead of @requestscoped.

as see, may need build infrastructure , stick patterns on, it's feasible. have used mix of both strategies in personal project , works fine.

java java-ee model-view-controller

No comments:

Post a Comment