我在开发struts+hibernate+mysql时,设置了字符转换为中文,但数据显示还是乱码,应该如何设置呢?
部分主要代码如下:
hibernate.cfg.xml
....................
<property name="connection.url">
jdbc:mysql://localhost:3306/goby?useUnicode=true&characterEncoding=GBK
</property>
............................... ShowAction.java
....................
ShowService ss=new ShowService();
Text text=ss.getText(request.getParameter("id"));
request.setAttribute("text", text);
return mapping.findForward("show");
..................ShowService.java
.................
public Text getText(long id) {
s = SessionFactory.currentSession();
Transaction tx = s.beginTransaction();
Long theId = new Long(id);
Text text = (Text) s.load(Text.class, theId);
tx.commit();
return text;
}................web.xml
..........
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>system.util.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>.........
*这个system.util.EncodingFilter代码我就不在此显示啦!因为篇幅太长啦!
show.jsp
.................................
<bean:write name="text" property="title" filter="false"/>
<bean:write name="text" property="context" filter="false"/>
.................................
这里我用了struts标签来显示,如果我想把ShowAction.java中的text数据转换为ISO8859-1输出,那应该如何做呢?不是在show.jsp里,是否可以在ShowAction.java中做了转换,然后输出在show.jsp.请指点.
部分主要代码如下:
hibernate.cfg.xml
....................
<property name="connection.url">
jdbc:mysql://localhost:3306/goby?useUnicode=true&characterEncoding=GBK
</property>
............................... ShowAction.java
....................
ShowService ss=new ShowService();
Text text=ss.getText(request.getParameter("id"));
request.setAttribute("text", text);
return mapping.findForward("show");
..................ShowService.java
.................
public Text getText(long id) {
s = SessionFactory.currentSession();
Transaction tx = s.beginTransaction();
Long theId = new Long(id);
Text text = (Text) s.load(Text.class, theId);
tx.commit();
return text;
}................web.xml
..........
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>system.util.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>.........
*这个system.util.EncodingFilter代码我就不在此显示啦!因为篇幅太长啦!
show.jsp
.................................
<bean:write name="text" property="title" filter="false"/>
<bean:write name="text" property="context" filter="false"/>
.................................
这里我用了struts标签来显示,如果我想把ShowAction.java中的text数据转换为ISO8859-1输出,那应该如何做呢?不是在show.jsp里,是否可以在ShowAction.java中做了转换,然后输出在show.jsp.请指点.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货