奇怪的乱码问题 居然参数传递时出现乱码 请高手解答

zhoutao198712 2008-11-11 03:31:16
今天练习一个添加学生信息的,出现了奇怪的乱码。使用的是Struts2+hibernate3
首先贴出其代码:
AddStudent.java

public class AddStudent extends ActionSupport{

private static final long serialVersionUID = 1178440535366734465L;
private String name;
private String password;
private String sex;
private String email;
private String jiguan;
private String phone;
private String tel;
private String id;
private String department;
private StudentDAO stuDao;

private StudentDAO getStudentDAO(){
return DAOFactory.getInstance().createStudentDAO();
}
public String execute() throws Exception{
stuDao = this.getStudentDAO();
Student stu = new Student();
try{
stu.setId(new String(getId()));
stu.setName(new String(getName().getBytes("ISO-8859-1"),"GB2312"));
stu.setPassword(new String(getPassword().getBytes("ISO-8859-1"),"UTF-8"));
stu.setDepartment(new String(getDepartment().getBytes("ISO-8859-1"),"utf-8"));
stu.setSex(new String(getSex().getBytes("ISO-8859-1"),"utf-8"));
stu.setJiguan(new String(getJiguan().getBytes("ISO-8859-1"),"utf-8"));
stu.setEmail(getEmail());
stu.setPhone(getPhone());
stu.setTel(getTel());
System.out.println(getName() +getPhone()+getDepartment());
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}
boolean result=stuDao.saveStudent(stu);
if(result){
return SUCCESS;
}else{
return INPUT;
}
}
//......setter getter
}


StudentDAOImp.java

public class StudentDAOImp implements StudentDAO {
private static Log log = LogFactory.getLog(StudentDAOImp.class);

//保存学生信息
public boolean saveStudent(Student stu){
System.out.println(stu.getName() +stu.getPhone());
try{
Session s= HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
s.save(stu);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
return true;
}catch(Exception e){
log.fatal(e);
}
return false;
}
}



当在页面中输入一些数据之后,在控制台输出如下数据:
某某23484461信电学院 //这是AddStudent类中System.out.println(); 输出的
??23484461 //这是StudentDAOImp类中的saveStudent方法中的System.out.输出的
这样存入数据库中的当然也是乱码。


如果在StudentDAOImp中直接实例一个类,也可以正常输出和存入数据库中而且没有乱码。


请高手解答这个是问什么啊?

<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/stuman</property>
<property name="connection.username">root</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<property name="connection.password">123</property>
<property name="connection.pool_size">5</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- <property name="hbm2ddl.auto">create</property> -->
<property name="show_sql">true</property>
<mapping resource="Admin.hbm.xml" />
<mapping resource="Teacher.hbm.xml"/>
<mapping resource="Student.hbm.xml"/>
<mapping resource="Course.hbm.xml"/>
<mapping resource="Classes.hbm.xml"/>
<mapping resource="Enrol.hbm.xml"/>
</session-factory>
</hibernate-configuration>

...全文
192 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhoutao198712 2008-11-11
  • 打赏
  • 举报
回复
设置一个filter能够解决问题,我想应该是要求统一的字符编码集。
zhoutao198712 2008-11-11
  • 打赏
  • 举报
回复
在发现新的问题:如果在AddStudent中直接实例一个Student也没乱码:
public String execute() throws Exception{
stuDao = this.getStudentDAO();
Student student =new Student();
student.setName("某某");
student.setId("12345667");
student.setPassword("123");
boolean result=stuDao.saveStudent(student);
if(result){
return SUCCESS;
}else{
return INPUT;
}
}


Why?头大了。
sunday_wu 2008-11-11
  • 打赏
  • 举报
回复
编码问题
zhoutao198712 2008-11-11
  • 打赏
  • 举报
回复
我的编码格式,都用的UTF-8。一堆stu.set()方法是在给Student的属性赋值,而Struts只有set AddStudent类中的属性啊。
问题的关键是,在AddStudent类中没有乱码,而在StudenDAOImp中全乱码了啊?
意思就是:从web中获取的获取到的数据没有乱码,而在AddStudent这个Action中调用了一个StudentDAOImp类的saveStudent()方法,在这个saveStudent()方法中输出的数据是乱码。
关键就是为什么调用一个方法反而出现了乱码。
Jerry-He 2008-11-11
  • 打赏
  • 举报
回复
注意你存入数据库时数据的编码格式要一致,比如都用“UTF-8”,同时也要注意数据库的编码格式
beckhamcat1 2008-11-11
  • 打赏
  • 举报
回复
呵呵 ,ls说的对。
imasmallbird 2008-11-11
  • 打赏
  • 举报
回复
若前台是post提交,且所有编码集设置全是一致的应该是没有问题的
若是get提交,你取一下URI看一下参数中是不是乱码??
______________________

我是说,你为什么要定义一堆String,
Struts2会把你前台的值直接set进去呀

zhoutao198712 2008-11-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 danny2 的回复:]
设个编码过滤器就可以了
[/Quote]

我不是已经编码了吗?
zhoutao198712 2008-11-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 imasmallbird 的回复:]
为什么别的全是“UTF-8”
而名字是“GB2312”

_______________

怎么不直接定义一个Student类的一个属性..
[/Quote]
我开始都用的UTF-8,后来想看看GB2312能不行就改了一下了。
我怎么可能自己实例化一个Student呢,这个是需要从客户端传入的啊?
imasmallbird 2008-11-11
  • 打赏
  • 举报
回复
为什么别的全是“UTF-8”
而名字是“GB2312”

_______________

怎么不直接定义一个Student类的一个属性..
danny2 2008-11-11
  • 打赏
  • 举报
回复
设个编码过滤器就可以了
zhoutao198712 2008-11-11
  • 打赏
  • 举报
回复
自己顶!

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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