关于struts+hibernate+mysql5.0乱码问题

e3002 2006-04-22 09:12:03
我在jsp页面中来获得值显示正确,但当执行紧接着的hibernate时却不能正常执行,程序大概如下
String name=new String(s.getBytes("iso-8859-1"),"gbk"),
//执行上步时name显示正常
Query query=session.createQuery("from customers c where c.name='"+name+"'");
//执行到该步时不能正常执行,中文也不能正常显示,
不知道是数据库的事还是hibernate本身的问题,按理说hibernate本身应该每问题,郁闷好几天了,请各位大虾帮忙解决以下,感激不仅!
...全文
310 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hanhongmin 2007-02-14
  • 打赏
  • 举报
回复
解决中....顶了
evilzydar 2006-04-24
  • 打赏
  • 举报
回复
安装数据库时将数据库默认编码设置为utf8,程序编码想设什么就设什么了,这也是一种解决办法,看看其它朋友的答案.
whzhaha 2006-04-24
  • 打赏
  • 举报
回复
用了监听器,或改下struts也行,但千万不要转化两次哦 呵呵
keanfly 2006-04-24
  • 打赏
  • 举报
回复
Query query=session.createQuery("from UserManager where username=? and userpass=?");
query.setString(0,"xxx");
query.setString(1,"xxx");

没必要楼上那么复杂吧?看了人都晕。

通过参数动态绑定。这种方式具有很多优点,一、便于SQL语句的性能提升 二、代码具有更好的清晰度 三、防止了SQL字符串的攻击。
keanfly 2006-04-23
  • 打赏
  • 举报
回复
我也碰到同样问题,正在寻求解决方案中。。。。
Jineral 2006-04-23
  • 打赏
  • 举报
回复
to (阿真)
好人啊!
xiaoxiao130130 2006-04-23
  • 打赏
  • 举报
回复
以后在jsp文件中加入

<%@ page contentType="text/html; charset=GBK"%>
就可以了。
xiaoxiao130130 2006-04-23
  • 打赏
  • 举报
回复
第一步:
在你的项目web.xml文件中加入
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.pengzeng.struts.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
再加入这个类到你项目中
package com.pengzeng.struts;

import javax.servlet.*;
import java.io.IOException;

/**
* <p>Filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> - The character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the <code>ignore</code> initialization parameter. This parameter
* is required, so there is no default.</li>
* <li><strong>ignore</strong> - If set to "true", any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to "false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this
* parameter is set to "true".</li>
* </ul>
*
* <p>Although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>Accept-Language</code>
* and <code>User-Agent</code> headers, or a value stashed in the current
* user's session.</p>
*
* @author <a href="mailto:jwtronics@yahoo.com">John Wong</a>
*
* @version $Id: SetCharacterEncodingFilter.java,v 1.1 2002/04/10 13:59:27 johnwong Exp $
*/
public class SetCharacterEncodingFilter implements Filter {

// ----------------------------------------------------- Instance Variables


/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;


/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;


/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;


// --------------------------------------------------------- Public Methods


/**
* Take this filter out of service.
*/
public void destroy() {

this.encoding = null;
this.filterConfig = null;

}


/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {

// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}

// Pass control on to the next filter
chain.doFilter(request, response);

}


/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;

}


// ------------------------------------------------------ Protected Methods


/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {

return (this.encoding);

}

}//EOC



e3002 2006-04-22
  • 打赏
  • 举报
回复
up 高人指点以下
本书以网络留言板开发为主线将JavaWeb知识融会贯通,知识模块完整,通过静态网页篇、动态网站篇、系统框架篇、项目实战篇循序渐进地使学生全面掌握JavaWeb开发技术。静态网页篇主要讲解HTML、CSS、DIV、JavaScript等基础知识。动态网站篇深入讲解Servlet、JSP、JavaBean、JDBC数据库连接、数据库连接池等Java Web开发的核心技术。系统框架篇则是以架构设计的高度,讲解搭建网站的三大框架StrutsHibernate、Sprin9及标签库。项目实战篇详细阐述了通用论坛BBS、社交网站、DIY商品电子交易平台3个系统的开发细节,使读者真正掌握JavaWeb开发的精髓。 本书论述精准而深刻,程序实例丰富实用,不仅适用于数字媒体技术、计算机等相关专业的学生作为教材.廿可作为IT培训机构的培训教材,还可供广大JavaWeb程序员作为参考。 静态网页篇 第1章 开发环境安装及配置 1.1 JDK的安装及测试 1.2 Tomcat的安装及测试 1.3 MyEclipse的安装及配置 1.4 MySQL的安装及配置 第2章 HTML及其应用 2.1 网页设计流程 2.2 HTML的基础知识 2.3 HTML文档结构 2.4 HTML常用标记 第3章 CSS及其应用 3.1 CSS的基础知识 3.2 DIV基础知识 3.3 DIV + CSS实现个人网站首页 第4章 JavaScript及其应用 4.1 JavaScript 的基础知识 4.2 JavaScript 的基本语法 4.3 JavaScript 事件 4.4 JavaScript 常用对象 动态网站篇 第5章 Web程序运行原理 5.1 Web程序架构 5.2 Web服务器汇总 5.3 Web程序流程 5.4 Web应用程序开发 第6章 Servlet及其应用 6.1 Servlet 简介 6.2 Servlet 应用实例 6.3 HTML表单在Servlet中的应用 6.4 HTML表单验证 6.5 FCKeditor 框架应用 第7章 JDBC数据库连接 7.1 JDBC 概述 7.2 JDBC的工作原理 7.3 数据库的安装与使用 7.4 JDBC 编程 7.5 网络留言板V1.0 第8章数据库连接池技术 8.1 数据库连接池 8.2 网络留言板V2.0 8.3 Commons DbUtils 8.4 网络留言板V3.0 第9章 JSP及其应用 9.1 JSP基础知识 9.2 JSP 语法 9.3 JSP 范例 9.4 网络留言板V4.0 第10章 JavaBean及其应用 10.1 JavaBean 基础知识 10.2 JavaBean 在 JSP 中的调用 10.3 JavaBean 的作用域 10.4 JSP+JavaBean 的应用 10.5 网络留言板V5.0 系统框架篇 第11章 Stmts框架及其应用 11.1 Struts 基础知识 11.2 Struts 应用步骤 11.3 Struts 开发中的中文乱码问题 11.4 Action 数据获取与传递 11.5 Struts 表单验证 第12章 标签库及其应用 12.1 Struts标签库基础知识 12.2 Struts标签库应用实例 12.3 JSTL基础知识 12.4 EL表达式基础知识 12.5 JSTL核心标签库 12.6 网络留言板V6.0 第13章 Hibernate框架及其应用 13.1 Hibernate 基础知识 13.2 DataBase Explorer 透视图 13.3 Hibernate 应用实例 第14章 Spring框架及其应用 14.1 Spring 基础知识 14.2 Spring框架应用实例 14.3 Spring 和 Hibernate 组合开发实例 第15章 SSH整合应用 15.1 SSH整合理念 15.2 网络留言板V7.0 15.3 实例开发步骤 15.4 实例完善 项目实战篇 第16章 通用论坛BBS设计与实现 16.1 关键技术解析 16.2 系统功能分析 16.3 数据库设计与连接 16.4 各模块功能设计与实现 第17章 社交网站设计与实现 17.1 关键技术解析 17.2 系统功能分析 17.3 数据库表设计 17.4 各模块功能设计与实现 第18章 DIY商品电子交易平台设计与实现 18.1 关键技术解析 18.2 系统功能分析 18.3 数据库表设计 18.4 各模块功能设计与实现

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧