Static resource ing and server push technology in spring MVC developed by JavaEE, javaeespringmvc

Source: Internet
Author: User

Static resource ing and server push technology in spring MVC developed by JavaEE, javaeespringmvc

In the previous blog, we talked about the custom interceptor and Exception Handling in spring MVC developed by Java EE. In this blog, we will continue to talk about spring MVC. Below we will talk about loading configurations of static files such as js and css, and the two implementation methods of server push. Of course, JQuery is used for server push. So let's talk about how to load static resource files and then how to implement server push.

Below are two ways to implement Server push, one is SSE (Server Send Event (Server push Event) and the other is push Based on Servlet asynchronous processing, the detailed implementation method is provided below, and the difference between the two is given.

 

I. Static resource file ing

The configuration of static resource file ing in Spring MVC is also relatively simple. Just configure it in our Spring Config file. Below is what we did when configuring static resource files.

 

1. Map resource files

First, we created the moth assets file under the src/main/resources package, which stores all the static resource files used in our project. Then, rewrite the addResourceHandlers () method in the Spring configuration file to configure the "assets" directory.

  

 

 

2. Resource file reference

Create a jquery_test.jsp file, which introduces the jquery. js file under the js folder in the assets folder. JQuery is used in jquery_test.jsp. Below is all the content of the file. Of course, the function of the page below is relatively simple, that is, click the button to dynamically add new nodes to HTML. The Code is as follows:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 

 

3. Test the above page

Of course, to access the above page, you must configure routes in the Spring configuration file. The following code snippet is the quick configuration of static file routing in the Spring configuration file.

  

 

The following figure shows the access result of the corresponding route. The following example shows that the jquery. js resource file can be accessed normally.

  

 

 

 

Ii. Server Send Event (Server push Event)

Server Send Event (SSE) can be used to Send events on the Server like a browser, that is, PUSH on the Server. The implementation principle of the server push technology discussed in this blog is that when the client sends a request to the server, the server will seize this request and return it to the client only when there is data update. After the client receives the message, it sends the request to the server again and again.

The network requests pushed by the server and sent by the client are all one-way communication. The following blog will introduce a two-way communication technology: WebSocket. In this article, we will first talk about the push events on the server.

 

1. Create SSEController

First, we create a general SpringMVC Controller named ssecontrler. Below is the specific implementation of the SSEController class, which is similar to that of a common Controller. However, when configuring routes, set the text type of the produces attribute to "text/event-stream.

In the push () method of the following class, a message is sent to the client every Ms. The message content is the current time, as shown below:

  

 

 

2. Create the requested JSP page

After creating the above class, we should create the JSP page for testing the Controller above. We will create a sse. jsp page in the corresponding resource directory. In sse. on the jsp page, we will use the EventSource object in JavaScript to listen to the event message of the "/sse" route. After receiving the event initiated by the above Controller, will do some things in the Event Callback. Of course, what we do is to add a new node to the HTML page and add the Event Response Message to the HTML text for display.

The following is the code for the sse. jsp page.

1 <% @ page language = "java" contentType = "text/html; charset = UTF-8" 2 pageEncoding = "UTF-8" %> 3 <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" http://www.w3.org/TR/html4/loose.dtd "> 4 

 

3. test our SSE

The code of the event sender and event listener has been completed. Next we will perform the test. Before testing, we need to add an access route for our sse. jsp page. In the Spring configuration file, we can quickly configure the routing of the sse. jsp page. The following is the code for sse. jsp route Configuration:

registry.addViewController("/ssetest").setViewName("/sse");

 

After configuring the preceding route, we can access the JSP page corresponding to the preceding route. The following figure shows the specific running effect. From the demo below, we can easily see that a message event from the server is received at intervals, as shown below:

  

 

 

 

Iii. asynchronous push in Servlet

Next, we will use Servlet asynchronous processing and Spring's task plan (timer) to push events. Of course, the final implementation effect of this part is the same as the above, but the implementation method is different. SSE requires support from new browsers, while Servlet Asynchronous Method push is cross-browser. Next, let's take a good look at this technical point.

 

1. Implement configuration before asynchronous push in Servlet

First, we need to configure it in the Spring configuration file. Our Spring supports Scheduleing, which is actually a timer. Because we need to regularly push to the client, the timer configuration is required.

  

 

After starting the timer, We need to enable asynchronous Servlet support in the class initialized by the Web, as shown below.

  

 

 

2. Create a Push Service

After the configuration is complete, what we need to do next is to create our Push Service. The Service is responsible for pushing events to the client. The Class Object of the Push Service is the dependent object of the corresponding Controller. Later, we will inject the dependency into the corresponding Controller for event Push.

Below is the code implementation of the PushService class, which needs to be modified using @ Service. A DeferredResult object is instantiated to transmit event messages. We use the @ Scheduled annotation to set the interval of each push.

  

 

The @ Service annotation is used above. We can click it to see the content in the Service annotation. From its source code, we can easily see that the @ Service and @ Component usage is always the same. The implementation of @ Service annotation is as follows:

  

 

 

3. Create a Controller that calls PushService

After the PushService is created, create a Controller class that calls the PushService. The following ServletAsyncController is the Controller that calls the PushService object. The @ Autowired annotation is used to declare the injection point of dependency injection. Then, use the route to the method that calls PushService. DeferredResult <String> is the carrier of the push event, and the message type is String. The specific implementation is as follows:

  

 

 

4. Implementation of JSP pages for receiving events

After creating the Push Service and the Controller responsible for pushing, we should create the client code for receiving the Push. The code below is relatively simple. It mainly uses jQuery to receive push events. Append the pushed content to html for display, as shown below:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

 

The routing configuration code for the preceding JSP page is omitted here. As before, configure a route for the preceding JSP page in the SpringConfig file, where "/async_push" is used ", then we access the route. The following figure shows the access effect, as shown below:

  

 

This blog is here, and the sharing address of the above example on github is: https://github.com/lizelu/SpringMVCWithMaven

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.