AppFuse使用“mvn native2ascii:native2ascii”进行文件编码格式的转换。但是转换后,界面显示仍然是乱码。打开ApplicationResources_zh_CN.properties文件,里面是中文貌似已经转换为ascii。但是仔细看,ApplicationResources_zh_CN.properties文件仍然是GB18030。
在网上找了找,如果在%PROJECT_HOME%运行:
native2ascii -encoding UTF-8 .\src\main\resources\ApplicationResources_zh_CN.properties .\src\main\webapp\WEB-INF\classes\ApplicationResources_zh_CN.properties
则显示正常。
AppFuse的pom.xml里明明制定了UTF-8,为什么文件就变成了GB18030呢?
1 <plugin>
2 <groupId>org.codehaus.mojo</groupId>
3 <artifactId>native2ascii-maven-plugin</artifactId>
4 <version>1.0-alpha-1</version>
5 <configuration>
6 <dest>target/classes</dest>
7 <src>src/main/resources</src>
8 </configuration>
9 <executions>
10 <execution>
11 <id>native2ascii-utf8</id>
12 <goals>
13 <goal>native2ascii</goal>
14 </goals>
15 <configuration>
16 <encoding>UTF8</encoding>
17 <includes>
18 ApplicationResources_ko.properties,
19 ApplicationResources_no.properties,
20 ApplicationResources_tr.properties,
21 ApplicationResources_zh*.properties
22 </includes>
23 </configuration>
24 </execution>
25 <execution>
26 <id>native2ascii-8859_1</id>
27 <goals>
28 <goal>native2ascii</goal>
29 </goals>
30 <configuration>
31 <encoding>8859_1</encoding>
32 <includes>
33 ApplicationResources_de.properties,
34 ApplicationResources_fr.properties,
35 ApplicationResources_nl.properties,
36 ApplicationResources_pt*.properties
37 </includes>
38 </configuration>
39 </execution>
40 </executions>
41 </plugin> 查看JDK的native2ascii文档:
-encoding encoding_name
Specify the encoding name which is used by the conversion procedure. The default encoding is taken from System property file.encoding. The encoding_name string must be taken from the first column of the table of supported encodings in the Supported Encodings document.
原来执行native2ascii时,默认取系统的file.encoding做为编码格式。试着输出file.encoding,果然是GB18030。难道是AppFuse执行“native2ascii:native2ascii”时,没有制定编码?
反编译repository\org\codehaus\mojo\native2ascii-maven-plugin\1.0-alpha-1\native2ascii-maven-plugin-1.0-alpha-1.jar!\org\codehaus\mojo\native2ascii\Native2AsciiMojo.class
1 protected void executeAnt()
2 {
3 Project antProject = new Project();
4 antProject.setName("native2ascii");
5 Native2Ascii antTask = new Native2Ascii();
6 antTask.setProject(antProject);
7 antTask.setSrc(src);
8 antTask.setDest(dest);
9 antTask.setEncoding(encoding);
10 antTask.setExt(ext);
11 antTask.setExcludes(excludes);
12 antTask.setIncludes(includes);
13 antTask.execute();
14 } 代码也很简单,调用Ant的API实现转换。估计Ant也不会出现什么问题,为什么会使用系统默认的编码呢?试着输出encoding,发现encoding居然为null!会不会是从pom.xml里没有取到encoding的设置呢?
修改pom.xml,尝试增加encoding节点
1 <groupId>org.codehaus.mojo</groupId>
2 <artifactId>native2ascii-maven-plugin</artifactId>
3 <version>1.0-alpha-1</version>
4 <configuration>
5 <dest>target/classes</dest>
6 <src>src/main/resources</src>
7 <encoding>UTF-8</encoding>
8 </configuration> 再运行“mvn native2ascii:native2ascii”,发现encoding有值了,但是生成的文件编码还是GB18030,不过运行服务,中文乱码没有了。看来和文件编码没有太大关系,应该是native2ascii转换时采用默认的GB18030编码转换导致的。
另外pom.xml只配置了ApplicationResources_zh*.properties的字符转换,AppFuse还有displaytag_zh.properties也需要转换,我们可以把pom.xml里ApplicationResources_zh*.properties都换成*_zh*.properties就可以了。
1 <configuration>
2 <encoding>UTF8</encoding>
3 <includes>
4 ApplicationResources_ko.properties,
5 ApplicationResources_no.properties,
6 ApplicationResources_tr.properties,
7 *_zh*.properties
8 </includes>
9 </configuration>
10
11
12
13 <resource>
14 <directory>src/main/resources</directory>
15 <excludes>
16 <exclude>ApplicationResources_de.properties</exclude>
17 <exclude>ApplicationResources_fr.properties</exclude>
18 <exclude>ApplicationResources_ko.properties</exclude>
19 <exclude>ApplicationResources_nl.properties</exclude>
20 <exclude>ApplicationResources_no.properties</exclude>
21 <exclude>ApplicationResources_pt*.properties</exclude>
22 <exclude>ApplicationResources_tr.properties</exclude>
23 <exclude>*_zh*.properties</exclude>
24 <exclude>applicationContext-resources.xml</exclude>
25 <exclude>struts.xml</exclude>
26 </excludes>
27 <filtering>true</filtering>
28 </resource> 文章来源:
http://heyday.blogcn.com/diary,15137426.shtml