enterprise modeling of mvc and java ee artifacts · enterprise modeling of mvc and java ee...
Post on 02-Apr-2018
232 Views
Preview:
TRANSCRIPT
Enterprise Modeling of MVC and Java EE Artifacts
Ivar Grimstad Principal Consultant, Cybercom Sweden Gaurav Gupta JPA Modeler creator and lead developer Josh Juneau Applications Developer and Systems Analyst, Fermi National Accelerator Laboratory
View@Controller@Path(“hello”)publicclassHelloController{
@GETpublicStringhello(){return“hello.jsp”;}}
View@Controller@Path(“hello”)publicclassHelloController{
@GETpublicViewablehello(){returnnewViewable(“hello.jsp”);}}
View@Controller@Path(“hello”)publicclassHelloController{
@GETpublicResponsehello(){returnResponse.status(OK).entity(“hello.jsp”).build();}}
Model@Named(“greeting”)@RequestScopedpublicclassGreeting{
privateStringmessage;publicvoidsetMessage(Stringmessage){this.message=message;}
publicvoidgetMessage(){returnmessage;}}
Model@View(“hello.jsp”)@Controller@Path(“hello”)publicclassHelloController{
@GETpublicvoidhello(){}}
Model@View(“hello.jsp”)@Controller@Path(“hello”)publicclassHelloController{
@InjectprivateGreetinggreeting;@GETpublicvoidhello(){greeting.setMessage(“HelloJavaOne!”);}}
Model<%@page contentType=“text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>MVC 1.0 Hello Demo</title> </head> <body> <h1>Hello ${greeting.message}</h1> </body> </html>
Model@View(“hello.jsp”)@Controller@Path(“hello”)publicclassHelloController{
@GETpublicvoidhello(){}}
Model@View(“hello.jsp”)@Controller@Path(“hello”)publicclassHelloController{
@InjectprivateModelsmodel;@GETpublicvoidhello(){model.put(“message”,“HelloJavOne!”);}}
Model<%@page contentType=“text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>MVC 1.0 Hello Demo</title> </head> <body> <h1>Hello ${message}</h1> </body> </html>
@Controller@Path("form")publicclassFormController{
@POSTpublicResponseformPost(@Valid@BeanParamFormDataBeanf){
returnResponse.status(OK).entity(“data.jsp”).build();}}
Validation
ConstraintValidationException
@Min, @NotNulletc.
ValidationpublicclassFormViolationMapperimplementsExceptionMapper<ConstraintViolationException>{
publicResponsetoResponse(ConstraintViolationExceptione){
Set<ConstraintViolation<?>>s=e.getConstraintViolations();
//processviolations...returnResponse.status(BAD_REQUEST).entity(“error.jsp”).build();}}
@Controller@Path("form")publicclassFormController{@InjectprivateBindingResultbr;
@POSTpublicResponseformPost(@Valid@BeanParamFormDataBeanf){if(br.isFailed()){returnResponse.status(BAD_REQUEST).entity(“error.jsp”).build();}returnResponse.status(OK).entity(“data.jsp”).build();}}
ValidationAllows for MVC Error Handling
Cross Site Request Forgery@Controller@Path(“csrf”)publicclassHelloController{
@CsrfValid@POSTpublicResponsepost(@FormParam(“greeting")Stringgreet{}}
Cross Site Request Forgery<%@page contentType=“text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>MVC 1.0 Hello Demo</title> </head> <body> <form action="csrf" method="post" accept-charset="utf-8"> <input type="submit" value="Click here"/> <input type="hidden" name="${mvc.csrf.name}" value="${mvc.csrf.token}"/> </form> </body> </html>
JPA Modeler
• Entity and Database mapping tool
• Java EE 8 source code generator
• Wires the technology together
DB Modeler
• Design schema transparently
• Multi Database support (e.g PostgreSQL, MySQL, Oracle etc)
• Generate SQL
• ORM knowledge not required
▪ MVC 1.0 (JSR - 371)
▪ JAX-RS (REST)
▪ Security API (JSR - 375)
▪ CDI
▪ Bean Validation
▪ EJB
▪ JSP
▪ Swagger
▪ Metrics
▪ Angular JS
▪ Responsive Web Design
▪ HTML5 Boilerplate
▪ Twitter Bootstrap
Technology Stack
Using the MVC Plugin
• Obtain and install the MVC Plugin
• Generate project with model and controllers
• View and navigate to MVC Controllers
• Create New Controller Class
Plugin – In Progress• Work is in progress for the MVC plugin • Ability to create new MVC application that will include simple
controller method and view • Easy navigation from controller to view
• Send comments/feedback/suggestions my way
• Sources Available: • https://github.com/juneau001/BasicMVCSupport
JSF Overview
• Mature Framework for Java EE
• Follows MVC Architecture
• Component-Based
• Many Libraries Available (PrimeFaces, PrettyFaces, MyFaces, etc.)…get up and running quickly with so many components available
• Utility Libraries also (PrimeFaces, OmniFaces, etc.)
JSF in NetBeans IDE
• Create Maven Web Project
• Generate data model (entity classes from database)
• Generate JPA Diagram from entity classes • Can also generate database tables and entity classes with JPA
Modeler • Generate controllers and views
MVC & JSFDifferent Use-Cases for Different Solutions
JSF • Component Based • Controller Logic• Automates Processing• Facelets• Rapid Development• Works well with REST• Stateful
MVC • Action-Based • Layered on top of JAX-RS• Manual Processing• Various View Options• Fine-grained Control• Great fit for REST • No State…Request-
based
@ivar_grimstadUGF6435 - JavaOne 2016
Project Page https://java.net/projects/mvc-spec
GitHub https://github.com/mvc-spec
JPA Modeler http://jpamodeler.github.io/
top related