问题描述
applicationContext.xml中配置Freemarker中文乱码问题(Struts2+SpringFreemarker)FTL中获取到得值生成HTML页面中文显示正常,但就是FTL模板中原有的中文显示乱码,在网上找了些方法基本都一样,只剩下这个方法没试,但在过程中遇到下面的问题。希望大家帮下忙,或者对于这种问题还有别的解决方案吗?<beanid="freemarderConfig"class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"><propertyname="templateLoaderPath"value="/ftl/"/><!--错误Nosetterfoundforproperty'templateLoaderPath'inclass'org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer'--><propertyname="freemarkerVariables"><!--错误Nosetterfoundforproperty'freemarkerVariables'inclass'org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer'--><map><entrykey="xml_escape"value-ref="fmXmlEscape"/></map></property><propertyname="freemarkerSettings"><!--Nosetterfoundforproperty'freemarkerSettings'inclass'org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer'--><props><propkey="defaultEncoding">UTF-8</prop></props></property></bean>
spring-webmvc.jar这个包已有,看了下源代码,对应类中没有那些属性,是不是本版问题呢?另外用Freemarker生成HTML页面过程中,原FTL里的中文已经是乱码,而通过Action获取到得值的中文却显示正常FTL头部分也加入<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>也没起到效果附上相关代码ConfigurationfreemarkerCfg=newConfiguration();//加载模板freemarkerCfg.setServletContextForTemplateLoading(ServletActionContext.getServletContext(),"/WEB-INF/template");freemarkerCfg.setEncoding(Locale.CHINA,"UTF-8");//增加设置忽略异常freemarkerCfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);//freemarkerCfg.setClassicCompatible(true);}try{//指定模板路径Templatetemplate=freemarkerCfg.getTemplate(templatePath,"UTF-8");template.setEncoding("UTF-8");StringhtmlPath=ServletActionContext.getServletContext().getRealPath("/")+targetHtmlName;FilehtmlFile=newFile(htmlPath);Writerout=newBufferedWriter(newOutputStreamWriter(newFileOutputStream(htmlFile),"UTF-8"));//处理模版template.process(sourcedata,out);out.flush();out.close();}catch(Exceptione){e.printStackTrace();}
看到的麻烦帮下吧!
解决方案
解决方案二:
在Web.xml中配置~Spring对编码的处理~百度可以查询代码~
解决方案三:
该回复于2011-04-15 09:45:43被版主删除
解决方案四:
看看是不是你eclipse项目中编码引起的
解决方案五:
引用1楼hpjianhua的回复:
在Web.xml中配置~Spring对编码的处理~百度可以查询代码~
是加入一个过滤器吗
解决方案六:
怎么没人来看看呢!另外在Spring1.2下Freemarker配置正常,但是换到Spring2.0下就出问题了。JAR包问题吧?