1.配置springmvc-servlet.xml,具体配置代码如下.这段代码必须放在.jsp视图之前
<!--freemarker页面解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix" value=".ftl"></property>
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="viewClass" value="com.gotobus.common.ExFreeMarkerView" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
</bean>
<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />
<!--配置Freemarker -->
<bean id="freemarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/ftl/"></property>
<property name="freemarkerVariables">
<map>
<entry key="xml_escape" value-ref="fmXmlEscape" />
</map>
</property>
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">10</prop>
<prop key="defaultEncoding">UTF-8</prop>
<prop key="auto_import">common/spring.ftl as spring</prop>
</props>
</property>
</bean>
<!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在/WEB-INF/jsp/目录下,查找XXX.jsp文件-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
注:标红处代表自动导入国际化配置文件,可以应用在模板里面:(ftl)
spring.ftl可以在jar包中找到,这里就不多说.
接下来,比较重要的一步就是:写我们自己的视图类
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.support.RequestContextUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerView;
import freemarker.template.SimpleHash;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class ExFreeMarkerView extends FreeMarkerView {
@Override
protected void doRender(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)
throws Exception {
exposeModelAsRequestAttributes(model, request);
SimpleHash fmModel = buildTemplateModel(model, request, response);
System.out.println("come here");
if (logger.isDebugEnabled()) {
logger.debug("Rendering FreeMarker template [" + getUrl() + "] in FreeMarkerView '" + getBeanName() + "'");
}
Locale locale = RequestContextUtils.getLocale(request);
if(Boolean.FALSE.equals(model.get("STATIC_HTML"))){
processTemplate(getTemplate(locale), fmModel, response);
}else{
String htmlFilePath=(String) model.get("htmlFilePath");
String htmlFileName=(String) model.get("htmlFileName");
createHTML(getTemplate(locale), fmModel, request, response,htmlFilePath,htmlFileName);
}
}
public void createHTML(Template template, SimpleHash model,HttpServletRequest request,
HttpServletResponse response,String htmlFilePath,String htmlFileName) throws IOException, TemplateException, ServletException {
String basePath = request.getSession().getServletContext().getRealPath("/");
String requestHTML=htmlFilePath+htmlFileName;
String htmlPath = basePath + requestHTML;
File generatorhtmlFile = new File(htmlPath);
if(!generatorhtmlFile.getParentFile().exists()){
generatorhtmlFile.getParentFile().mkdirs();
}
generatorhtmlFile.createNewFile();
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(generatorhtmlFile), "UTF-8"));
template.process(model, out);
out.flush();
out.close();
System.out.println("success");
request.getRequestDispatcher(requestHTML).forward(request, response);
}
}
这段代码,就是,在前台响应.do请求时,进行处理的视图界面:
测试的controller类防范的编写
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.gotobus.controller.BaseController;
import com.gotobus.service.impl.common.page.TourRoutePageService;
@Controller
public class PageController extends BaseController {
@RequestMapping("createTourRouteHtml.do")
public ModelAndView createTourRouteHtmlFile(){
ModelAndView modelAndView = new ModelAndView("index");
List list1 = new ArrayList();
Map map=new HashMap();
Map map1=new HashMap();
list1=tourRoutePageService.getModel();
map1=tourRoutePageService.getMap();
Iterator<Map> it = list1.iterator();
while (it.hasNext()) {
map = it.next();
modelAndView.addObject("test2", map.get("test1"));
}
modelAndView.addObject("content", "网站标题");
modelAndView.addObject("htmlFilePath", "/tour_route_pages_test/");
modelAndView.addObject("htmlFileName", "index.html");
modelAndView.addObject("html", tourRoutePageService.getHtml());
modelAndView.addObject("model", tourRoutePageService.getModel());
modelAndView.addObject("demo1", map1.get("demo1"));
modelAndView.addObject("demo2", map1.get("demo2"));
//当设置false时不生成静态页面
modelAndView.addObject("STATIC_HTML", true);
return modelAndView;
}
}
模板页面的测试代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<#import "/common/spring.ftl" as spring/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>new website---website---website</title>
</head>
<body>
this is result for conetent:${content}
guojihuapeizhi:<@spring.message "home"/>
shsahsajs:${html}
dsdsaada:
<#list model as map>
<#list map?keys as itemKey>
<input type="button" name="button1" value="<#if itemKey="test1">Phone:${map[itemKey]}</#if>"/>
<#if itemKey="test1">
Phone:${map[itemKey]}
</#if>
<#if itemKey="test2">
Email:${map[itemKey]}
</#if>
<#if itemKey="test3">
Address:${map[itemKey]}
</#if>
</#list><br/>
</#list>
<br>
dsadsadsad::::${test2}
<br>
wwwwwwwwwwwww:${demo1}<br>
tttttttttttt:${demo2}
</body>
</html>
推荐一下个人网站: 大神网