Spring MVC consolidation Freemarker based on annotations-practical tips

Source: Internet
Author: User
web-based improvements to: the most normal version
Copy Code code 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 established
Copy Code code 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 name of the view (WELCOM.FTL)
Modelandview mv = new Modelandview ("welcom");
Mv.addobject ("name", "My I-Spring Mvc");
return MV;
}
}


Knocking http://localhost:8080/welcome on the URL will render the data on the WEB-INF/VIEW/WELCOM.FTL page
WELCOM.FTL page
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=iso-8859-1 ">
<title>insert title here</title>
<body>
Hello ${name}
</body>

The effect of the page:
Hello my i-Spring MVC

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.