企业网站建设应该,广州做网站信科建设,商标查询怎么查,腾讯企业邮箱注册申请ssh框架整合步骤如下
提示#xff1a;myeclipse环境、工程环境、tomcat环境的jdk保持一致 1、新建一个工程#xff0c;把工程的编码为utf-8 2、把jsp的编码形式改成utf-8 3、把jar包放入到lib下 4、建立三个src folder src 存放源代码 config 存放配置文件 hibernate…ssh框架整合步骤如下
提示myeclipse环境、工程环境、tomcat环境的jdk保持一致 1、新建一个工程把工程的编码为utf-8 2、把jsp的编码形式改成utf-8 3、把jar包放入到lib下 4、建立三个src folder src 存放源代码 config 存放配置文件 hibernate 存放hibernate的配置文件 spring 存放spring的配置文件 struts 存放struts的配置文件 struts.xml test 存放单元测试 5、在src下建立包 cn.itcast.s2sh.domain 持久化类和映射文件 6、编写dao层和service层 7、写spring的配置文件 1、写sessionFactory 2、测试 3、写dao和service 4、测试 8、写action 9、写spring的配置文件 把action注入到spring容器中 bean idpersonAction classcn.itcast.s2sh.struts2.action.sh.PersonAction scopeprototype
scope为”prototype”保证了action的多实例 10、在web.xml 加入spring的监听器 加入struts2的过滤器 11、请求
详细代码
持久化类与映射文件
?xml version1.0 encodingutf-8?
!DOCTYPE hibernate-mapping PUBLIC -//Hibernate/Hibernate Mapping DTD 3.0//EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd
hibernate-mapping!-- 用来描述一个持久化类name 类的全名table 可以不写 默认值和类名一样 catalog 数据库的名称 一般不写--class namecn.itcast.s2sh.domain.sh.Person!-- 标示属性 和数据库中的主键对应name 属性的名称column 列的名称--id namepid columnpid length200 typejava.lang.Long!-- 主键的产生器就该告诉hibernate容器用什么样的方式产生主键--generator classincrement/generator/id!-- 描述一般属性--property namepname columnpname length20 typestring/propertyproperty namepsex columnpsex length10 typejava.lang.String/property/class
/hibernate-mapping
dao
package cn.itcast.s2sh.sh.dao.impl;import java.io.Serializable;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import cn.itcast.s2sh.domain.sh.Person;
import cn.itcast.s2sh.sh.dao.PersonDao;public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao{Overridepublic void savePerson(Person person) {// TODO Auto-generated method stubthis.getHibernateTemplate().save(person);}Overridepublic Person getPesonById(Serializable id) {// TODO Auto-generated method stubreturn (Person) this.getHibernateTemplate().load(Person.class, id);}}
service
package cn.itcast.s2sh.sh.service.impl;import java.io.Serializable;import cn.itcast.s2sh.domain.sh.Person;
import cn.itcast.s2sh.sh.dao.PersonDao;
import cn.itcast.s2sh.sh.service.PersonService;public class PersonServiceImpl implements PersonService{private PersonDao personDao;public PersonDao getPersonDao() {return personDao;}public void setPersonDao(PersonDao personDao) {this.personDao personDao;}Overridepublic void savePerson(Person person) {// TODO Auto-generated method stubthis.personDao.savePerson(person);}Overridepublic Person getPersonByID(Serializable id) {// TODO Auto-generated method stubreturn this.personDao.getPesonById(id);}
}
spring配置文件
applicationcontext.xml
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:aophttp://www.springframework.org/schema/aopxmlns:txhttp://www.springframework.org/schema/txxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdimport resourceapplicationContext-db.xml/import resourceapplicationContext-person.xml/
/beans
applicationContext-db.xml
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:aophttp://www.springframework.org/schema/aopxmlns:txhttp://www.springframework.org/schema/txxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd!-- bean idsessionFactory classorg.springframework.orm.hibernate3.LocalSessionFactoryBeanproperty nameconfigLocationvalueclasspath:hibernate/hibernate.cfg.xml/value/property/bean--beanclassorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerproperty namelocationsvalueclasspath:jdbc.properties/value/property/beanbean iddataSource destroy-methodcloseclassorg.apache.commons.dbcp.BasicDataSourceproperty namedriverClassName value${jdbc.driverClassName} /property nameurl value${jdbc.url} /property nameusername value${jdbc.username} /property namepassword value${jdbc.password} //beanbean idsessionFactoryclassorg.springframework.orm.hibernate3.LocalSessionFactoryBeanproperty namedataSourceref beandataSource //propertyproperty namemappingResources!-- list all the annotated PO classes --listvaluecn/itcast/s2sh/domain/sh/Person.hbm.xml/value/list/propertyproperty namehibernatePropertiespropsprop keyhibernate.dialectorg.hibernate.dialect.MySQLDialect/propprop keyhibernate.hbm2ddl.autoupdate/propprop keyhibernate.show_sqltrue/propprop keyhibernate.format_sqltrue/prop/props/property/beanbean idtransactionManager classorg.springframework.orm.hibernate3.HibernateTransactionManagerproperty namesessionFactoryref beansessionFactory//property/beantx:advice idtx transaction-managertransactionManagertx:attributestx:method namesave* read-onlyfalse/tx:method nameupdate* read-onlyfalse/tx:method namedelete* read-onlyfalse/!-- * 代表了除了上述的三种情况的以外的情况--tx:method name* read-onlytrue//tx:attributes/tx:adviceaop:configaop:pointcut expressionexecution(* cn.itcast.s2sh.sh.service.impl.*.*(..)) idperform/aop:advisor advice-reftx pointcut-refperform//aop:config
/beans
applicationContext-person.xml
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:aophttp://www.springframework.org/schema/aopxmlns:txhttp://www.springframework.org/schema/txxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdbean idpersonDao classcn.itcast.s2sh.sh.dao.impl.PersonDaoImplproperty namesessionFactoryref beansessionFactory//property/beanbean idpersonService classcn.itcast.s2sh.sh.service.impl.PersonServiceImplproperty namepersonDaoref beanpersonDao//property/beanbean idpersonAction classcn.itcast.s2sh.struts2.action.sh.PersonAction scopeprototypeproperty namepersonServiceref beanpersonService//property/bean
/beans
struts.xml
?xml version1.0 encodingUTF-8?
!DOCTYPE struts PUBLIC-//Apache Software Foundation//DTD Struts Configuration 2.1.7//ENhttp://struts.apache.org/dtds/struts-2.1.7.dtd
strutsconstant namestruts.devMode valuetrue/include filestruts2/struts-person.xml/include!-- constant namestruts.objectFactory valuespring /--
/struts
struts-person.xml
?xml version1.0 encodingUTF-8?
!DOCTYPE struts PUBLIC-//Apache Software Foundation//DTD Struts Configuration 2.1.7//ENhttp://struts.apache.org/dtds/struts-2.1.7.dtd
strutspackage nameperson namespace/ extendsstruts-defaultaction namepersonAction_* method{1} classpersonActionresult nameindexindex.jsp/result/action/package
/struts
web.xml文件的编写
?xml version1.0 encodingUTF-8?
web-app version2.5 xmlnshttp://java.sun.com/xml/ns/javaee xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd!-- 整合Spring --listenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listenercontext-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:spring/applicationContext.xml/param-value/context-paramfilterfilter-nameOpenSessionInViewFilter/filter-namefilter-classorg.springframework.orm.hibernate3.support.OpenSessionInViewFilter/filter-class/filterfilter-mappingfilter-nameOpenSessionInViewFilter/filter-nameurl-pattern*.action/url-pattern/filter-mappingfilterfilter-namestruts2/filter-namefilter-classorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter/filter-class/filterfilter-mappingfilter-namestruts2/filter-nameurl-pattern/*/url-pattern/filter-mappingwelcome-file-listwelcome-fileindex.jsp/welcome-file/welcome-file-list
/web-app
三大框架整合原理
1、三大框架的作用
struts2是一个mvc框架 spring容器 1、利用ioc和di做到了完全的面向接口编程 2、由于spring的声明式事务处理使程序员不再关注事务 3、dao层和service层的类是单例的但是action层是多例 hibernate 就是一个数据库的ormapping的框架 2、整合原理 1、当tomcat启动时做的事情 1、因为在web.xml中 listenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listenercontext-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:spring/applicationContext.xml/param-value/context-paramfilterfilter-namestruts2/filter-namefilter-classorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter/filter-class/filterfilter-mappingfilter-namestruts2/filter-nameurl-pattern/*/url-pattern/filter-mapping
所以在启动的时候执行的是
ContextLoaderListenercontextInitializedthis.contextLoader createContextLoader();加载spring的配置文件这里有一个固定的参数con的textConfigLocation可以指定classpath路径下的spring的配置文也可以任意位置指定配置文件 spring*.xml WEB-INF/任意多个任意文件夹/spring-*.xml如果没有指定固定参数则查找默认的加载路径WEB-INF/applicationContext.xmlthis.contextLoader.initWebApplicationContext(event.getServletContext());启动spring容器总结当tomcat启动的时候spring容器就启动了这个时候service层和dao层所有的单例类就创建对象了struts2容器加载了default.properties,struts-default.xml,struts-plugin.xml,struts.xml
2、请求一个url时发生的事情 1、在引入jar包时导入了struts2-spring-plugin-2.1.8.1.jar包该jar中有一个文件struts-plugin.xml bean typecom.opensymphony.xwork2.ObjectFactory namespring classorg.apache.struts2.spring.StrutsSpringObjectFactory /constant namestruts.objectFactory valuespring /
2、由于上面的配置改变了action的生成方式action由StrutsSpringObjectFactory生成经过查找是由SpringObjectFactory中的buidBean方法 生成的 try {o appContext.getBean(beanName);} catch (NoSuchBeanDefinitionException e) {lass beanClazz getClassInstance(beanName);o buildBean(beanClazz, extraContext);}
3、由上面的代码可以看出先从spring容器中查找相应的action,如果没有找到再根据反射机制创建action, beanName就是struts配置文件class属性的值所以class属性的值和spring中ID的值保持一致
openSessionView模式
在web.xml中filterfilter-nameOpenSessionInViewFilter/filter-namefilter-classorg.springframework.orm.hibernate3.support.OpenSessionInViewFilter/filter-class/filterfilter-mappingfilter-nameOpenSessionInViewFilter/filter-nameurl-pattern*.action/url-pattern/filter-mappingfilterfilter-namestruts2/filter-namefilter-classorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter/filter-class/filterfilter-mappingfilter-namestruts2/filter-nameurl-pattern/*/url-pattern/filter-mapping
OpenSessionInView在第一个位置struts2的过滤器在第二个位置
1、加入了OpenSessionInView模式解决了懒加载的问题 2、因为延迟了session的关闭时间所以在session一级缓存中的数据会长时间停留在内存中 增加了内存的开销
错误
在整合SSH框架的时候出现了很多错误
java.lang.OutOfMemoryError: PermGen space
内存不够可能是tomcat,或者是myeclipse内存不够的原因
参考链接
Tomcat中JVM内存溢出及合理配置 - ye1992的专栏 - 博客频道 - CSDN.NET
[转]MyEclipse内存不足问题 - - ITeye技术网站
myeclipse修改内存大小不足_百度经验
错误 这个错误很奇怪困扰很久原来的那种写法不知道为什么就是不能成功而且在tomcat8中刷新一下就可以了但在tomcat7中不行而且如果tomcat中有了一个新的写法的程序时原来写法的程序也可以用了tomcat程序之间可以互相影响吗找到一个最接近的解释不知道对不对。
参考链接
上网站时输入密码后提示could not open Hibernate Session for transactionnested exception is_百度知道
注解方式实现整合SSH框架
spring配置文件修改为
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:aophttp://www.springframework.org/schema/aopxmlns:txhttp://www.springframework.org/schema/txxmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd!-- bean idsessionFactory classorg.springframework.orm.hibernate3.LocalSessionFactoryBeanproperty nameconfigLocationvalueclasspath:hibernate/hibernate.cfg.xml/value/property/bean--beanclassorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerproperty namelocationsvalueclasspath:jdbc.properties/value/property/beanbean iddataSource destroy-methodcloseclassorg.apache.commons.dbcp.BasicDataSourceproperty namedriverClassName value${jdbc.driverClassName} /property nameurl value${jdbc.url} /property nameusername value${jdbc.username} /property namepassword value${jdbc.password} //beanbean idsessionFactoryclassorg.springframework.orm.hibernate3.LocalSessionFactoryBeanproperty namedataSourceref beandataSource //propertyproperty namemappingResources!-- list all the annotated PO classes --listvaluecn/itcast/s2sh/domain/sh/Person.hbm.xml/value/list/propertyproperty namehibernatePropertiespropsprop keyhibernate.dialectorg.hibernate.dialect.MySQLDialect/propprop keyhibernate.hbm2ddl.autoupdate/propprop keyhibernate.show_sqltrue/propprop keyhibernate.format_sqltrue/prop/props/property/beanbean idhibernateTemplate classorg.springframework.orm.hibernate3.HibernateTemplateproperty namesessionFactoryref beansessionFactory//property/beanbean idtransactionManager classorg.springframework.orm.hibernate3.HibernateTransactionManagerproperty namesessionFactoryref beansessionFactory//property/beancontext:component-scan base-packagecn.itcast.s2sh/context:component-scantx:annotation-driven transaction-managertransactionManager//beans
dao
package cn.itcast.s2sh.sh.dao.impl;import java.io.Serializable;import javax.annotation.Resource;import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;import cn.itcast.s2sh.domain.sh.Person;
import cn.itcast.s2sh.sh.dao.PersonDao;Repository(personDao)
public class PersonDaoImpl implements PersonDao{Resource(namehibernateTemplate)private HibernateTemplate hibernateTemplate;Overridepublic void savePerson(Person person) {// TODO Auto-generated method stubthis.hibernateTemplate.save(person);}Overridepublic Person getPesonById(Serializable id) {// TODO Auto-generated method stubreturn (Person) this.hibernateTemplate.load(Person.class, id);}}
service
package cn.itcast.s2sh.sh.service.impl;import java.io.Serializable;import javax.annotation.Resource;import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import cn.itcast.s2sh.domain.sh.Person;
import cn.itcast.s2sh.sh.dao.PersonDao;
import cn.itcast.s2sh.sh.service.PersonService;Service(personService)
public class PersonServiceImpl implements PersonService{Resource(namepersonDao)private PersonDao personDao;OverrideTransactional(readOnlyfalse)public void savePerson(Person person) {// TODO Auto-generated method stubthis.personDao.savePerson(person);}Overridepublic Person getPersonByID(Serializable id) {// TODO Auto-generated method stubPerson person this.personDao.getPesonById(id);return person;}
}
action
package cn.itcast.s2sh.struts2.action.sh;import javax.annotation.Resource;import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;import cn.itcast.s2sh.domain.sh.Person;
import cn.itcast.s2sh.sh.service.PersonService;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.config.entities.ActionConfig;
import com.opensymphony.xwork2.config.impl.ActionConfigMatcher;Controller(personAction)
Scope(prototype)
public class PersonAction extends ActionSupport{Resource(namepersonService)private PersonService personService;public String savePerson(){Person person new Person();person.setPname(afds);this.personService.savePerson(person);return null;}public String showPerson(){System.out.println(annoation aaaaaaaaaaaaaaaaa);Person person this.personService.getPersonByID(2L);ServletActionContext.getRequest().setAttribute(person, person);return index;}
}
完成