最近整理了一下ssh项目环境的搭建。写在这里的原因有二,一是记录下来以后好翻阅。二是本着开源的精神(虽然这可能算不上开源吧 呵呵)。下面是一个整体过程,当然这是我个人的习惯。
另外、这是我第一次写这么多,还有代码,而且图片一直上传不成功。项目截图没有。有点小小的遗憾。如果你能看到,希望能帮到你。
1、创建工程。并将字符集定义为utf—8;
2、导入jar包。位于本目录下
3、创建两个src包
config 放ssh的配置文件 位于本目录config中
1、hibernate
hibernate.cfg.xml 主文件
2、spring
applicationContext.xml 主文件
applicationContext-db.xml 数据库文件
2.1、在配置实体文件之前先要简历实体类以及对应的service和action
在完成实体类后,需要配置Person.hbm.xml
applicationContext-person.xml 实体文件
3、struts
struts-person.xml 实体文件
4、struts.xml 主文件
test 做些测试工作
4、web.xml 文件 整合ssh的配置文件 位于本目录web文件夹
5、测试
1、创建工程。并将字符集定义为utf—8;
2、导入jar包。位于本目录下
3、创建两个src包
config 放ssh的配置文件 位于本目录config中
1、hibernate
hibernate.cfg.xml 主文件
2、spring
applicationContext.xml 主文件
applicationContext-db.xml 数据库文件
2.1、在配置实体文件之前先要简历实体类以及对应的service和action
在完成实体类后,需要配置Person.hbm.xml
applicationContext-person.xml 实体文件
3、struts
struts-person.xml 实体文件
4、struts.xml 主文件
test 做些测试工作
4、web.xml 文件 整合ssh的配置文件 位于本目录web文件夹
5、测试
以下是配置文件的内容:
1、hibernate配置文件
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. --><!-- 基于mysql的配置文件,其实我每次都是copy从来没有记住过 --><hibernate-configuration><session-factory><property name= "dialect" >org.hibernate.dialect.MySQLDialect</property><property name= "connection.url" >jdbc:mysql://localhost:3306/joa</property><property name= "connection.username" >root </property><property name= "connection.password" >root </property><property name= "connection.driver_class" >com.mysql.jdbc.Driver</property><property name= "myeclipse.connection.profile" >mysql </property><property name= "show_sql" >true </property><property name= "hbm2ddl.auto" >update </property><!-- 引入实体映射文件 --><mapping resource= "com/joa/domain/Person.hbm.xml" /></session-factory></hibernate-configuration>
Person.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"><hibernate-mapping><class name= "com.joa.domain.Person" ><id name= "pid" type= "java.lang.Long" length= "10" ><generator class= "increment" ></generator></id><property name= "pname" type= "java.lang.String" length= "20" ><column name= "pname" ></column></property></class></hibernate-mapping>2、spring的配置文件applicationContext.xmlapplicationContext-db.xml<?xml version="1.0" encoding="UTF-8"?><beans xmlns= "http://www.springframework.org/schema/beans"xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xmlns:aop= "http://www.springframework.org/schema/aop"xmlns:tx= "http://www.springframework.org/schema/tx"xmlns:context= "http://www.springframework.org/schema/context"xsi:schemaLocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aophttp://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/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd" ><!-- 这是spring的主文件,作用是引入数据库配置文件和实体配置文件 --><!-- 数据库配置文件的引入 --><import resource= "applicationContext-db.xml" /><!-- 实体配置文件 --><import resource= "applicationContext-person.xml" /></beans><?xml version="1.0" encoding="UTF-8"?><beans xmlns= "http://www.springframework.org/schema/beans"xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xmlns:aop= "http://www.springframework.org/schema/aop"xmlns:tx= "http://www.springframework.org/schema/tx"xmlns:context= "http://www.springframework.org/schema/context"xsi:schemaLocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aophttp://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/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd" ><!-- 将hibernate配置文件的实例化交由spring来管理 --><bean id= "sessionFactory" class= "org.springframework.orm.hibernate3.LocalSessionFactoryBean" ><property name= "configLocation" ><!-- 加载hibernate配置文件 --><value>classpath:hibernate/hibernate.cfg.xml </value></property></bean><!-- hibernate 事物管理 --><bean id= "transactionManager" class= "org.springframework.orm.hibernate3.HibernateTransactionManager" ><property name= "sessionFactory" ><ref bean= "sessionFactory" /></property></bean><!-- 配置声明式事物 --><tx:advice transaction-manager= "transactionManager" id= "tx" ><tx:attributes><!-- 所有save开头的方法都会有事物 --><tx:method name= "save*" read-only= "false" /><tx:method name= "*" read-only= "true" /></tx:attributes></tx:advice><!-- aop切面的配置 --><aop:config><aop:pointcut expression= "execution(* com.joa.service.impl.*.*(..))" id= "perform" /><aop:advisor advice-ref= "tx" pointcut-ref= "perform" /></aop:config></beans>
applicationContext-person.xml3、sturts2的配置文件<?xml version="1.0" encoding="UTF-8"?><beans xmlns= "http://www.springframework.org/schema/beans"xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xmlns:aop= "http://www.springframework.org/schema/aop"xmlns:tx= "http://www.springframework.org/schema/tx"xmlns:context= "http://www.springframework.org/schema/context"xsi:schemaLocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aophttp://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/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd" ><!-- 注入sessionFactory 是因为dao中要使用hibernateTemplate --><bean id= "personDao" class= "com.joa.dao.impl.PersonDaoImpl" ><property name= "sessionFactory" ><ref bean= "sessionFactory" /></property></bean><bean id= "personService" class= "com.joa.service.impl.PersonServiceImpl" ><property name= "personDao" ><ref bean= "personDao" /></property></bean><!-- 此处的id必须与struts-person.xml中的action的class属性一致 --><bean id= "personAction" class= "com.joa.struts.action.PersonAction" ><property name= "personService" ><ref bean= "personService" /></property></bean></beans>struts.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts><!-- 配置文件改了以后不用重新启动 --><constant name= "struts.devMode" value= "true" /><!-- 导入实体配置文件 --><include file= "struts/struts-person.xml" ></include></struts>
struts-person.xml
4、web.xml配置文件<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts><package name= "person" namespace= "/" extends= "struts-default" ><!-- 这里的userAction跟spring配置文件中的名称必须一致 --><action name= "personAction_*" method= "{1}" class= "personAction" ></action></package></struts><?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xmlns= "http://java.sun.com/xml/ns/javaee"xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation= "http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "WebApp_ID" version= "2.5" ><display-name>JOA </display-name><!-- 以监听器的方式加入spring --><listener><listener-class>org.springframework.web.context.ContextLoaderListener </listener-class></listener><context-param><param-name>contextConfigLocation </param-name><!-- 加载spring的配置文件 --><param-value>classpath:spring/applicationContext.xml </param-value></context-param><!-- 以过滤器的方式加入struts2 --><filter><filter-name>struts2 </filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class></filter><filter-mapping><filter-name>struts2 </filter-name><url-pattern>/* </url-pattern></filter-mapping><welcome-file-list><welcome-file>index.html </welcome-file><welcome-file>index.htm </welcome-file><welcome-file>index.jsp </welcome-file><welcome-file>default.html </welcome-file><welcome-file>default.htm </welcome-file><welcome-file>default.jsp </welcome-file></welcome-file-list></web-app>