SSH项目搭建

1.导入struts、spring、hibernate的jar包。

注意:1.jta.jar不导入会导致Error creating bean with name 'sessionFactory' defined in ServletContext异常
2.asm.jar asm-attrs.jar cglib-2.1.3.jar commons-cllections-2.1.1.jar 包重复也会导致上面的异常

2.建立存放spring、struts配置文件的文件夹,然后再WEB.XML文件中配置他们的路径
<!--②-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!-- 使用通配符,可以导入/WEB-INF/spring文件夹下,以applicationContext-开始的所有xml文件 -->
/WEB-INF/spring/applicationContext-*.xml
</param-value>
</context-param>

<!--①-->
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- ②是①的参数配置,如果不写②会导致 No WebApplicationContext found: no ContextLoaderListener registered?异常-->

3.applicationContext-resources.xml文件,配置数据库连接属性
例:连接oracle数据库,用户:chy_public 密码:chy_public
<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@192.168.0.2:1521:ITSV</value>
</property>
<property name="username">
<value>chy_public</value>
</property>
<property name="password">
<value>chy_public</value>
</property>
</bean>
</beans>

4.applicationContext-hibernate.xml文件: 用spring管理hibernian
<beans>
<!-- 管理使用数据库基本配置 start -->
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>
com/itsv/search/model/Login.hbm.xml
</value>
</list>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
net.sf.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- 管理使用数据库基本配置 end -->

<!-- 配置dao层 -->
<bean id="dao" class="com.dao.hibernate.baseDAOHibernate" singleton="true">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>

</beans>


5.applicationContext-manager.xml文件 spring层
<!-- 添加事务 -->
<beans>
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 连接dao层 -->
<bean id="djSearchManager" parent="txProxyTemplate">
<property name="target">
<bean class="com.itsv.search.service.impl.DjSearchManagerImpl">
<!-- xxxDAO为DjSearchManagerImpl类的属性,yyyDAO为dao层bean的id -->
<property name="xxxDAO"><ref bean="yyyDAO"/></property>
</bean>
</property>
</bean>
</beans>


6.web.xml 字符集过滤
<!-- 使用struts的字符集过滤的配置 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>


<!-- 使用servlet时的servlet及servlet字符集过滤的配置 -->
<filter>
<filter-name>encodingFilterServlet</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilterServlet</filter-name>
<url-pattern>*.go</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>SearchServlet</servlet-name>
<servlet-class>
com.itsv.cms.search.webapp.servlet.SearchServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SearchServlet</servlet-name>
<url-pattern>*.go</url-pattern>
</servlet-mapping>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值