网上有很多关于SSH架构的实例文章,但都显得复杂,我想,入门的朋友,还是希望从最简单的CRUD操作开始,一些复杂的数据库关系,逻辑关系,以后在项目中就能碰到,所以,本文定位于从零开始的一个SSH架构的例子,功能很简单,就是完成一个人员的CRUD操作,麻雀虽小,五脏俱全,希望能对从来没有接触过三者结合开发的朋友带来一点点帮助,这个例子,也算是我的入门实例,从此,开始Struts+Spring+Hibernate之旅 开发工具:MyEclipse5.1+Tomcat+Mysql
开发前准备:数据库安装,tomcat安装,下载jar包这些就不费口水了,直接切入正题吧 CREATE TABLE `people` ( `id` int(11) NOT NULL auto_increment, `name` varchar(100) default NULL, `location` varchar(100) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=gb2312 ROW_FORMAT=COMPRESSED;
首先,建立工程和package,以下是我的工程目录 action DeletePeopleAction.java //删除人员action EditPeopleAction.java //编辑人员action第一步,读取需要修改的人员资料 EditPeopleActionDo.java //修改人员action ListPeopleAction.javaSave //显示人员列表action SavePeopleAction.java //新增人员action SearchPeopleAction.java //查找人员action dao IDAO.java //数据库操作接口 StudentDAOImpl.java //数据库操作实现 domain AbstractPeople.java //实体抽象类 People.hbm.xml //数据库映射 People.java //实体类 service IService.java //服务层接口 StudentManagerImpl.java //服务层实现 util Character.java //字符编码过滤器 PageSupport.java //分页 applicationContext_hibernate.xml //Spring配置文件(hibernate部分) applicationContext_service.xml //Spring配置文件(Service部分)
JSP:这几个jsp从名字就能看出功能,不说了 addStudent.jsp editStudent.jsp searchList.jsp searchStudent.jsp studentList.jsp
代码:
applicationContext_hibernate.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"
>
<
beans
>
<!--
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:comp/env/jdbc/StudentManager</value> </property> </bean>
-->
<
bean
id
="dataSource"
class
="org.springframework.jdbc.datasource.DriverManagerDataSource"
>
<
property
name
="driverClassName"
>
<
value
>
com.mysql.jdbc.Driver
</
value
>
</
property
>
<
property
name
="url"
>
<
value
>
jdbc:mysql://localhost:3306/studentmanager
</
value
>
</
property
>
<
property
name
="username"
>
<
value
>
root
</
value
>
</
property
>
<
property
name
="password"
>
<
value
>
1234
</
value
>
</
property
>
</
bean
>
<
bean
id
="TransactionManager"
class
="org.springframework.jdbc.datasource.DataSourceTransactionManager"
>
<
property
name
="dataSource"
>
<
ref
bean
="dataSource"
/>
</
property
>
</
bean
>
<
bean
id
="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
>
<
property
name
="dataSource"
>
<
ref
local
="dataSource"
/>
</
property
>
<
property
name
="hibernateProperties"
>
<
props
>
<
prop
key
="hibernate.dialect"
>
org.hibernate.dialect.MySQLDialect
</
prop
>
<
prop
key
="hibernate.show_sql"
>
true
</
prop
>
<
prop
key
="hibernate.cache.use_query_cache"
>
true
</
prop
>
<
prop
key
="hibernate.cache.provider_class"
>
org.hibernate.cache.HashtableCacheProvider
</
prop
>
</
props
>
</
property
>
<!--
<property name="mappingDirectoryLocations"> <list> <value>classpath:/domain</value> </list> </property>
-->
<
property
name
="mappingResources"
>
<
list
>
<
value
>
domain/People.hbm.xml
</
value
>
</
list
>
</
property
>
</
bean
>
<
bean
id
="StudentDAO"
class
="dao.StudentDAOImpl"
>
<
property
name
="sessionFactory"
>
<
ref
local
="sessionFactory"
/>
</
property
>
</
bean
>
</
beans
>
applicationContext_service.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"
>
<
beans
>
<
bean
id
="transactionManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager"
>
<
property
name
="sessionFactory"
>
<
ref
bean
="sessionFactory"
/>
</
property
>
</
bean
>
<
bean
id
="studentManager"
class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
>
<
property
name
="transactionManager"
>
<
ref
bean
="transactionManager"
></
ref
>
</
property
>
<
property
name
="target"
>
<
bean
class
="service.StudentManagerImpl"
>
<
property
name
="studentDAO"
>
<
ref
bean
="StudentDAO"
/>
</
property
>
</
bean
>
</
property
>
<
property
name
="transactionAttributes"
>
<
props
>
<
prop
key
="save*"
>
PROPAGATION_REQUIRED
</
prop
>
<
prop
key
="list*"
>
PROPAGATION_REQUIRED,readOnly
</
prop
>
<
prop
key
="delete*"
>
PROPAGATION_REQUIRED
</
prop
>
<
prop
key
="get*"
>
PROPAGATION_REQUIRED,readOnly
</
prop
>
<
prop
key
="edit*"
>
PROPAGATION_REQUIRED
</
prop
>
<
prop
key
="search*"
>
PROPAGATION_REQUIRED
</
prop
>
</
props
>
</
property
>
<
property
name
="proxyTargetClass"
>
<
value
>
true
</
value
>
</
property
>
</
bean
>
</
beans
>
Web.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<
web-app
xmlns
="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
version
="2.4"
xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
<
context-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
/WEB-INF/classes/applicationContext_service.xml, /WEB-INF/classes/applicationContext_hibernate.xml
</
param-value
>
</
context-param
>
<
resource-ref
>
<
description
>
SqlServer Datasource example
</
description
>
<
res-ref-name
>
jdbc/StudentManager
</
res-ref-name
>
<
res-type
>
javax.sql.DataSource
</
res-type
>
<
res-auth
>
Container
</
res-auth
>
</
resource-ref
>
<
filter
>
<
filter-name
>
hibernateFilter
</
filter-name
>
<
filter-class
>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</
filter-class
>
<
init-param
>
<
param-name
>
singleSession
</
param-name
>
<
param-value
>
true
</
param-value
>
</
init-param
>
</
filter
>
<
filter-mapping
>
<
filter-name
>
hibernateFilter
</
filter-name
>
<
url-pattern
>
*.do
</
url-pattern
>
</
filter-mapping
>
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
<
servlet
>
<
servlet-name
>
action
</
servlet-name
>
<
servlet-class
>
org.apache.struts.action.ActionServlet
</
servlet-class
>
<
init-param
>
<
param-name
>
config
</
param-name
>
<
param-value
>
/WEB-INF/struts-config.xml
</
param-value
>
</
init-param
>
<
init-param
>
<
param-name
>
debug
</
param-name
>
<
param-value
>
3
</
param-value
>
</
init-param
>
<
init-param
>
<
param-name
>
detail
</
param-name
>
<
param-value
>
3
</
param-value
>
</
init-param
>
<
load-on-startup
>
0
</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>
action
</
servlet-name
>
<
url-pattern
>
*.do
</
url-pattern
>
</
servlet-mapping
>
</
web-app
>
struts-config.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"
>
<
struts-config
>
<
data-sources
/>
<
form-beans
>
<
form-bean
name
="people"
type
="domain.People"
></
form-bean
>
</
form-beans
>
<
global-exceptions
/>
<
global-forwards
/>
<
action-mappings
>
<
action
path
="/savePeople"
name
="people"
type
="action.SavePeopleAction"
>
<
forward
name
="success"
path
="/listPeople.do"
></
forward
>
</
action
>
<
action
path
="/listPeople"
name
="people"
type
="action.ListPeopleAction"
>
<
forward
name
="success"
path
="/studentList.jsp"
></
forward
>
</
action
>
<
action
path
="/deletePeople"
name
="people"
type
="action.DeletePeopleAction"
>
<
forward
name
="success"
path
="/studentList.jsp"
></
forward
>
</
action
>
<
action
path
="/editPeople"
name
="people"
type
="action.EditPeopleAction"
>
<
forward
name
="success"
path
="/editStudent.jsp"
></
forward
>
</
action
>
<
action
path
="/editPeopleDo"
name
="people"
type
="action.EditPeopleActionDo"
>
<
forward
name
="success"
path
="/studentList.jsp"
></
forward
>
</
action
>
<
action
path
="/searchPeople"
name
="people"
type
="action.SearchPeopleAction"
>
<
forward
name
="success"
path
="/searchList.jsp"
></
forward
>
</
action
>
</
action-mappings
>
<
controller
contentType
="text/html;charset=gb2312"
processorClass
="util.Character"
/>
<
message-resources
parameter
=""
/>
</
struts-config
>
DeletePeopleAction.java
/**/
/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */
package
action;
import
java.util.ArrayList;
import
java.util.List;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.springframework.web.struts.ActionSupport;
import
service.StudentManagerImpl;
import
domain.People;
/** */
/** * MyEclipse Struts * Creation date: 01-17-2007 * * XDoclet definition: * @struts.action validate="true" */
public
class
DeletePeopleAction
extends
ActionSupport
...
{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) ... { Integer id = new Integer(request.getParameter( " id " )); StudentManagerImpl manager = (StudentManagerImpl)getWebApplicationContext().getBean( " studentManager " ); manager.deletePeople(id); List studentList = manager.listPeople( 0 , 5 , " from People " ); request.setAttribute( " studentList " , studentList); request.setAttribute( " currentPage " , 1 ); return mapping.findForward( " success " ); } }
EditPeopleAction.java
/**/
/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */
package
action;
import
java.util.ArrayList;
import
java.util.List;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.springframework.web.struts.ActionSupport;
import
service.StudentManagerImpl;
import
domain.People;
/** */
/** * MyEclipse Struts * Creation date: 01-17-2007 * * XDoclet definition: * @struts.action validate="true" */
public
class
EditPeopleAction
extends
ActionSupport
...
{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) ... { String id = request.getParameter( " id " ); StudentManagerImpl manager = (StudentManagerImpl)getWebApplicationContext().getBean( " studentManager " ); People people = (People)manager.getPeople( new Integer(id)); request.setAttribute( " id " , id); request.setAttribute( " name " , people.getName()); request.setAttribute( " location " , people.getLocation()); return mapping.findForward( " success " ); } }
EditPeopleActionDo.java
/**/
/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */
package
action;
import
java.util.ArrayList;
import
java.util.List;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.springframework.web.struts.ActionSupport;
import
service.StudentManagerImpl;
import
domain.People;
/** */
/** * MyEclipse Struts * Creation date: 01-17-2007 * * XDoclet definition: * @struts.action validate="true" */
public
class
EditPeopleActionDo
extends
ActionSupport
...
{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) ... { People people = (People)form; StudentManagerImpl manager = (StudentManagerImpl)getWebApplicationContext().getBean( " studentManager " ); People sPeople = (People)manager.getPeople( new Integer(people.getId())); sPeople.setName(people.getName()); sPeople.setLocation(people.getLocation()); manager.editPeople(sPeople); List studentList = manager.listPeople( 0 , 5 , " from People " ); request.setAttribute( " studentList " , studentList); request.setAttribute( " currentPage " , 1 ); return mapping.findForward( " success " ); } }
ListPeopleAction.java
/**/
/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */
package
action;
import
java.util.List;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.springframework.web.struts.ActionSupport;
import
service.StudentManagerImpl;
import
util.PageSupport;
/** */
/** * MyEclipse Struts * Creation date: 01-17-2007 * * XDoclet definition: * @struts.action validate="true" */
public
class
ListPeopleAction
extends
ActionSupport
...
{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) ... { String page = request.getParameter( " page " ); int firstRow = 0 ; int lastRow = 0 ; StudentManagerImpl manager = (StudentManagerImpl)getWebApplicationContext().getBean( " studentManager " ); PageSupport sPage = new PageSupport(page); sPage.setPagetotal(manager.listPeople().size()); firstRow = sPage.calcFirstPage(); lastRow = sPage.calcLastPage(); List studentList = manager.listPeople(firstRow - 1 ,lastRow, " from People " ); request.setAttribute( " studentList " , studentList); request.setAttribute( " currentPage " , sPage.getThispage()); return mapping.findForward( " success " ); } }
SavePeopleAction.java
/**/
/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */
package
action;
import
java.util.ArrayList;
import
java.util.List;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.springframework.web.struts.ActionSupport;
import
service.StudentManagerImpl;
import
domain.People;
/** */
/** * MyEclipse Struts * Creation date: 01-17-2007 * * XDoclet definition: * @struts.action validate="true" */
public
class
SavePeopleAction
extends
ActionSupport
...
{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) ... { People people = (People)form; StudentManagerImpl manager = (StudentManagerImpl)getWebApplicationContext().getBean( " studentManager " ); manager.savePeople(people); return mapping.findForward( " success " ); } }
SearchPeopleAction.java
/**/
/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */
package
action;
import
java.util.ArrayList;
import
java.util.List;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.springframework.web.struts.ActionSupport;
import
service.StudentManagerImpl;
import
util.PageSupport;
import
domain.People;
/** */
/** * MyEclipse Struts * Creation date: 01-17-2007 * * XDoclet definition: * @struts.action validate="true" */
public
class
SearchPeopleAction
extends
ActionSupport
...
{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) ... { People people = (People)form; String page = request.getParameter( " page " ); int firstRow = 0 ; int lastRow = 0 ; String name = people.getName(); String location = people.getLocation(); String hql = " from People where name= " + name + " and location= " + location; StudentManagerImpl manager = (StudentManagerImpl)getWebApplicationContext().getBean( " studentManager " ); PageSupport sPage = new PageSupport(page); sPage.setPagetotal(manager.searchPeople(hql).size()); firstRow = sPage.calcFirstPage(); lastRow = sPage.calcLastPage(); List studentList = manager.searchPeople(firstRow - 1 , lastRow, hql); request.setAttribute( " studentList " , studentList); request.setAttribute( " currentPage " , sPage.getThispage()); return mapping.findForward( " success " ); } }
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" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<!--
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=<bean:write name="
student" property
="id"
/>
">删除
</
a
>
<
a
href
="/StudentManager/editPeople.do?id=<bean:write name="
student" 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=<bean:write name="
student" property
="id"
/>
">删除
</
a
>
<
a
href
="/StudentManager/editPeople.do?id=<bean:write name="
student" 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
>
Trackback: http://tb.blog.youkuaiyun.com/TrackBack.aspx?PostId=1487313