一、在WEB中配置文件spring字符集过滤
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
二、在web.xml加载spring配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
三、在web.xml中配置OpenSessionInViewFilter
<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>false</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
四、在struts-config.xml配置spring
<controllercontentType="text/html;charset=UTF-8">
<set-propertyproperty="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
<message-resourcesparameter="org.school.web.ApplicationResources"/>
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-propertyproperty="contextConfigLocation"
value="/WEB-INF/strutsContext.xml"/>
</plug-in>
五、配置log4j.properties文件
#log4j.rootLogger=DEBUG,A1,R
#log4j.rootLogger=INFO,A1,R
log4j.rootLogger=ERROR,A1
#log4j.category.com.laoer=INFO,A1,R
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-ddHH:mm:ss,SSS}[%c]-[%p]%m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=study_log.txt
log4j.appender.R.MaxFileSize=500KB
log4j.appender.R.MaxBackupIndex=1
#log4j.appender.R.layout=org.apache.log4j.HTMLLayout
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss,SSS}[%t][%c][%p]-%m%n
六、配置applicationContent.xml文件
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<beansdefault-lazy-init="false">
<beanid="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<propertyname="url">
<value>jdbc:mysql://localhost/school?useUnicode=true&characterEncoding=utf8</value>
</property>
<propertyname="username">
<value>root</value>
</property>
<propertyname="password">
<value>vkbje76</value>
</property>
<propertyname="maxActive">
<value>10</value>
</property>
<propertyname="maxIdle">
<value>2</value>
</property>
<propertyname="maxWait">
<value>1200</value>
</property>
</bean>
<beanid="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="dataSource">
<refbean="dataSource"/>
</property>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!--
<propkey="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
-->
<propkey="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<propkey="hibernate.show_sql">true</prop>
<propkey="hibernate.jdbc.fetch_size">25</prop>
<propkey="hibernate.jdbc.batch_size">50</prop>
<!--<propkey="hibernate.hbm2ddl.auto">create-drop</prop>
-->
</props>
</property>
<propertyname="mappingResources">
<list>
<value>org/school/dao/entity/Users.hbm.xml</value>
<value>org/school/dao/entity/Groups.hbm.xml</value>
<value>org/school/dao/entity/GroupUser.hbm.xml</value>
</list>
</property>
</bean>
<beanid="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>
<beanid="baseProxyFactory"abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<propertyname="transactionManager"ref="transactionManager"/>
<propertyname="transactionAttributes">
<props>
<propkey="get*">PROPAGATION_REQUIRED</prop>
<propkey="update*">PROPAGATION_REQUIRED</prop>
<propkey="add*">PROPAGATION_REQUIRED</prop>
<propkey="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!--
<beanid="UserServerProxy"parent="baseProxyFactory">
<propertyname="target">
<reflocal="UserServer"/>
</property>
</bean>
-->
<beanid="UserDAO"class="org.school.dao.hibernate.UserDAO">
<propertyname="sessionFactory">
<refbean="sessionFactory"/>
</property>
</bean>
<beanid="GroupDAO"class="org.school.dao.hibernate.GroupDAO">
<propertyname="sessionFactory">
<refbean="sessionFactory"/>
</property>
</bean>
<beanid="GroupUserDAO"class="org.school.dao.hibernate.GroupUserDAO">
<propertyname="sessionFactory">
<refbean="sessionFactory"/>
</property>
</bean>
<beanid="UserServer"class="org.school.server.impl.UserServer">
<propertyname="userDAO">
<refbean="UserDAO"/>
</property>
<propertyname="groupDAO">
<refbean="GroupDAO"/>
</property>
<propertyname="groupUserDAO">
<refbean="GroupUserDAO"/>
</property>
</bean>
<beanid="UserServerProxy"parent="baseProxyFactory">
<propertyname="target">
<reflocal="UserServer"/>
</property>
</bean>
</beans>
七、写DAO基础类
IBaseDAO.java
packageorg.school.dao;
importjava.io.Serializable;
importjava.util.List;
publicinterfaceIBaseDAO...{
/**//*
*
*/
publicObjectfindById(StringclassName,Serializableid);
publicListfindByProperty(StringtableName,StringpropertyName,Objectvalue);
publicvoidattachDirty(Objectinstance);
publicSerializablesave(ObjecttransientInstance);
publicvoiddelete(ObjectpersistentInstance);
}
BaseDAO.java
packageorg.school.dao.hibernate;
importjava.io.Serializable;
importjava.util.List;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importorg.hibernate.Query;
importorg.school.dao.IBaseDAO;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
/***//**
*Dataaccessobject(DAO)fordomainmodel
*
*@authorMyEclipse-HibernateTools
*/
publicclassBaseDAOextendsHibernateDaoSupportimplementsIBaseDAO...{
publicstaticfinalLoglog=LogFactory.getLog(UserDAO.class);
/**//*(non-Javadoc)
*@seeorg.school.dao.hibernate.IBaseDAO#findById(java.lang.String,java.lang.Integer)
*/
publicObjectfindById(StringclassName,Serializableid)...{
log.debug("gettinginstancewithid:"+id);
try...{
Objectinstance=getHibernateTemplate().get(className,id);
returninstance;
}catch(RuntimeExceptionre)...{
log.error("getfailed",re);
throwre;
}
}
/**//*(non-Javadoc)
*@seeorg.school.dao.hibernate.IBaseDAO#findByProperty(java.lang.String,java.lang.Object)
*/
publicListfindByProperty(StringtableName,StringpropertyName,Objectvalue)...{
log.debug("findinginstancewithproperty:"+propertyName
+",value:"+value);
try...{
StringqueryString="from"+tableName+"asmodelwheremodel."
+propertyName+"=?";
QueryqueryObject=getSession().createQuery(queryString);
queryObject.setParameter(0,value);
returnqueryObject.list();
}catch(RuntimeExceptionre)...{
log.error("findbypropertynamefailed",re);
throwre;
}
}
/**//*(non-Javadoc)
*@seeorg.school.dao.hibernate.IBaseDAO#attachDirty(java.lang.Object)
*/
publicvoidattachDirty(Objectinstance)...{
log.debug("attachingdirtyinstance");
try...{
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attachsuccessful");
}catch(RuntimeExceptionre)...{
log.error("attachfailed",re);
throwre;
}
}
/**//*(non-Javadoc)
*@seeorg.school.dao.hibernate.IBaseDAO#save(java.lang.Object)
*/
publicSerializablesave(ObjecttransientInstance)...{
log.debug("savinginstance");
try...{
returngetHibernateTemplate().save(transientInstance);
//log.debug("savesuccessful");
}catch(RuntimeExceptionre)...{
log.error("savefailed",re);
throwre;
}
}
/**//*(non-Javadoc)
*@seeorg.school.dao.hibernate.IBaseDAO#delete(java.lang.Object)
*/
publicvoiddelete(ObjectpersistentInstance)...{
log.debug("deletinginstance");
try...{
getSession().delete(persistentInstance);
log.debug("deletesuccessful");
}catch(RuntimeExceptionre)...{
log.error("deletefailed",re);
throwre;
}
}
}
分享到:
相关推荐
简单struts+spring+hibernate搭建,配置,适合初学者
DWR+Struts+spring+hibernate的订货系统,自己添加的dwr功能
论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts...
Struts+Hibernate+Spring+Eclipse已经成为轻量级开发J2EE的标准配置,被称为SHS经典组合,这也是目前Java程序员必须掌握的技能。由于使用范围广也使它们的版本更替非常之快,尤其是Java5推出以后这些项目都采用了...
网上订餐系统(struts+spring+hibernate).rar 网上订餐设计主要采用采用MYSQL数据库进行数据表的设计,利用JSP的动态生成页面编程技术, 实现了用网络展示餐饮信息、浏览餐饮页面、注册客户、更改客户信息;同时,...
### 配置Struts+Spring+Hibernate开发环境详解 #### 一、所需软件与环境配置 为了搭建一个基于Struts+Spring+Hibernate的开发环境,首先需要准备一系列的基础软件及工具,具体包括: 1. **Java SDK (J2SDK)**:...
本光盘是《整合Struts+Hibernate+Spring应用开发详解》一书的配书光盘,书中的代码按章存放, 即第二章所使用的代码放在codes文件夹的02文件夹下,依次类推。 本光盘根目录下有11个文件夹,其内容和含义说明如下: ...
SSH整合的关键在于配置文件,包括Struts 2的struts.xml、Spring的applicationContext.xml和Hibernate的hibernate.cfg.xml。这些文件定义了各组件之间的关系和行为。例如,struts.xml中配置Action类及其结果视图,...
《开发者突击:Java Web主流框架整合开发(J2EE+Struts+Hibernate+Spring)》详细介绍了Java Web软件架构中的各种开发技术。主要内容包括:以MySQL为数据库、Tomcat为Web服务器、Eclipse为开发工具、CVS为版本控制工具...
AJAX实现用户登录注册(Struts+Spring+Hibernate+Ajax框架) AJAX实现用户登录注册(Struts+Spring+Hibernate+Ajax框架) AJAX实现用户登录注册(Struts+Spring+Hibernate+Ajax框架)
【基于Struts+Hibernate+Spring的用户管理系统】是一种常见的企业级Web应用架构,它整合了三个主流的Java技术框架,以构建高效、可维护性高的系统。Struts提供了MVC(Model-View-Controller)设计模式,使得业务逻辑...
SSH(Struts+Spring+Hibernate)是一个经典的Java Web开发框架,用于构建高效、可扩展的企业级应用程序。这个教务管理信息系统是基于SSH框架实现的,提供了完整的源代码,可以直接运行,便于开发者学习和参考。 **...
基于struts+spring+hibernate+oracle的移动ssh项目源码 基于struts+spring+hibernate+oracle的移动ssh项目源码 基于struts+spring+hibernate+oracle的移动ssh项目源码 基于struts+spring+hibernate+oracle的移动ssh...
这个系统的核心架构是使用了经典的Java企业级开发框架组合:Struts、Hibernate、Spring以及Oracle数据库。接下来,我们将详细讨论这些技术在车辆管理系统中的作用和相关知识点。 1. Struts: Struts是Apache组织...
基于spring实现的网上订餐系统(struts+spring+hibernate+SQL Server) 基于spring实现的网上订餐系统(struts+spring+hibernate+SQL Server) 基于spring实现的网上订餐系统(struts+spring+hibernate+SQL Server) 基于...
本光盘是《整合Struts+Hibernate+Spring应用开发详解》一书的配书光盘,书中的代码按章存放, 即第二章所使用的代码放在codes文件夹的02文件夹下,依次类推。 本光盘根目录下有11个文件夹,其内容和含义说明如下: ...
使用了Struts+Hibernate+Spring来构建整个网站。 布局使用了CSS和DIV。 部署前请修改以下文件: NewReleseSystem\WebRoot\WEB-INF下面的applicationContext.xml文件 修改里面的数据库的用户名和密码为你所安装的...
基于JavaWeb实现的图书管理系统(struts+spring+hibernate+SQL Server) 基于JavaWeb实现的图书管理系统(struts+spring+hibernate+SQL Server) 基于JavaWeb实现的图书管理系统(struts+spring+hibernate+SQL Server) ...
JSP-车辆管理系统(struts+hibernate+spring+oracle) 仅供学习交流! 后续会持续分享相关资源,记得关注哦! JSP-车辆管理系统(struts+hibernate+spring+oracle) JSP-车辆管理系统(struts+hibernate+spring+oracle) ...