[Notes] "spring Combat"-rendering Web View

Source: Internet
Author: User
Tags html tags

I. Understanding and Analysis

Spring MVC defines an interface called Viewresolver, which is roughly as follows:

 Public Interface viewresolver {    throws  Exception;}

View is another interface.

 Public Interface View {    String getcontenttype ();     void throws Exception;}

The task of the view interface is to accept the model and the request and response objects of the servlet, and render the output to response.

All we need to do is write the Viewresolver and view implementations, put the rendered content into the response, and show it to the user's browser. In fact, Spring provides a number of built-in implementations.

Second, the use of thymeleaf

1. Configuring the Thymeleaf View Resolver

In order to use thymeleaf in spring, we need to configure three thymeleaf-enabled beans that are integrated with spring:

    • Thymeleafviewresolver: Resolves the logical view name to the Thymeleaf template view;
    • Springtemplateengine: Working with templates and rendering results;
    • Templateresolver: Loads the thymeleaf template.

The following is a Java configuration that declares these beans.

Previous versions of THYMELEAF3:

@Bean Publicviewresolver viewresolver (springtemplateengine templateengine) {thymeleafviewresolver ViewResolver=NewThymeleafviewresolver ();    Viewresolver.settemplateengine (Templateengine); returnViewresolver;} @Bean Publictemplateengine templateengine (templateresolver templateresolver) {springtemplateengine TemplateEngine=NewSpringtemplateengine ();    Templateengine.settemplateresolver (Templateresolver); returnTemplateengine;} @Bean Publictemplateresolver Templateresolver () {templateresolver templateresolver=NewServletcontexttemplateresolver (); Templateresolver.setprefix ("/web-inf/templates/"); Templateresolver.setsuffix (". html"); Templateresolver.settemplatemode ("HTML5"); returnTemplateresolver;}

THYMELEAF3 version:

@Bean Publicviewresolver viewresolver (springtemplateengine templateengine) {thymeleafviewresolver ViewResolver=NewThymeleafviewresolver ();    Viewresolver.settemplateengine (Templateengine); returnViewresolver;} @Bean Publictemplateengine templateengine (templateresolver templateresolver) {springtemplateengine TemplateEngine=NewSpringtemplateengine ();    Templateengine.settemplateresolver (Templateresolver); returnTemplateengine;} @Bean Publicitemplateresolver Templateresolver () {templateresolver templateresolver=NewSpringresourcetemplateresolver (); Templateresolver.setprefix ("/web-inf/templates/"); Templateresolver.setsuffix (". html"); Templateresolver.settemplatemode ("HTML5"); returnTemplateresolver;}

If you prefer to use XML to configure the Bean, the following <bean> declaration can accomplish the task.

<bean id= "Viewresolver"    class= "Org.thymeleaf.spring3.view.ThymeleafViewResolver"    P: Templateengine-ref= "templateengine"/><bean id= "Templateengine"    class= " Org.thymeleaf.spring3.SpringTemplateEngine "    p:templateresolver-ref=" Templateresolver "/>< Bean id= "Templateresolver"    class= "Org.thymeleaf.templateresolver.ServletContextTemplateResolver"     P:prefix= "/web-inf/templates"    p:suffix= ". html"    p:templatemode = "HTML5"/>

2. Defining thymeleaf Templates

Thymeleaf is to a large extent HTML file, unlike JSP, it has no special tag or tag library. Thymeleaf works because it adds the Thymeleaf property to the standard set of HTML tags through a custom named control.

[Notes] "spring Combat"-rendering Web View

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.