新手求教
为什么我的程序在连接MySQL的时候输入汉字是返回到错误界面,而英文就没问题,数据库是UTF-8 ,表也是,网上的方法全试完了 小弟用的是struts1.2+Hibenate3.2+MySQL5.0
代码如下:
test.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for TestForm form</title>
</head>
<body>
<html:form action="/test" method="post">
name : <html:text property="name"/><html:errors property="name"/><br/>
password : <html:password property="password"/><html:errors property="password"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
控制层:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.os.action;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import servlet.AbstractTest;
import servlet.Test;
import com.os.form.TestForm;
import Dao.Testdao;
/**
* MyEclipse Struts
* Creation date: 08-11-2010
*
* XDoclet definition:
* @struts.action path="/test" name="testForm" input="/form/test.jsp" scope="request" validate="true"
* @struts.action-forward name="lose" path="/form/lose.jsp"
* @struts.action-forward name="success" path="/form/success.jsp"
*/
public class TestAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws UnsupportedEncodingException
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException
{
TestForm testForm = (TestForm) form;
String name = new String(testForm.getName().getBytes("iso8859-1"),"utf-8");
String password=new String(testForm.getPassword().getBytes("iso8859-1"),"utf-8");
Testdao test = new Test();
ActionForward forword = null;
try{
AbstractTest admin= test.TestLogin(name,password);
if(admin!=null)
{
forword=mapping.findForward("success");
}
else
{
forword=mapping.findForward("lose");
}
catch(Exception e)
{
}
return forword;
}
}
业务层:
import org.hibernate.cfg.Configuration;
import org.hibernate.*;
import Dao.Testdao;
import SessionFactory.HibernateSessionFactory;
import SessionFactory.TestSessionFactory;
/**
* Test entity.
*
* @author MyEclipse Persistence Tools
*/
public class Test extends AbstractTest implements java.io.Serializable, Testdao{
// Constructors
/** default constructor */
public Test() {
}
/** minimal constructor */
public Test(Integer id) {
super(id);
}
/** full constructor */
public Test(Integer id, String name, String password) {
super(id, name, password);
}
public AbstractTest TestLogin(String name, String password) {
// TODO Auto-generated method stub
Session session=HibernateSessionFactory.getSession();
Transaction tx=null;
AbstractTest admin=null;
try{
tx=session.beginTransaction();
String hql="select a from AbstractTest as a where a.name=:name and a.password=:password";
Query query=session.createQuery(hql);
query.setString("name", name);
query.setString("password", password);
query.setMaxResults(1);
admin=(AbstractTest)query.uniqueResult();
tx.commit();
}
catch(Exception e)
{
if(tx!=null)
{tx.rollback();}
e.printStackTrace();
}
finally
{
TestSessionFactory.closeSession();
}
return admin;
}
}
求教啊 郁闷坏了 整了两天了
qlraixwj
2010/08/15 20:09- it技术
- 互联网问答
- IT行业问题
- 编程语言问答
- 计算机技术
- 点赞
- 收藏
- 回答
0个回复
