SpringMVC integrates freeMarker to make pages static +SpringMVC configates multiple views
1. Background:
FreeMarker is a template engine, a general-purpose tool for generating text output based on templates, written in pure Java. FreeMarker is designed to generate HTML Web pages, especially applications based on the MVC pattern. Although FreeMarker has some programming capabilities, it is usually Java programs that prepare the data to be displayed, and FreeMarker generates the pages and displays the prepared data through templates
2. The characteristics of
- Ability to generate all kinds of text: HTML,XML,RTF,Java source code, etc
- Easy to embed in your product: lightweight, no Servlet environment required
- Plug-in template loader: Templates can be loaded from any source, such as local files, databases
- You can press you need to generate text, save to the local file; Sent as Email, sent from the Web application back to the browser
integration
This example integrates FreeMarker on top of Spring, so it doesn’t focus on some of spring’s configurations.
Where: spring-mVC. XML is the spring and freeMarker integration configuration file, and in the web-INF folder, FTL is the freeMarker template and JSP is the Spring template.
1. Introduce dependencies in the POM.xml file
< the dependency > < groupId > org. Freemarker < / groupId > < artifactId > freemarker < / artifactId > < version > 2.3.23 < / version > </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> < version > 5.0.5. RELEASE < / version > < / dependency >Copy the code
- Configuration in spring-mVC.xml
<! - parser configuration Jsp view - > < bean class = "org. Springframework. Web. Servlet. The InternalResourceViewResolver" > < property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> <property name="order" value="1" /> </bean> <! - configuration freeMarker template path - > < bean class = "org. Springframework. Web. Servlet. The freeMarker. FreeMarkerConfigurer" > < property name="templateLoaderPath" value="/WEB-INF/ftl/" /><! <property name="defaultEncoding" value=" UTF-8 "/> <entry key="xml_escape" value-ref="fmXmlEscape" /> </map> </property> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">0</prop> <prop key="default_encoding">UTF-8</prop> <prop key="number_format">0.##########</prop> <prop key="date_format">yyyy-MM-dd</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="locale">zh_CN</prop> <prop key="classic_compatible">true</prop> <prop key="template_exception_handler">ignore</prop> </props> </property> </bean> <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/> <! - configuration parser freeMarker view - > < bean class = "org. Springframework. Web. Servlet. The freeMarker. FreeMarkerViewResolver" > < property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/> <property name="contentType" value="text/html; charset=UTF-8"></property> <property name="requestContextAttribute" value="request" /> <property name="exposeSpringMacroHelpers" value="true" /> <property name="exposeRequestAttributes" value="true" /> <property name="exposeSessionAttributes" value="true" /> <property name="cache" value="true" /> <property name="prefix" value="" /> <property name="suffix" value=".ftl" /> <property name="order" value="0"/> </bean>Copy the code
- The order property in the JSP view parser and FreeMarker view parser is configured only for multiple views in a Spring project. Set FreeMarker’s order to 0 and JSP to 1. This means that when looking for a view, look for an FTL file and then look for a JSP file as a view