插入进去是好的,用数据库select也正常 查询页面时如果查询的是中文名字则会出现乱码(乱码直接出现在text中),如果查询的名字是英文,则显示正常(显示正常的意思是其他中文的信息也会显示出来),最 后发现只要在查询框(text)不输入中文,则不会显示乱码,可能问题就出在这了,但搞了很久都没搞定,有点失去信心了请大家帮帮忙吧! 说明一下,简单的实现的是按人名查询人的信息!
解决方案 »
- struts2中如何用标签计算两个时间的差,例如还余下多少天!
- spring quartz定时问题
- Hibernate如何保存多条记录了?
- 为什么通过eclips启动Tomcat只能看到Tomcat的开始页面?
- JPA哪个版本开始支持Criteria API,相关的hibernate实现包要什么版本?
- 求SQL Server 7 Driver for JDBC
- 关于hibernate的查询错误
- 请问如何在jcreator下执行带参数的程序?
- C语言可以用Turbo C编译, Java用什么东西编译啊,我是个菜鸟,那个知道告诉我,我好去下载!
- ejb返回ResultSet,做法科学吗
- 我想用java实现数据库到excel的数据存储?
- 请教一个项目开发的具体问题
<%@ page language="java" import="java.util.*" pageEncoding="gb2312" contentType="text/html; charset=gb2312"%>
用Serverlet的话,在Serverlet的doPost或者doGet方法中加
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
当然,你Jsp页面的pageEncoding="UTF-8"
在hibernate.cfg.xml中的<session-factory>的配置中加一行<property name="query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
或者换3.0以上版本~~试试吧
import util.*;
/**
* MyEclipse Struts
* Creation date: 10-08-2007
*
* XDoclet definition:
* @struts.action path="/researcher" name="researcherForm" input="/researcher_insert.jsp" scope="request"
*/
public class ResearcherAction extends BaseAction {
private static final String CONTENT_TYPE = "text/html; charset=GB2312";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
ResearcherForm researcherForm = (ResearcherForm) form;
request.setCharacterEncoding("gb2312");
HttpSession session = request.getSession();
String = new String();
String name = researcherForm.getName();
if(name!=null ) name = new String(name.getBytes("iso8859_1"), "gb2312");
if (name == null) {
name = "";
}
String sex = researcherForm.getSex();
if(sex!=null ) sex = new String(sex.getBytes("iso8859_1"), "gb2312");
if (sex == null) {
sex = "";
}
String title = researcherForm.getTitle();
if(title!=null ) title = new String(title.getBytes("iso8859_1"), "gb2312");
if (title == null) {
title = "";
}
String career = researcherForm.getCareer();
if(career!=null ) career = new String(career.getBytes("iso8859_1"), "gb2312");
if (career == null) {
career = "";
}
int rid = Integer.parseInt(researcherForm.getRid());
String[] check = researcherForm.getCheck(); response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter(); String command = researcherForm.getCommand();
if(command!=null) {
command = command.trim();
command = new String(command.getBytes("iso8859_1"), "gb2312");
}
System.out.println("要执行的操作是:" + command); boolean bool = false;
Researcher researcher = new Researcher();
researcher.setRid(rid);
researcher.setName(name);
researcher.setSex(sex);
researcher.setTitle(title);
researcher.setCareer(career);
IResearcherDAO researcherDAO = getResearcherService().getResearcherDAO();
String condition = new String();
PageBean pageBean = new PageBean(); //添加信息 if (command.equals("insert")) { bool = researcherDAO.saveResearcher(researcher); if (bool) {
out.println("<script language=javascript>alert('添加成功!')");
out.println("location.href='researcher/query.jsp'</script>");
out.close(); } else {
out.println("<script language=javascript>alert('错误!添加未成功!')");
out.println("location.href='researcher/query.jsp'</script>");
out.close(); }
} //查询
if (command.equals("query")) {
System.out.println("Here is ResearcherAction"); condition = researcherForm.getCondition();
//if(condition!=null) condition = new String(condition.getBytes("iso8859_1"),"gb2312");
System.out.println("The query condition is: "+condition);
if(condition==null) condition="";
session.setAttribute("condition", condition);
int pageNum = 5;
int currPage=0;
pageBean.setPageNum(pageNum);
pageBean.setCurrPage(currPage);
pageBean.setCondition(condition); ArrayList ls = (ArrayList)researcherDAO.getResearcher(pageBean); session.setAttribute("queryResult", ls);
= "queryPage"; }
//更新信息 if (command.equals("update")) { bool = researcherDAO.updateResearcher(researcher); if (bool) {
out.println("<script language=javascript>alert('修改成功!')");
//out.println("window.close()</script>");
out.println("location.href='researcher/query.jsp'</script>");
out.close(); } else {
out.println("<script language=javascript>alert('错误!修改未成功!')");
//out.println("window.close()</script>");
out.println("location.href='researcher/query.jsp'</script>");
out.close(); }
}
//删除信息 if (command.equals("删除")) {
if (check != null) {
int num = check.length;
for (int i = 0; i <= num - 1; i++) {
System.out.println("check[i]=" + check[i]);
bool = researcherDAO.delResearcher(check[i]);
if (bool) {
out.println("<script language=javascript>alert('删除成功!')");
out.println("location.href='researcher/query.jsp'</script>");
out.close();
}
else {
out.println("<script language=javascript>alert('删除未成功!')");
out.println("location.href='researcher/query.jsp'</script>");
out.close();
} }
}
}
return mapping.findForward();
}
}
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import="java.util.*, beans.*, dao.*, util.*" %>
<%request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
%>
<html:html>
<LINK href="../css/style.css" rel=stylesheet type=text/css>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>
研究人员浏览与查询
</title>
</head>
<body bgcolor="#ffffff">
<br>
<br>
<br>
<h1 align="center">研究人员浏览与查询 </h1>
<br><html:form action="researcher" method="post" enctype="gb2312">
<html:hidden value="query" property="command"/>
<div align="right">
研究人员搜索 <html:text property="condition" />
<html:submit value="查询" property="submit"/>
</div>
</html:form> <br><br>
<html:form action="researcher" method="post" >
<table width=70% border=0 cellpadding="3" cellspacing="1" align="center">
<tr bgcolor=#8899cc>
<td> </td>
<td> 编号 </td>
<td> 姓名 </td>
<td>性别</td>
<td>职称</td>
<td> 简介 </td>
</tr><%
request.setCharacterEncoding("gb2312");
PageBean pageBean = new PageBean(); //String condition = request.getParameter("condition");
String condition = (String)session.getAttribute("condition");
session.removeAttribute("condition");
if(condition!=null) condition = new String(condition.getBytes("iso8859_1"),"gb2312");
System.out.println("The query condition is: "+condition);
if(condition==null) condition=""; int pageNum = 5;
int currPage;
String url = "researcher/query.jsp";
if (request.getParameter("pages")==null) {
currPage =0;
}
else {
currPage = new Integer(request.getParameter("pages")).intValue();
} System.out.println("Here is currPage!"); pageBean.setPageNum(pageNum);
pageBean.setCurrPage(currPage);
pageBean.setCondition(condition);
session.setAttribute("pageBean",pageBean);
ArrayList list = (ArrayList)session.getAttribute("queryResult");
// session.removeAttribute("queryResult");
int start = currPage * pageNum; //当前页的开始记录
int end = start + pageNum; //当前页的结束记录
if(list!=null && list.size()!=0) {
for(int i=start;i<end; i++ ) {
if(i<list.size()) {
Researcher info = (Researcher)list.get(i);
session.setAttribute(String.valueOf(info.getRid()),info);
%>
<tr bgcolor=#ffffff>
<td> <html:checkbox property="check" value="<%=String.valueOf(info.getRid())%>" /> </td>
<td><%=info.getRid()%> </td>
<td> <%=info.getName()%></td>
<td><%=info.getSex()%></td>
<td><%=info.getTitle()%></td>
<td><%=info.getCareer()%></td><td> <a href="researcher/update.jsp?rid=<%=String.valueOf(info.getRid())%>" target="_self">修改 </a> </td>
</tr>
<% } }}%><tr>
<td> </td>
<td> </td>
<td> </td><td> </td>
</tr></table>
<p align=center>
<%=Pagination.getPaginationScript(pageBean.getCurrPage(),pageBean.getPageNum(),PageBean.getRecordNum(),pageBean.getCondition(),url)%>
</p>
<br><br>
<p align=center> <html:submit value="删除" property="command"/>
</p>
<div > </div>
</html:form>
</body>
</html:html>
自带的spring-hibernate3.jar