为此,我首先来贴一段有关用struts+hibernate+myeclipse的代码,这是我自己做来为初学者参考的,有问题可以直接回帖,有指导,很欢迎哦,希望路过的也来顶一下
解决方案 »
- html改为jsp后session就不丢失了!哪位帮忙看看啊
- 那位大侠能帮忙给看看关于两表联查的问题(能远程帮忙调试一下急急)
- dwr验证失败
- 求救啊
- 最近tomcat 启动后,过段时间就出现下面异常,修改了连接数也不行,希望大虾们指点下!谢谢了
- 多线程并发 怎么 控制单一现成
- HQL 的查询语句怎么写啊?
- 我是菜鸟,想参加编程培训,不知是否有效果?请各位给点意见!
- 数据库连接池
- 请大家讨论一下tiles使用中的一些原则,参与讨论者有分。包括tiles中的布局定义、jsp的命名、书写规范,文件结构的规则,可以尽情讨论,
- ACTIONFORM和DTO替换的问题,帮帮忙,谢谢!!!!
- fileupload组件上传文件-出现错误Processing of multipart/form-data request failed. Stream ended unexpectedly
public ActionForward split(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws HibernateException {
SplitForm splitForm = (SplitForm) form;
Student ulist=null;
int page;
Transaction tx;
if(splitForm.getName()!=null){
page=1;
ulist=new Student();
ulist.setName(splitForm.getName());
ulist.setScore(new Float(splitForm.getScore()));
}
else{
page=Integer.parseInt((String)request.getParameter("page"));
if(page==0)
page=1;
}
QueryInfo qi=new QueryInfo();
qi.pageSize=3;
qi.currentPage=page;
Session session=HibernateUtil.currentSession();
tx=session.beginTransaction();
DbOperation db=new DbOperation();
List list=db.getStudent(session,qi);
DataForm dataform=new DataForm();
dataform.setList(list);
dataform.setNextPage(db.nextPage);
dataform.setPreviousPage(db.previousPage);
dataform.setPageCount(qi.pageCount);
dataform.setNext(qi.currentPage);
dataform.setPrevious(qi.currentPage);
request.setAttribute("form",dataform);
tx.commit();
HibernateUtil.closeSession();
return mapping.findForward("splitpage");
}
public boolean nextPage,previousPage;
public Student getStudent(String id) throws HibernateException{
Session session=HibernateUtil.currentSession();
Student student=getStudent(session,id);
session.close();
return student;
}
public Student getStudent(Session session,String id)throws HibernateException{
Transaction tx=null;
Student student=null;
try{
System.out.println("success in getStudent method");
tx=session.beginTransaction();
Query query=session.createQuery("FROM "+Student.class.getName()+"where id=:id");
query.setParameter("id",id);
List list=query.list();
if(!list.isEmpty()){
student=(Student)list.get(0);
}
tx.commit();
}catch(HibernateException e){
if(tx!=null)
tx.rollback();
throw e;
}
return student;
}
public List getStudent(Session session,QueryInfo qi)throws HibernateException{
String sql="select count(*) from "+Student.class.getName();
qi.rsCount=((Integer)session.iterate(sql).next()).intValue();
if(qi.rsCount==0)
return Collections.EMPTY_LIST;
if(qi.rsCount%qi.pageSize==0)
qi.pageCount=qi.rsCount/qi.pageSize;
else
qi.pageCount=qi.rsCount/qi.pageSize+1;
if(qi.currentPage>1)
previousPage=true;
if(qi.currentPage<qi.pageCount)
nextPage=true;
int start=(qi.currentPage-1)*qi.pageSize;
Transaction tx=null;
List list=null;
try{
tx=session.beginTransaction();
Query q=session.createQuery("from "+Student.class.getName());
q.setFirstResult(start);
q.setMaxResults(qi.pageSize);
list=q.list();
tx.commit();
}catch(HibernateException e){
if(tx!=null)
tx.rollback();
throw e;
}
return list;
}}
public class QueryInfo {
public int currentPage; //当前页
public int pageSize;//每页记录数
public int pageCount;//总页数
public int rsCount;//总记录数
}下面是配置文件config-struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="splitForm" type="com.split.struts.form.SplitForm" /> </form-beans> <global-exceptions />
<global-forwards >
<forward name="splitpage" path="/form/split.jsp" /> </global-forwards> <action-mappings >
<action
attribute="splitForm"
input="/form/input.jsp"
name="splitForm"
path="/split"
scope="request"
parameter="method"
type="com.split.struts.action.SplitAction" /> <action
path="/splitforward"
parameter="/form/input.jsp"
scope="request"
type="org.apache.struts.acitons.ForwardAction"
/>
</action-mappings>
<message-resources parameter="com.split.struts.ApplicationResources" />
</struts-config>
用来添加数据的表单文件<%@ page language="java" contentType="text/html;charset=gb2312"%><%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />
<title>input.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<CENTER>
<html:form action="/split?method=split" method="post">
<TABLE border=1 width=300 cellspacing=3 cellpadding=0 bgcolor="#cccccc">
<TR bgcolor="#9999CC">
<TD colspan=2><div align=center><FONT color="blue">请输入学生姓名和分数</font></div></TD>
</TR>
<TR>
<TD align=right bgcolor="#9999CC">姓名:</TD><TD align=left bgcolor="#ffffff"><html:text property="name"/></TD>
</TR>
<TR>
<TD align=right bgcolor="#9999CC">分数:</TD><TD align=left bgcolor="#ffffff"><html:text property="score"/></TD>
</TR>
<TR bgcolor="#ffffff">
<TD colspan=2><div align=center><html:submit value="提交"/><html:cancel value="取消"/></div></TD>
</TR>
</TABLE>
</html:form>
</CENTER>
</body>
</html:html>一个用来分页显示的文件
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>
<%@page import="com.split.struts.form.*"%>
<%@page import="com.split.struts.Hibernate.*"%>
<%@page import="java.util.*"%>
<html>
<head>
<title>学生信息系统</title>
</head>
<body>
<CENTER>
<TABLE border=1 width=300 cellspacing=3 cellpadding=0 bgcolor="#cccccc">
<TR bgcolor="#9999CC">
<TD colspan=2><div align=center><FONT color="blue">学生信息</font></div></TD>
</TR>
<TR bgcolor="#ffffff">
<TD align=right width=50%>姓名</TD><TD align=left bgcolor="#ffffff">分数</TD>
</TR>
<logic:present name="form" scope="request">
<logic:iterate name="form" property="list" id="element">
<TR bgcolor="#ffffff">
<TD><bean:write name="element" property="name"/></TD><TD> <bean:write name="element" property="score"/></TD>
</tr>
</logic:iterate>
</logic:present>
<TR bgcolor="#ffffff">
<TD colspan=2 align=right>
<a href="/splitpage/split.do?method=split&page=1">first</a>
<logic:equal name="form" property="previousPage" value="true">
<a href="/splitpage/split.do?method=split&page=<bean:write name="form" property="previous"/>">previous</a>
</logic:equal>
<logic:equal name="form" property="nextPage" value="true">
<a href="/splitpage/split.do?method=split&page=<bean:write name="form" property="next"/>">next</a>
</logic:equal>
<a href="/splitpage/split.do?method=split&page=<bean:write name="form" property="pageCount"/>">last</a>
</TD>
</TR>
<TR>
<TD colspan=2 align=center><html:link action="/splitforward">Return To Add Page</html:link></TD>
</TR>
</TABLE>
</CENTER>
</body>
</html>