• Complain

Arjan Tijms - The Definitive Guide to JSF in Java EE 8: Building Web Applications with JavaServer Faces

Here you can read online Arjan Tijms - The Definitive Guide to JSF in Java EE 8: Building Web Applications with JavaServer Faces full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2018, publisher: Apress, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Arjan Tijms The Definitive Guide to JSF in Java EE 8: Building Web Applications with JavaServer Faces

The Definitive Guide to JSF in Java EE 8: Building Web Applications with JavaServer Faces: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "The Definitive Guide to JSF in Java EE 8: Building Web Applications with JavaServer Faces" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Learn and master the new features in the JSF 2.3 MVC web framework in this definitive guide written by two of the JavaServer Faces (JSF) specification leads. The authors take you through real-world examples that demonstrate how these new features are used with other APIs in Java EE 8. Youll see the new and exciting ways JSF applications can use to communicate between a client and a server, such as using WebSockets, invoking bean methods directly from Ajax, executing client-side JavaScript when Ajax calls complete, and more
Along the way youll broaden your knowledge of JSF components and web APIs best practices, and learn a great deal about the internals of JSF and the design decisions that have been made when building the JSF API. For example, youll see what artefacts are now CDI injectable, how CDI changed JSF internally, and what some of the caveats are when working with the CDI versions of a JSF artefact.
Furthermore, youll build an example application from scratch. After reading The Definitive Guide to JSF in Java EE 8, youll be ready to build your own efficient and secure web applications.
What You Will Learn
  • Leverage the new features in JSF 2.3 in your existing applications
  • Integrate JSF and CDI
  • Use the brand new Component Search Expression framework, which enables you to more easily locate components from your template
  • Extend the Component Search Expression framework with your own search operators
  • Work with the different ways of mapping requests to JSF, make your application use extensionless URLs, and programmatically inspect which resources are present in your application
  • Master the best practices for web application development and see which are obsolete

Who This Book Is For
Existing JSF or Java developers who need to create a web UI. No prior knowledge of JSF is required, but the book does skew towards the more experienced developer. Concepts such as dependency injection and MVC are assumed to be known, as is a general knowledge about HTML, HTTP and other web standards.

Arjan Tijms: author's other books


Who wrote The Definitive Guide to JSF in Java EE 8: Building Web Applications with JavaServer Faces? Find out the surname, the name of the author of the book and a list of all author's works by series.

The Definitive Guide to JSF in Java EE 8: Building Web Applications with JavaServer Faces — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "The Definitive Guide to JSF in Java EE 8: Building Web Applications with JavaServer Faces" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make

Bauke Scholtz, Arjan Tijms 2018

Bauke Scholtz and Arjan Tijms , The Definitive Guide to JSF in Java EE 8 ,

10. WebSocket Push

Bauke Scholtz 1 and Arjan Tijms 2

(1) Willemstad, Curaao

(2) Amsterdam, Noord-Holland, The Netherlands

JSF 1.0 introduced HTML form-based POST action support. JSF 2.0 introduced AJAX-based POST support. JSF 2.0 introduced GET query string parameter mapping support. JSF 2.2 introduced GET-based action support. JSF 2.3 introduces WebSocket support.

JSFs WebSocket support is represented by the new tag, the PushContext interface , and the @Push annotation. It is built on top of the JSR-356 WebSockets specification, introduced in Java EE 7. Therefore, it is technically possible to use it in a Java EE 7 environment as well. JSR-356 is even natively supported in Tomcat since 7.0.27 and in Jetty since 9.1.0.

In Mojarra , the has an additional Java EE 7 dependency: JSON-P (JSR-353). In case youre targeting Tomcat or Jetty instead of a Java EE application server, you might need to install it separately. JSON-P is internally used to convert Java objects to a JSON string so that it can, without much hassle, be transferred to the client side and be provided as an argument of JavaScript listener function attached to .

Configuration

The JSR- WebSocket specification does not officially support programmatic initialization of the socket end point during runtime. So we cannot initialize it by simply declaring in the view and wait until a JSF page referencing it is being opened for the first time. We really need to initialize it explicitly during deployment time. We could do that by default, but having an unused WebSocket end point open forever is not really nice if its never used by the web application. So we cannot avoid having a context parameter to explicitly initialize it during deployment time.


javax.faces.ENABLE_WEBSOCKET_ENDPOINT
true

If you prefer programmatic initialization over declarative initialization, then you can always use ServletContext#setInitParameter() in a ServletContainerInitializer of your web fragment library as follows:

public class YourInitializer implements ServletContainerInitializer {
@Override
public void onStartup(Set> types, ServletContext context) {
context.setInitParameter(
PushContext.ENABLE_WEBSOCKET_ENDPOINT_PARAM_NAME, "true");
}
}

Note that it is not possible to perform this task in a ServletContextListener as JSF will actually register the WebSocket end point in its own ServletContainerInitializer implementation which always runs before any ServletContextListener .

Once the WebSocket end point is enabled and successfully initialized during deployment, it will listen for WebSocket handshake requests on the URL (uniform resource locator) pattern /javax.faces.push/* . The first path element will represent the WebSocket channel name.

Coming back to officially, some WebSocket implementations do, however, support programmatic initialization, such as the one provided by Undertow , which is in turn used in WildFly. Unfortunately, the spec doesnt say so, and there may be WebSocket implementations that simply do not support programmatic initialization, such as Tyrus as used in Payara.

The WebSocket container will, by default, listen for handshake requests on the same port as the application server is listening for HTTP requests. You can optionally change the port with another web.xml context parameter,


javax.faces.WEBSOCKET_ENDPOINT_PORT
8000

or programmatically in a ServletContainerInitializer :

context.setInitParameter(
PushContext.WEBSOCKET_ENDPOINT_PORT_PARAM_NAME, "8000");
Usage

In your JSF page , just declare the tag along with the required channel attribute representing the channel name and the optional onmessage attribute representing a reference to a JavaScript function.



function logMessage(message, channel, event) {
console.log(message);
}

The JavaScript function will be invoked with three arguments.

  1. message : the push message as JSON object.

  2. channel : the WebSocket channel name. This may be useful in case you intend to have a global listener, or want to manually control the close of the WebSocket.

  3. event : the original MessageEvent object. This may be useful in case you intend to inspect it in the JavaScript function.

On the WAR side, you can inject the PushContext via the @Push annotation in any web artifact that supports CDI injection. This can be a simple CDI managed bean, but it can also be a @WebServlet , @WebFilter or @WebListener .

import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.faces.push.Push;
import javax.faces.push.PushContext;
@Named @RequestScoped
public class Bean {
@Inject @Push
private PushContext test;
public void submit() {
test.send("Hello World!");
}
}

The PushContext variable name test must match the channel name declared in the JSF page. In case you cannot match the variable name with the channel name, you can always specify the channel name in the optional channel attribute of the @Push annotation.

@Inject @Push(channel="test")
private PushContext foo;

Once the submit() method of the bean shown before is invoked by some JSF command component, even in a different JSF page, the push message Hello World! will be sent to all opened sockets on the very same channel name, application wide.

Scopes and Users

As you may have realized, is thus, by default, application scoped. You can control the scope by the optional scope attribute. Allowed values are application , session , and view .

When set to session , the message will be sent to all opened sockets on the very same channel in the current session only.

This is particularly useful for progress messages coming from long-running session-scoped background tasks initiated by the user itself. This way the user can just continue browsing the site without the need to wait for the result on the very same page.

Alternatively, you can also set the optional user attribute to a serializable value representing the unique user identifier, which can be a String representing the user login name or a Long representing the user ID. When this attribute is set, the scope of the socket will automatically default to session and it cannot be set to application .

This offers the opportunity to send a message to a specific user as follows:

private String message;
private User recipient;
@Inject @Push
private PushContext chat;
public void sendMessage() {
Long recipientId = recipient.getId();
chat.send(message, recipientId);
}

You can even send it to multiple users by providing a Set argument.

private String message;
private Set recipients;
@Inject @Push
private PushContext chat;
public void sendMessage() {
Set recipientIds = recipients.stream()
.map(User::getId)
.collect(Collectors.toSet());
chat.send(message, recipientIds);
}

In other words, you can easily implement a chat box this way. Incidentally, real-time user targeted notifications at, for example, Stack Overflow and Facebook work this way.

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «The Definitive Guide to JSF in Java EE 8: Building Web Applications with JavaServer Faces»

Look at similar books to The Definitive Guide to JSF in Java EE 8: Building Web Applications with JavaServer Faces. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «The Definitive Guide to JSF in Java EE 8: Building Web Applications with JavaServer Faces»

Discussion, reviews of the book The Definitive Guide to JSF in Java EE 8: Building Web Applications with JavaServer Faces and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.