spring mvc整合freemarker基于注解方式

基于网络改进为:最正常版本

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans 
  3.     xmlns="http://www.springframework.org/schema/beans" 
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  5.     xmlns:p="http://www.springframework.org/schema/p" 
  6.     xmlns:context="http://www.springframework.org/schema/context" 
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
  9.     http://www.springframework.org/schema/context  
  10.     http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 
  11.  
  12.     <!-- 针对freemarker的视图配置 --> 
  13.     <bean id="viewResolver" 
  14.         class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 
  15.         <property name="order" value="5" /> 
  16.         <property name="suffix" value=".ftl" /> 
  17.         <property name="contentType" value="text/html;charset=UTF-8" />
  18.     </bean> 
  19.     
  20.       
  21. <bean id="freemarkerConfig" 
  22.         class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> 
  23.         <property name="templateLoaderPath" value="/WEB-INF/view/" /> 
  24.         <property name="freemarkerSettings"> 
  25.             <props> 
  26.                 <prop key="template_update_delay">0</prop> 
  27.                 <prop key="default_encoding">UTF-8</prop> 
  28.                 <prop key="number_format">0.##########</prop> 
  29.                 <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> 
  30.                 <prop key="classic_compatible">true</prop> 
  31.                 <prop key="template_exception_handler">ignore</prop> 
  32.             </props> 
  33.         </property> 
  34.     </bean> 
6.Controller建立
 
  1. import javax.servlet.http.HttpServletRequest; 
  2.   
  3. import org.springframework.stereotype.Controller; 
  4. import org.springframework.web.bind.annotation.RequestMapping; 
  5. import org.springframework.web.bind.annotation.RequestMethod; 
  6. import org.springframework.web.servlet.ModelAndView; 
  7.   
  8. @Controller 
  9. public class SpringMvcController { 
  10.   
  11.     @RequestMapping(value="/welcome",method={RequestMethod.GET})  
  12.     public ModelAndView getFirstPage(HttpServletRequest request) { 
  13.         //welcom就是视图的名称(welcom.ftl) 
  14.         ModelAndView mv = new ModelAndView("welcom"); 
  15.         mv.addObject("name""My First Spring Mvc"); 
  16.         return mv; 
  17.     } 
 
 
在url上敲http://localhost:8080/welcome就会到WEB-INF/view/welcom.ftl页面渲染数据
7.welcom.ftl页面
 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
  5. <title>Insert title here</title> 
  6. </head> 
  7. <body> 
  8. Hello ${name} 
  9. </body> 
  10. </html> 
页面出来的效果: 
 
  1. Hello My First Spring Mvc 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值