IDAO.java
package dao;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
public interface IDAO ...{ public void save(Object obj); public List list(final int firstRow,final int lastRow,final String hql); public List list(); public void delete(Object obj); public Object getPeople(Integer id); public void editPeople(Object obj); public List searchPeople(final int firstRow,final int lastRow,final String hql); public List searchPeople(final String sql); }
StudentDAOImpl.java package dao;
import java.sql.SQLException; import java.util.ArrayList; import java.util.List;
import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.criterion.DetachedCriteria; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import domain.People;
public class StudentDAOImpl extends HibernateDaoSupport implements IDAO ...{ public List searchPeople(final int firstRow,final int lastRow,final String hql)...{ return getHibernateTemplate().executeFind(new HibernateCallback() ...{ public Object doInHibernate(Session s) throws HibernateException, SQLException ...{ Query query = s.createQuery(hql); query.setFirstResult(firstRow); query.setMaxResults(lastRow); List list = query.list(); return list; } }); } public List searchPeople(final String hql)...{ return getHibernateTemplate().executeFind(new HibernateCallback() ...{ public Object doInHibernate(Session s) throws HibernateException, SQLException ...{ Query query = s.createQuery(hql); List list = query.list(); return list; } }); } public void save(Object obj) ...{ if(obj instanceof People)...{ People people=(People)obj; try...{ getHibernateTemplate().save(people); }catch(Exception e)...{ e.printStackTrace(); } } } public List list()...{ return getHibernateTemplate().find("from People"); } public List list(final int firstRow,final int lastRow,final String hql)...{
return getHibernateTemplate().executeFind(new HibernateCallback() ...{ public Object doInHibernate(Session s) throws HibernateException, SQLException ...{ Query query = s.createQuery(hql); query.setFirstResult(firstRow); query.setMaxResults(lastRow); List list = query.list(); return list; } }); } public void editPeople(Object obj)...{ if(obj instanceof People)...{ People people=(People)obj; try...{ getHibernateTemplate().saveOrUpdate(people); }catch(Exception e)...{ e.printStackTrace(); } } } public Object getPeople(Integer id)...{ return getHibernateTemplate().get(People.class, id); } public void delete(Object obj)...{ if(obj instanceof People)...{ getHibernateTemplate().delete((People)obj); } } }
AbstractPeople.java package domain; import org.apache.struts.action.ActionForm;
public abstract class AbstractPeople extends ActionForm implements java.io.Serializable ...{
// Fields
private Integer id; private String name; private String location;
// Constructors
/** *//** default constructor */ public AbstractPeople() ...{ }
/** *//** full constructor */ public AbstractPeople(String name, String location) ...{ this.name = name; this.location = location; }
// Property accessors
public Integer getId() ...{ return this.id; } public void setId(Integer id) ...{ this.id = id; }
public String getName() ...{ return this.name; } public void setName(String name) ...{ this.name = name; }
public String getLocation() ...{ return this.location; } public void setLocation(String location) ...{ this.location = location; }
} People.hbm.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" ""> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping package="domain"> <class name="People" table="people" > <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="increment"></generator> </id> <property name="name" type="java.lang.String"> <column name="name" length="100" /> </property> <property name="location" type="java.lang.String"> <column name="location" length="100" /> </property> </class> </hibernate-mapping>
People.java package domain;
/** *//** * People generated by MyEclipse - Hibernate Tools */ public class People extends AbstractPeople implements java.io.Serializable ...{
// Constructors
/** *//** default constructor */ public People() ...{ }
/** *//** full constructor */ public People(String name, String location) ...{ super(name, location); } }
IService.java package service;
import java.util.List;
public interface IService ...{ public void savePeople(Object obj); public List listPeople(int firstRow,int lastRow,String hql); public List listPeople(); public void deletePeople(Integer id); public Object getPeople(Integer id); public void editPeople(Object obj); public List searchPeople(String hql); public List searchPeople(int firstRow,int lastRow,String hql); }
StudentManagerImpl.java
package service;
import java.util.ArrayList; import java.util.List;
import dao.IDAO; import domain.People;
public class StudentManagerImpl implements IService ...{ private IDAO studentDAO; public void savePeople(Object obj) ...{ if(obj instanceof People)...{ People people=(People)obj; studentDAO.save(people); } } public List searchPeople(String hql)...{ return studentDAO.searchPeople(hql); } public List searchPeople(int firstRow,int lastRow,String hql)...{ return studentDAO.searchPeople(firstRow, lastRow, hql); } public List listPeople(int firstRow,int lastRow,String hql)...{ List peopleList=new ArrayList(); peopleList=studentDAO.list(firstRow,lastRow,hql); return peopleList; } public List listPeople()...{ return studentDAO.list(); } public void editPeople(Object obj)...{ if(obj instanceof People)...{ People people=(People)obj; studentDAO.editPeople(people); } } public Object getPeople(Integer id)...{ return studentDAO.getPeople(id); } public void deletePeople(Integer id)...{ studentDAO.delete(this.getPeople(id)); } public IDAO getStudentDAO() ...{ return studentDAO; } public void setStudentDAO(IDAO studentDAO) ...{ this.studentDAO = studentDAO; }
}
Character.java
package util;
import java.io.IOException; import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionMapping; import org.apache.struts.action.RequestProcessor;
public class Character extends RequestProcessor...{ protected boolean processRoles(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws IOException, ServletException ...{
return super.processRoles(request, response, mapping); } protected void processContent(HttpServletRequest request, HttpServletResponse response) ...{ try ...{
request.setCharacterEncoding("gb2312"); System.out.println(request.getCharacterEncoding()); } catch (UnsupportedEncodingException e) ...{ e.printStackTrace(); } super.processContent(request, response); } }
PageSupport.java
package util;
public class PageSupport ...{ int thispage = 0;// 当前页数
int pagesize = 5;// 每页最大记录数
int firstpage = 0;// 记录开始的位置
int lastpage = 0;// 记录结束的位置
int pagetotal = 0;// 总记录数
int pagenums = 0;// 总页数
public PageSupport(String page)...{ if(page==null||page.equals(""))...{ thispage=1; } else...{ thispage=Integer.parseInt(page); } } public int calcFirstPage()...{ pagenums = pagetotal / pagesize; if (pagetotal % pagesize != 0)...{ pagenums = pagenums + 1; } if (thispage > pagenums) thispage = pagenums; else if(thispage<=0)...{ thispage=1; } if(thispage<=1)...{ firstpage=1; } else...{ firstpage = (thispage-1) * pagesize+1; }
return this.firstpage; } public int calcLastPage()...{
return this.pagesize; } public int getFirstpage() ...{ return firstpage; } public void setFirstpage(int firstpage) ...{ this.firstpage = firstpage; } public int getLastpage() ...{ return lastpage; } public void setLastpage(int lastpage) ...{ this.lastpage = lastpage; } public int getPagenums() ...{ return pagenums; } public void setPagenums(int pagenums) ...{ this.pagenums = pagenums; } public int getPagesize() ...{ return pagesize; } public void setPagesize(int pagesize) ...{ this.pagesize = pagesize; } public int getPagetotal() ...{ return pagetotal; } public void setPagetotal(int pagetotal) ...{ this.pagetotal = pagetotal; } public int getThispage() ...{ return thispage; } public void setThispage(int thispage) ...{ this.thispage = thispage; }
}
addStudent.jsp <%...@ page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <form action="/StudentManager/savePeople.do" method="post"> 姓名:<input type="text" name="name"/><br> 籍贯:<input type="text" name="location"/><br> <input type="submit" name="submit" value="提交"/> </form> <a href="searchStudent.jsp">查询</a> </body> </html>
editStudent.jsp <%...@ page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <form action="/StudentManager/editPeopleDo.do" method="post"> <input type="hidden" name="id" value="<%=request.getAttribute("id") %>"/> 姓名:<input type="text" name="name" value="<%=request.getAttribute("name") %>"/><br> 籍贯:<input type="text" name="location" value="<%=request.getAttribute("location") %>"/><br> <input type="submit" name="submit" value="提交"/> </form> </body> </html>
searchList.jsp <%...@ page language="java" contentType="text/html;charset=gb2312"%> <%...@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%...@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%...@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%...
int previousPage=((Integer)request.getAttribute("currentPage")).intValue()-1; int nextPage=((Integer)request.getAttribute("currentPage")).intValue()+1; %> </head> <body> <table border=1> <tr> <td>姓名</td><td>籍贯</td><td align="center">操作</td> </tr> <logic:present name="studentList" scope="request"> <logic:iterate id="student" name="studentList"> <tr> <td><bean:write name="student" property="name"/></td> <td><bean:write name="student" property="location"/></td> <td align="center"><a href="/StudentManager/deletePeople.do?id=" property="id"/>">删除</a> <a href="/StudentManager/editPeople.do?id=" property="id"/>">修改</a></td> </tr> </logic:iterate> </logic:present> </table> <a href="/StudentManager/searchPeople.do?page=<%=previousPage%>">上一页</a> <a href="/StudentManager/searchPeople.do?page=<%=nextPage%>">下一页</a> <br><br> <a href="addStudent.jsp">新增</a> <a href="searchStudent.jsp">查询</a> </body> </html>
searchStudent.jsp <%...@ page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> 输入查询条件: <form action="/StudentManager/searchPeople.do" method="post"> 姓名:<input type="text" name="name"/><br> 籍贯:<input type="text" name="location"/><br> <input type="submit" name="submit" value="查询"/> </form>
</body> <a href="addStudent.jsp">新增</a> </html>
studentList.jsp
<%...@ page language="java" contentType="text/html;charset=gb2312"%> <%...@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%...@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%...@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%...
int previousPage=((Integer)request.getAttribute("currentPage")).intValue()-1; int nextPage=((Integer)request.getAttribute("currentPage")).intValue()+1; %> </head> <body> <table border=1> <tr> <td>姓名</td><td>籍贯</td><td align="center">操作</td> </tr> <logic:present name="studentList" scope="request"> <logic:iterate id="student" name="studentList"> <tr> <td><bean:write name="student" property="name"/></td> <td><bean:write name="student" property="location"/></td> <td align="center"><a href="/StudentManager/deletePeople.do?id=" property="id"/>">删除</a> <a href="/StudentManager/editPeople.do?id=" property="id"/>">修改</a></td> </tr> </logic:iterate> </logic:present> </table> <a href="/StudentManager/listPeople.do?page=<%=previousPage%>">上一页</a> <a href="/StudentManager/listPeople.do?page=<%=nextPage%>">下一页</a> <br><br> <a href="addStudent.jsp">新增</a> <a href="searchStudent.jsp">查询</a> </body> </html>
|