首先需要下载struts2 ,spring,hibernate 的资源包;
Struts2资源包下载路径:http://www.apache.org/
spring资源包下载路径:http://projects.spring.io/spring-framework/
hibernate资源包下载路径:http://hibernate.org/orm/downloads/
在SSH框架的搭建步骤:
第一步:在eclipse中创建一个web项目,并生成web.xml文件;
第二步:往lib目录导入jar包:
1.struts2所需的jar包:struts2.3.30\struts2-blank\WebContent\WEB-INF\lib(图片)
2.spring 所需的jar包:spring-framework-4.2.2.RELEASE-dist\spring-framework-4.2.2.RELEASE\libs(图片)
3.hibernate所需的jar包:
hibernate-release-5.2.2.Final\lib\required(图片)
第三步:在web.xml 文件中配置struts的过滤器 ,spring 的监听器;(代码如下)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>ssh</display-name> <welcome-file-list> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!--配置struts的过滤器 --> <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> <!--配置spring的监听器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
第四步:在src目录下创建四个包 分别是action 控制层, dao 数据层,entity 实体层 ,
service 业务层;(图片说明)
第五步:struts.xml的配置:在src目录下(代码)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <!-- 上面的头,注意版本,从样例里复制过来 showcase.war\WEB-INF\src\java\struts.xml --> <struts> <!-- 第1步:先定义一个包 --> <package name="mypck001" extends="struts-default"> <!-- 第2步:定义一个action,配置跳转信息 name 类似于Servlet @WebServlet("/IndexServlet") http://xxxx/项目名/Index.action http://xxxx/项目名/Index class 对应于自己写的Action类 当不写method属性时,默认调用的是execute --> <action name="Index" class="myIndexAction" method="execute1"> <!-- 跳转是forward /WEB-INF/是防止jsp不经过action就可以访问 --> <result name="success">/WEB-INF/jsp/index2.jsp</result> <result name="error">/WEB-INF/jsp/s_tag.jsp</result> </action> </package> </struts>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <!-- 上面的头,注意版本,是从struts2.3.3版本复制过来 struts2-showcase.war\WEB-INF\src\java\struts.xml --> <struts> <!-- 告知Struts2运行时使用Spring来创建对象 --> <constant name="struts.objectFactory" value="spring" /> <!-- include文件用于分割,实现多人并发不冲突 --> <include file="s001.xml" /> <include file="s002.xml" /> <include file="s003.xml" /> </struts>
第六步:spring的配置: applicationContext.xml文件在src目录下:(代码)
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- 引入外部属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 所有需要类的实例都由spring去管理 --> <bean id="myIndexAction" class="ssh.action.IndexAction" scope="prototype"> <!-- setIs(myIndexService) --> <property name="is" ref="myIndexService"/> </bean> <!-- myIndexService = new ssh.service.IndexServiceImpl() --> <bean id="myIndexService" class="ssh.service.IndexServiceImpl" scope="prototype"> <property name="id" ref="myIndexDao"/> </bean> <bean id="myIndexDao" class="ssh.dao.IndexDaoImpl" scope="prototype"> <!-- 注入seesionFactory --> <property name="sessionFactory" ref="mySessionFactory"></property> </bean> <!-- 错误的做法,new org.hibernate.internal.SessionFactoryImpl() 不可以,需要configuration来创建 bean id="mySessionFactory" class="org.hibernate.internal.SessionFactoryImpl"></bean --> <bean id="mySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 注入连接池,包含了数据库用户名,密码等等信息 --> <property name="dataSource" ref="myDataSource"/> <!-- 配置Hibernate的其他的属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.connection.autocommit">false</prop> <!-- 开机自动生成表 --> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="mappingResources"> <list> <value>ssh/entity/BookCard.hbm.xml</value> </list> </property> </bean> <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> <!-- 每300秒检查所有连接池中的空闲连接 --> <property name="idleConnectionTestPeriod" value="300"></property> <!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 --> <property name="maxIdleTime" value="900"></property> <!-- 最大连接数 --> <property name="maxPoolSize" value="2"></property> </bean> <!-- dbcp连接池 --> <!--切换dbcp连接池 要导入commons-dbcp.jar 和 commons-pool.jar 包 --> <!--<bean id="myDataSource2" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean> --> </beans>
时间: 2024-10-10 07:04:37