! Answer to your question "Java EE is useful framework as next Struts?" ! Views are Thymeleaf3 ! Codes on GitHub ! https://github.com/MasatoshiTada/jjug-action-based- mvc ! In this slide, some codes are omitted. Codes in GitHub are correct. 2
action-based MVC? ᶄ Getting started MVC 1.0(EE 8) ᶅ Using Jersey MVC and RESTEasy HTML in EE 7 ᶆ Other Java EE technologies you should know ᶇ Final conclusions 4
MVC 1.0 is specification ! Set of interfaces, annotations, exceptions ! Implementation is Ozark ! Implementation classes of interfaces above ! Reference Implementation 12
technologies? ! In Non-Goal of MVC specification, "Define a new view (template) language and processor." ! Instead, MVC provides integration with many view technologies! 15
can use in MVC 1.0 ! MVC 1.0 ! JSP, Facelets ! Ozark ! Thymeleaf, Mustache, Freemarker, Handlerbars, Velocity, AsciiDoc, etc. 16 Implement ViewEngine interface if you want to use other view
and Facelets 17 ! JSP ! Mix of view and logic ! Sometime occurs XSS ! Facelets ! Cannot use all features in MVC 1.0 ! Bad compatibility with JavaScript
18 ! Pure HTML! ! Isolate view and logic absolutely! ! Good compatibility with JavaScript! ! Escape strings by default! ! Other useful feature ! Link expression, internationalization, …
Thymeleaf in MVC 1.0 19 <dependency> <groupId>org.glassfish.ozark.ext</groupId> <artifactId>ozark-thymeleaf</artifactId> <version>1.0.0-m02</version> <scope>compile</scope> </dependency> ! Just adding dependency in pom.xml Thymeleaf 2 at this moment. In GitHub, I created original ViewEngine for Thymeleaf 3
token? ! In expert group’s maling-list, "This is a client side concern" ! https://java.net/projects/mvc-spec/lists/jsr371- experts/archive/2015-07/message/2 ! Spring MVC doesn’t have this feature, either 20
public class EmployeeController { @GET @Path("/index") @Controller public String index() { return "employee/index.html"; } } Path of view (extention is REQUIRED) Indicate as controller method Mapping to URL and HTTP request method ※Italic is feature of MVC jjug-mvc10/src/main/java/com/example/rest/controller/EmployeeController.java
to view 25 public class EmployeeController { @Inject private Models models; @GET @Path("/result") @Controller @ValidateOnExecution(type=ExecutableType.NONE) public String result(…) { models.put("employee", employee); return "employee/result.html"; } Hold value (Map + Iterator) Value referenced by view jjug-mvc10/src/main/java/com/example/rest/controller/EmployeeController.java ※Italic is feature of MVC
28 <ul th:if="${bindingResult}"> <li th:each="message : ${bindingResult.allMessages}" th:text="${message}"> This is dummy message </li> </ul> Get messages from BindingResult jjug-mvc10/src/main/webapp/WEB-INF/views/employee/index.html
Implement ExceptionMapper interface ! When exception occurs in controller methods, ExceptionMapper catches this exception ! You can create multiple ExceptionMappers according to exception types 33
Security (CSRF, XSS) ! File upload and download (Upload is Jersey’s specific feature) ! MvcContext ! Mix of HTML app and REST app (Hybrid class) ! For more details, see GUGJ’s slides ! http://www.slideshare.net/masatoshitada7/java-ee-8mvc-10 35
chapter ! MVC 1.0 + Thymeleaf cover most of features of Struts ! More easy, more secure! ! Need to implement transaction token and form-binding by yourself ! MVC 1.0 is simple, leverages existing Java EE features 36
! Register MvcFeature to Application subclass ! ResourceConfig is useful 41 @ApplicationPath("/api") public class MyApplication extends ResourceConfig { public MyApplication() { register(MvcFeature.class); property(MvcFeature.TEMPLATE_BASE_PATH, "/WEB-INF/views/"); packages(true, "com.example.rest"); } } jjug-jersey-mvc/src/main/java/com/example/rest/MyApplication.java ※Italic is feature of Jersey MVC
MVC ! Return Viewable in controller methods 42 @Path("/employee") public class EmployeeController { @GET @Path("/index") public ThymeleafViewable index(…) { return new ThymeleafViewable( "employee/index.html"); } } My original Viewable’s subclass jjug-jersey-mvc/src/main/java/com/example/rest/controller/EmployeeControler.java ※Italic is feature of Jersey MVC
over value to view in Jersey MVC ! Use 2nd parameter of Viewable constructor 43 @Path("/employee") public class EmployeeController { @GET @Path("/result") public ThymeleafViewable result(…) { Map<String, Object> models = … return new ThymeleafViewable( "employee/result.html", models); } Put value to Map jjug-jersey-mvc/src/main/java/com/example/rest/controller/EmployeeControler.java ※Italic is feature of Jersey MVC
@Provider public class ThymeleafTemplateProcessor extends AbstractTemplateProcessor<String> { private TemplateEngine templateEngine; @Inject public ThymeleafTemplateProcessor( Configuration config, ServletContext sc) { super(config, sc, "html", "html"); ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(sc); … templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(resolver); } // to next slide jjug-jersey-mvc/src/main/java/com/example/rest /thymeleaf/ThymeleafTemplateProcessor.java ※Italic is feature of Jersey MVC
MVC ! We can specify error view with @ErrorTemplate, but… ! Treats in the same way validation errors and exceptions ! Bad compatibility with MVC 1.0, because MVC 1.0 doesn’t have this feature ! Cannot use validation group and group sequence 47
! Advantage ! Pure JAX-RS code ! Good compatibility with MVC 1.0 ! Prevent excess customize ! Disadvantage ! Take some time 50 I thought and tried many way, this way is better
! JAX-RSͷίʔυͰॻ͚OK 52 @GET @Path("/redirect") public Response redirect( @Context UriInfo uriInfo) throws Exception { URI location = uriInfo.getBaseUriBuilder() .path(HelloResource.class) .path("redirect2") .build(); return Response.status(Response.Status.FOUND) .location(location).build(); } Has some methods to build URI Specify URI to Location header Status code is 3xx
! Create Application subclass ! No need to register specific class ! JAX-RS’s "Providers" feature 53 @ApplicationPath("/api") public class MyApplication extends Application { } jjug-resteasy-html/src/main/java/com/example/rest/MyApplication.java
sets TemplateEngine to Renderable class ! JAX-RS’s WriterInterceptor ! Create original NameBinding annotation ! This is not only way. We can think and try other way, too. 58
chapter ! In Java EE 7, use Jersey MVC or RESTEasy HTML! ! Most codes are same to MVC 1.0! ! No BindingResult feature, so validate in controller methods! 64
features of MVC 1.0/Jersey MVC/ RESTEasy HTML are not many ! To achieve your requirement and trouble- shooting, knowledge of JAX-RS is very important 66
MVC and RESTEasy HTML are NOT "Java EE 7 standard" ! They will be out of range of server vendors’ support ! In "Java EE 7 standard", JSF is the ONLY HTML Framework 71