Struts + hibernate + MySQL Chinese garbled Solution

Source: Internet
Author: User
Tags mysql client

From: http://citszhanghj.spaces.live.com/blog/cns! D7f04e581faf210! 390. Entry

Struts + hibernate + MySQL Chinese garbled Solution

1. Modify the my. ini configuration file of the MySQL database,# Client Section
#----------------------------------------------------------------------
#
# The following options will be read by MYSQL client applications.
# Note that only client applications are shipped by MySQL are guaranteed
# To Read this section. If you want your own MYSQL client program
# Honor these values, you need to specify it as an option during
# MYSQL client library initialization.
#
[Client] Port = 3306default-character-set = GBK # Here, the default encoding is changed to GBK or UTF-8 # server section
#----------------------------------------------------------------------
# 2. When creating a data table# HOST: localhost
# Database: CITs
# Table: 'Article'
#
Create Table 'Article '(
'Article _ id' varchar (100) not null default '0 ',
'Article _ title' varchar (100) default null,
'Domain _ id' varchar (100) default null,
'Article _ text' varchar (100) default '',
'Good _ flg' tinyint (1) default null,
'Lock _ flg' tinyint (1) default null,
'Top _ flg' tinyint (1) default null,
'Read _ Times 'int (11) default null,
'Reply _ Times 'int (11) default null,
'Last _ reply' char (6) default null,
'Last _ reply_time 'datetime default null,
'Add _ user' varchar (30) default null,
'Add _ date' datetime default null,
'Update_user' varchar (30) default null,
'Update' datetime default null,
Primary Key ('Article _ id ')
) Engine = MyISAM default charset = GBK; GBK encoding is used here.   3. Use UTF-8 encoding in JSP pages<%
Object init = request. getattribute ("init ");
String domainid = (string) request. getattribute ("domainid ");
If (init = NULL) | (init. Equals ("") | (init. Equals ("null "))){
Response. sendredirect ("./listarticle. do? Domainid = "+ domainid );
} Else {
%>
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
<% @ Page import = "Java. util. hashmap" %>
<% @ Page import = "Java. util. List" %>
<% @ Page import = "Bo. *" %>
<%
List articlelist = (list) request. getattribute ("articlelist ");
// String domainid = (string) request. getattribute ("domainid ");
Int size = 0;
If (articlelist! = NULL ){
Size = articlelist. Size ();
}
%> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> post list </title>
</Head>
<Body>
<P align = "center"> <a href = "articlesave. jsp? Domainid = <% = domainid %> "> Publish a new topic </a> </P>
<Table width = "800" border = "1" align = "center" cellpadding = "0" cellspacing = "0" bordercolor = "# 6666ff">
<Tr>
<TD bgcolor = "# 6666ff"> <Div align = "center"> back/click </div> </TD>
<TD bgcolor = "# 6666ff"> <Div align = "center"> title </div> </TD>
<TD bgcolor = "# 6666ff"> <Div align = "center"> posting time </div> </TD>
<TD bgcolor = "# 6666ff"> <Div align = "center"> poster </div> </TD>
<TD bgcolor = "# 6666ff"> <Div align = "center"> last reply </div> </TD>
</Tr>
<% For (INT I = 0; I <size; I ++) {%>
<Tr>
<TD> <Div align = "center"> <% = (Article) articlelist. Get (I). getreplytimes () %> <br>
<% = (Article) articlelist. Get (I). getreadtimes () %> </div> </TD>
<TD> <Div align = "center"> <% = (Article) articlelist. Get (I). getarticletitle () %> </div> </TD>
<TD> <Div align = "center"> <% = (Article) articlelist. Get (I). getadddate () %> </div> </TD>
<TD> <Div align = "center"> <% = (Article) articlelist. Get (I). getadduser () %> </div> </TD>
<TD> <Div align = "center"> <% = (Article) articlelist. Get (I). getlastreply () %> </div> </TD>
</Tr>
<% }%>
</Table>
</Body>
</Html>
<% }%> 4, add a filter to the web application, do a fiter, unified use of UTF-8 encoding for all requests The source code of fiter is as follows:Package com. util. Common; import java. Io. ioexception; import javax. servlet. filter;
Import javax. servlet. filterchain;
Import javax. servlet. filterconfig;
Import javax. servlet. servletexception;
Import javax. servlet. servletrequest;
Import javax. servlet. servletresponse; public class encodingfilter implements filter {

Protected string encoding = NULL;
Protected filterconfig = NULL;
Protected Boolean ignore = true; Public void Init (filterconfig) throws servletexception {
// Todo auto-generated method stub
This. filterconfig = filterconfig;
This. Encoding = filterconfig. getinitparameter ("encoding ");
System. Out. println ("=== initencodingfilter ");
String value = filterconfig. getinitparameter ("Ignore ");
If (value = NULL ){
This. Ignore = true;
}
Else if (value. inclusignorecase ("true ")){
This. Ignore = true;
}
Else if (value. inclusignorecase ("yes ")){
This. Ignore = true;
}
Else {
This. Ignore = false;
}
} Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain) throws ioexception, servletexception {
// Todo auto-generated method stub
If (ignore | (request. getcharacterencoding () = NULL )){
String encoding = selectencoding (request );
If (encoding! = NULL ){
Request. setcharacterencoding (encoding );
}
}
Chain. dofilter (request, response);} public void destroy (){
// Todo auto-generated method stub
This. Encoding = NULL;
This. filterconfig = NULL ;}
Protected string selectencoding (servletrequest request ){
Return (this. Encoding );
}} 5. configure a filter in Web. XML to enable filtering. Add the following code to Web. xml:<Filter>
<Filter-Name> encoding </filter-Name>
<Filter-class> com. util. Common. encodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> UTF-8 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> encoding </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping> 6. Modify the property items for connecting data in hibernate. cfg. XML to the following code:<Property name = "connection. url">
JDBC: mysql: // localhost: 3306/CITS? Useunicode = true & amp; characterencoding = UTF-8
</Property> In this tutorial, go to struts1.2 + hibernate 3.1 + MySQL 5.0 Garbled characters can be solved through the windows platform. You can test other databases on your own.

 

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.