搭建SSH+JAP+MYSQL开发环境
一、严格的三层包结构,加入相应包
2.引入struts.xml,名字一定要写对否则报错。
3.引入ssh.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"> <context:annotation-config/> <context:component-scan base-package="cn.edu"/> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="mydataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/music"/> <property name="username" value="root"/> <property name="password" value="pxj1989"/> </bean> <bean id="mysessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="mydataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.format_sql">false</prop> </props> </property> <property name="annotatedClasses"> <list> <value>cn.edu.cqu.cqzy.entity.User</value> <value>cn.edu.cqu.cqzy.entity.Category</value> <value>cn.edu.cqu.cqzy.entity.CategoryRelation</value> <value>cn.edu.cqu.cqzy.entity.Song</value> <value>cn.edu.cqu.cqzy.entity.TempSong</value> <value>cn.edu.cqu.cqzy.entity.SearchSong</value> <value>cn.edu.cqu.cqzy.entity.Scroll</value> <value>cn.edu.cqu.cqzy.entity.OnlineSum</value> <value>cn.edu.seu.cqzy.domain.MapAudio</value> <value>cn.edu.seu.cqzy.domain.MapPoints</value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="mysessionFactory"/> </bean> </beans>
4.修改web.xml,添加struts2过滤器及实例化spring容器
<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容器配置文件位置, 利用Listener实例化spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:ssh.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 为了支持scope="request|session" --> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
4.合理分包,理清项目结构,struts.xml,ssh.xml也分割、引入
struts中引入其它.xml
<include file="struts-*.xml"></include>
ssh中引入其他.xml
<import resource="user_spring.xml" />
时间: 2024-10-29 14:09:43