配置SHOP++编码格式转换过滤器

配置SHOP++编码格式转换过滤器


正常情况下若SHOP++出现乱码问题,将Tomcat的编码格式统一修改为UTF-8格式即可解决,但在实际情况下并不是所有用户都有权限自行修改Tomcat编码格式(如虚拟主机环境)。SHOP++商业版加入了encodingFilter、encodingConvertFilter两个拦截器,轻松配置一下编码格式即可解决乱码问题,非常方便。
代码如下:
方法一:在tomcat安装目录下C:\apache-tomcat-6.0.29\conf修改server.xml
<Connector port="8080" protocol="HTTP/1.1" onnectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" />






<!-- 编码处理过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
方法二:在web.xml中配置
<!-- 编码格式转换过滤器 -->
<filter>
<filter-name>encodingConvertFilter</filter-name>
<filter-class>net.shopxx.filter.EncodingConvertFilter</filter-class>
<init-param>
<param-name>fromEncoding</param-name>
<param-value>ISO-8859-1</param-value>
</init-param>
<init-param>
<param-name>toEncoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingConvertFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
其中:
<param-value>ISO-8859-1</param-value>
为当前Tomcat的编码格式
mapping用于开启encodingConvertFilter编码转换过滤器


我们在web应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,如何输出一个某种编码的字符串?
答:Public String translate (String str)
{
String tempStr = "";
try {
tempStr = new String(str.getBytes("ISO-8859-1"), "GBK");
tempStr = tempStr.trim();
} catch (Exception e) {
System.err.println(e.getMessage());
} return tempStr;
}

你可能感兴趣的:(编码格式)