背景:
阅读新闻
Spring MVC整合Freemarker基于注解方式
[日期:2013-02-20] 来源:Linux社区 作者:yuanq20 [字体:]
基于网络改进为:最正常版本
<?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 
  
  
 "> 
 
<!-- 针对freemarker的视图配置 --> 
<bean 
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 
<property value="5" /> 
<property value=".ftl" /> 
<property value="text/html;charset=UTF-8" />
</bean>
<bean 
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> 
<property value="/WEB-INF/view/" /> 
<property> 
<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>
6.Controller建立
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就是视图的名称(welcom.ftl) 
ModelAndView mv = new ModelAndView("welcom"); 
 mv.addObject("name", "My First Spring Mvc"); 
return mv; 
 } 
}
在url上敲:8080/welcome就会到WEB-INF/view/welcom.ftl页面渲染数据
 
7.welcom.ftl页面
<!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>
页面出来的效果:
Hello My First Spring Mvc
相关资讯
		       
	
本文评论 查看全部评论 (0)
 尊重网上道德,遵守中华人民共和国的各项有关法律法规 承担一切因您的行为而直接或间接导致的民事或刑事法律责任 本站管理人员有权保留或删除其管辖留言中的任意内容 本站有权在网站内转载或引用您的评论 参与本评论即表明您已经阅读并接受上述条款
评论声明
最新资讯
