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