Explanation and use of Vert.x-web (iii)

Source: Internet
Author: User

The explanation and use of vert.x-web in the last chapter (iii)





Cross-domain resource sharing (CORS handling)

Cross-domain resource sharing It is a secure mechanism to request a resource for multiple domains.

Vert.x-web contains a corshandler for you to handle the Cors protocol.

For example:

router.route().handler(CorsHandler.create("vertx\\.io").allowedMethod(HttpMethod.GET));router.route().handler(routingContext -> {  // Your app handlers});
To learn more, see the cors documentation.



Templates (Templates)

Vert.x-web enables dynamic page generation with several popular template engines that are available out of the box. Can be added to your project very simply.

Templateengine defines the template engine, and you can use render to render the template.

The simplest way to use a template is to use Templatehandler instead of using the template engine, templatehandler the path to the HTTP request to invoke the template engine for you.

By default Templatehandler will look for the template under the templates path. You can also configure additional paths.

Templatehandler The default conten-type of the returned result is text/html. You can set it to a different value.

When you create a templatehandler you need to send an instance of Templateengine to it.

Here's an example:

TemplateEngine engine = HandlebarsTemplateEngine.create();TemplateHandler handler = TemplateHandler.create(engine);// This will route all GET requests starting with /dynamic/ to the template handler// E.g. /dynamic/graph.hbs will look for a template in /templates/dynamic/graph.hbsrouter.get("/dynamic/").handler(handler);// Route all GET requests for resource ending in .hbs to the template handlerrouter.getWithRegex(".+\\.hbs").handler(handler);

mvel template engine (mvel)

When you use the Mvel template engine, if you do not specify a special extension name, the template engine will match the template file with the. templ extension in the default directory.

Routingcontext can be used in context variables of the mvel template, which means that you can render a template based on Routingcontext any object, such as request,response,session or context data.

For example:

The request path is @{context.request (). Path ()}the variable ' foo ' from the session is @{context.session (). Get (' foo ')}the V Alue ' Bar ' from the context data is @{context.get (' Bar ')}
Consult the Mvel template documentation to learn how to use the Mvel template.

Jade Template engineWhen you use the Jade template engine, if you do not specify a special extension name, the template engine will match the template file with the. jade extension in the default directory.

Routingcontext can be used in the context variables of the jade template, which means that you can render the template based on Routingcontext any object, for example: request,response,session or context data.

For example:

!!! 5html  head    title= context.get (' foo ') + context.request (). Path ()  body
Check out the Jade template documentation to learn how to use the jade template.

handlebars template engine (handlebars)When you use the handlebars template engine, if you do not specify a special extension name, the template engine will match the template file with the. hbs extension in the default directory.

The Handlerbars template engine does not support the invocation of any method of an object, so we cannot simply pass the Routingcontext object to it to parse itself, as with other templates.

Conversely, contextdata can be used in templates.

If you want to use data such as request paths, request parameters, or session data, you need to add them to the Context database before Templatehandler processing.

For example:


TemplateEngine engine = HandlebarsTemplateEngine.create();TemplateHandler handler = TemplateHandler.create(engine);router.get("/dynamic").handler(routingContext -> {  routingContext.put("request_path", routingContext.request().path());  routingContext.put("session_data", routingContext.session().data());  routingContext.next();});router.get("/dynamic/").handler(handler);

Consult the handlebars template documentation to learn how to use the handlebars template.

thymeleaf template engine (Thymeleaftemplate engine)When you use the Thymeleaf template engine, if you do not specify a special extension name, the template engine will match the template file with the. html extension in the default directory.

Routingcontext can be used in the context variables of the jade template, which means that you can render the template based on Routingcontext any object, for example: request,response,session or context data.

For example:

[Snip]<p th:text= "${context.get (' foo ')}" ></p><p th:text= "${context.get (' Bar ') } "></p><p th:text=" ${context.normalisedpath ()} "></p><p Th:text= "${context.request (). params (). Get (' param1 ')} ' ></p><p th:text= ' ${ Context.request (). params (). Get (' param2 ')} "></p> [Snip]

Consult the Thymeleaf template documentation to learn how to use the Thymeleaf template.

Fault Handler (error handler)You can use a Templatehandler or other method to render your error, but Vert.x-web provides a nice out-of-the-box error handler that you can use to render the error.

This handler is ErrorHandler, which, like a failed handler, can be easily used by setting it to the path you want to overwrite.

request log (requests logger)You can use Loggerhandler to log the HTTP request.

The default request will be logged to the Vert.x log, which can be configured to use JUL,LOG4J or slf4j.

Timeout Processing (timeout handler)Vert.x-web contains a timeouthandler that you can use to time out a request that you think takes too long to process.

You can configure an instance of Timeouthandler.

If a request times out, response will return a 408 response to the client.

Here's an example of using the Timeouthandler, and all requests to the/foo path will time out after five seconds without a response.

router.route("/foo/").handler(TimeoutHandler.create(5000));

Explanation and use of Vert.x-web (iii)

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.