Model Object Scopes - Lecture notes 7 PDF

Title Model Object Scopes - Lecture notes 7
Course Java Intermediate (ODE) 
Institution Georgian College
Pages 4
File Size 47.5 KB
File Type PDF
Total Downloads 77
Total Views 135

Summary

Model Object Scopes...


Description

If you want to use a different name for the CDI beans, you can use @Named with a parameter: import javax.enterprise.context.RequestScoped; import javax.inject.Named; @Named("user") @RequestScoped public class UserData { private String name; private String email; // Getters and setters... } You can then use the altered name in a view page: ${user.name}. Since, in @Inject, the reference happens by class name and not by an annotation parameter, for injection into a Java class, you still use @Inject private UserName userName;, even with the altered name. Model Object Scopes If you’re using CDI to manage model data, model class instances subordinate to a lifecycle control governed by CDI. This means CDI decides when to construct beans and when to abandon them. In

injected beans, the way CDI controls the lifecycle of instances is by a characteristic called scope. In Java MVC, the following scopes exist: • Request scope: An instance of an injected bean is created during an HTTP request and prevails only for the lifespan of the HTTP request and the response sent to the client (the browser). A typical usage scenario of request scope variables is when communicating POST form data or GET query parameters to the view layer page defined in the response. So you inject @Named request scope beans into controllers, set their fields there, and use the beans in the view layer. Because the lifespan of request scoped beans is short, they help keep the memory footprint of a web application low and avoid memory leaks. Session scope: A session is bound to a browser window and spans several HTTP request/response cycles. A session is started whenever the user enters a web application and terminates upon some timeout or an explicit session cancellation. Session scoped data objects prevail until some timeout is fired or the session is explicitly closed. You use session scoped objects when you need to maintain state with a lifecycle exceeding a single HTTP request/response cycle. Session data simplifies state handling, but significantly increases the danger of

having a memory consuming web application or of establishing destabilizing memory leaks. Redirect scope: In order to support the POST-redirect-GET design pattern, Java MVC defines a redirect scope for CDI beans. You use this pattern if you want to avoid reposts when a browser user clicks the reload button prior to a POST action being terminated. The lifecycle of beans with a redirect scope span the POST and a subsequent GET (because the browser was made to receive a redirect code 303). In the Java MVC controller, you start POST-redirect-GET by either returning aResponse.seeOther( URI.create("response/path" )).build() or a string "redirect:response/path" from inside the method that handles the POST. The process is as follows: 1. The user enters data in a form and submits it. The Java MVC controller is invoked. 2. The controller works through the form parameters, and the method in the end returns Response.seeOther( URI.create("response/path" )).build()or "redirect:response/path". 3. The browser automatically sends a redirect to the given path.

4. The response/path path (adapt it accordingly) points to another controller method with the GET verb. It advances to a view page showing the appropriate response to the user’s request. The redirect scope CDI beans span a lifetime from the original POST request to the response generated by the subsequent GET request, which is two HTTP request/response cycles.  Application scope: Any application-wide user-independent data can use this scope. Data prevails until the web application is undeployed or the server is stopped.  Dependent scope: This is a pseudo-scope. It means the CDI bean gets the same scope as the bean it was activated from. The dependent scope is the default if no scope is explicitly set....


Similar Free PDFs