0
点赞
收藏
分享

微信扫一扫

VeloCity velocity 乱码问题 解决总结收藏

input.encoding   =   gbk   
  output.encoding=gbk   
  default.contenttype=text/html;charset=gbk




但是还是乱码。


在vm模版中中文怎么正确显示出来啊,如何解决这个问题呀?


解决办法:


这里这样写的话的确存在以上遇到的情况。但是可以通过另外一种方式来解决。


这里


#set ($nn="中文")


可以通过把变量值在代码里面set,这样便可解决


模板代码可以就可以这样写了


hello ${nn} world!!



代码:


publicstaticvoid main( String args[] ) throws Throwable{      
         VelocityEngine ve = new VelocityEngine();
         Properties p = new Properties();        
         p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "E:/opensource/workspace/source/volicity/com/vm");
         ve.init(p);
         Template t = ve.getTemplate("temp.vm");       
         VelocityContext context = new VelocityContext();          
         context.put("nn","我靠,中文呢?");
         StringWriter writer = new StringWriter();         
         t.merge(context,writer);

         FileOutputStream out = new FileOutputStream(new File("E:/T.java"));
         out.write(writer.toString().getBytes());   
         System.out.print(writer.toString());
         out.close();            
         }


但是问题又出现了


输出结果是 hello 我靠,中文呢? world!!


但是输出到文件里面的却是


hello ????? world!!


到这里我们该明白一点了。这是写文件的输出流那里编码出了问题。


我们可以通过设定output.encoding=gbk 来解决。



问题二:


在我们用Velocity生成一些文本文件,但是这些文本文件里面存放的是xml格式的字符串



例如temp.vm的代码是


<?xml version="1.0" encoding="UTF-8"?>

 <wpc:description>${chinese}</wpc:description>


我们的main函数是



代码:


publicstaticvoid main( String args[] ) throws Throwable{      
         VelocityEngine ve = new VelocityEngine();
         Properties p = new Properties();        
         p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "E:/opensource/workspace/source/volicity/com/vm");
         ve.init(p);
         Template t = ve.getTemplate("temp.vm");       
         VelocityContext context = new VelocityContext();          
         context.put("chinease","我靠,中文呢?");
         StringWriter writer = new StringWriter();        
         t.merge(context,writer);

         FileOutputStream out = new FileOutputStream(new File("E:/T.java"));
         out.write(writer.toString().getBytes());   
         System.out.print(writer.toString());
         out.close();            
         }


但是问题又出现了



输出结果是


<?xml version="1.0" encoding="UTF-8"?>

 <wpc:description>??????</wpc:description>


这里我们把属性文件


input.encoding = gbk


output.encoding=gbk 设置为这样,但是呢?结果如故,还是没有解决。


无论input.encoding = UTF-8 output.encoding=UTF-8 还是什么别的,还是乱码。


这个问题是网上很多人遇到的,在整合spring或者什么别的开源软件的时候,遇到xml格式的文件的时候遇到这样的乱码问题。


但是我们把模板文件改为这样


<wpc:description>${chinese}</wpc:description>


去掉了头一行


<?xml version="1.0" encoding="UTF-8"?>


输出结果正常了。呵呵,很奇妙噢。大家是不是发现了什么呢?这里的这行


<?xml version="1.0" encoding="UTF-8"?>


是罪魁祸首。


在控制台输出的是中文,因为模板有了这行,文件代码却乱了。


呵呵,到这里,我们似乎找到了解决办法。问题出在我们写文件的流上面。


我们现在用另外的PrintStream流来写文件吧。这样输出的编码格式可以搞定啦。


代码如下:


publicstaticvoid main( String args[] ) throws Throwable{      
         VelocityEngine ve = new VelocityEngine();
         Properties p = new Properties();        
         p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "E:/opensource/workspace/source/volicity/com/vm");
         ve.init(p);
         Template t = ve.getTemplate("temp.vm");       
         VelocityContext context = new VelocityContext();          
         context.put("TableBeans","我靠,中文呢?");
         StringWriter writer = new StringWriter();         
         t.merge(context,writer);      
         FileOutputStream fos = new FileOutputStream(new File("E:/x.java"));
         PrintStream ps = new PrintStream(fos, true, "UTF-8");//这里我们就可以设置编码了
         ps.print(writer.toString());
         ps.flush();
         ps.close();
         fos.close();            
         }


这下我们看一下输出的x.java文件,呵呵,OK了,原来病因就是那个第一行


<?xml version="1.0" encoding="UTF-8"?>


这里其实就是强制了文件的类型了。



告诉大家一个设置代码属性的捷径,可以省去velocity.properties文件。呵呵,这样就简单好用了。


Properties p = new Properties();         
         p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "E:/opensource/workspace/source/volicity/com/vm");
         p.setProperty(Velocity.INPUT_ENCODING, "GBK");
         p.setProperty(Velocity.OUTPUT_ENCODING, "GBK");
         ve.init(p);


这样设置属性就更方便啦。


举报

相关推荐

0 条评论