Spring mvc integrates freemarker Based on Annotation

Source: Internet
Author: User

Network-based improvement: the most normal version
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans
Xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: p = "http://www.springframework.org/schema/p"
Xmlns: context = "http://www.springframework.org/schema/context"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd>

<! -- View configuration for freemarker -->
<Bean id = "viewResolver"
Class = "org. springframework. web. servlet. view. freemarker. FreeMarkerViewResolver">
<Property name = "order" value = "5"/>
<Property name = "suffix" value = ". ftl"/>
<Property name = "contentType" value = "text/html; charset = UTF-8"/>
</Bean>


<Bean id = "freemarkerConfig"
Class = "org. springframework. web. servlet. view. freemarker. FreeMarkerConfigurer">
<Property name = "templateLoaderPath" value = "/WEB-INF/view/"/>
<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 = "datetime_format"> yyyy-MM-dd HH: mm: ss </prop>
<Prop key = "classic_compatible"> true </prop>
<Prop key = "template_exception_handler"> ignore </prop>
</Props>
</Property>
</Bean>

Controller Creation
Copy codeThe Code is as follows:
Import javax. servlet. http. HttpServletRequest;

Import org. springframework. stereotype. Controller;
Import org. springframework. web. bind. annotation. RequestMapping;
Import org. springframework. web. bind. annotation. RequestMethod;
Import org. springframework. web. servlet. ModelAndView;

@ Controller
Public class SpringMvcController {

@ RequestMapping (value = "/welcome", method = {RequestMethod. GET })
Public ModelAndView getFirstPage (HttpServletRequest request ){
// Welcom is the view name (welcom. ftl)
ModelAndView mv = new ModelAndView ("welcom ");
Mv. addObject ("name", "My First Spring Mvc ");
Return mv;
}
}


Hitting http: // localhost: 8080/welcome on the url will render the data to the WEB-INF/view/welcom. ftl page
Welcom. ftl page
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = ISO-8859-1">
<Title> Insert title here </title>
</Head>
<Body>
Hello $ {name}
</Body>
</Html>

Page effects:
Hello My First Spring Mvc

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.