I solve the Chinese garbled method in spring

Source: Internet
Author: User
Solve | chinese | garbled chinese

Software Environment: Jdk1.4.2_09+eclipse3.1+ms SQL server200+sp3+jtds1.0.2+struts1.1+hibernate3.0.5+spring1.2.4.

As just beginning to learn this framework, so many things are not particularly clear, before the JB Environment also did not encounter garbled problems. This time tried a lot of methods can not, so decided to add a fileter, web.xml part of the contents are as follows:

<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>
Org.springframework.web.filter.characterencodingfilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<!--type--> to be filtered
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

By adding breakpoint debugging to the action, it is found that the jump using the hyperlink can use filter, but if the request with a. Do suffix is not, I have modified the web.xml:

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app>
<!--Spring applicationcontext-->
<servlet>
<servlet-name>context</servlet-name>
<servlet-class> Org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>
Org.springframework.web.filter.characterencodingfilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<!--type--> to be filtered
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>main.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-template.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
</web-app>

Mainly add a filter content here! Other, to prevent in case, on the page (JSP) also added something:

<%@ page contenttype= "text/html; CHARSET=GBK "pageencoding=" GBK "%>

<meta http-equiv= "Content-type" content= "text/html; CHARSET=GBK ">

Oh, can be said to be armed to the teeth, to start debugging: This time in the debug, the display from the page of the value is finally not garbled, saved in the database, is not garbled. The problem is partly settled now. Because I have not tested on the page which is not to write, there is the page back to the Chinese characters is whether there will be problems, but here first to record their own income, if there is an expert on this matter, even if I am ignorant of it, hehe.

Also give some of my Hibernate.cfg.xml:

<session-factory>
<property name= "Hibernate.connection.url" >jdbc:jtds:sqlserver://192.168.0.3:1433;databasename=homeconsume ;charset=gbk</property>
<property name= "Hibernate.cglib.use_reflection_optimizer" >true</property>
<property name= "Hibernate.connection.password" >sju</property>
<property name= "Hibernate.connection.username" >sa</property>
<property name= "Hibernate.connection.driver_class" >net.sourceforge.jtds.jdbc.Driver</property>
<property name= "Hibernate.dialect" >org.hibernate.dialect.SQLServerDialect</property>
<mapping resource= "Net/magicyang/homeconsume/pojo/test.hbm.xml"/>
<mapping resource= "Net/magicyang/homeconsume/pojo/consumeinfo.hbm.xml"/>
<mapping resource= "Net/magicyang/homeconsume/pojo/consumetype.hbm.xml"/>
</session-factory>

I would also like to welcome you to discuss the relevant issues with me, thank you



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.