初学搭建SSH框架

 

学习了一段时间的SSH,是该自己学习搭建框架了。

环境:MyEclipse6.5+Tomcat6.0.18+Struts2.1+Hibernate3.2+Spring2.5.6

现在总结如下:

步骤一:新建一个web项目(选择J2SE5.0,会有一个警告,不用理会)

 

 

步骤二:新建这些包:

 

 

 

步骤三:新建这些文件

 

 

 

 

步骤四:导入Struts2的jar包

 

 

 

 

 

 

 

步骤五:导入Hibernate的jar包

 

 

 

步骤六:导入Spring的jar包

 

 

步骤七:加入数据库驱动(你使用的数据库对应的驱动)

 

步骤八:配置applicationContext.xml文件

 

在该配置文件中加入

 

 

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"></beans>
1.在<beans></beans>中加入数据源配置代码
<!--创建数据源,使用Apache的dbcp数据源工具   SQLSERVER-->
注:请只选择你使用的数据库。<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
SQL2005驱动
这里以SQLServer为例
<propertyname="driverClassName"value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
		<property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=您自己的数据库名"/>
<!—-Orcle配置
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@222.18.166.124:1521:用户名(对应数据库名) "/>
-->
<!—MYSQL配置
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/数据库名 "/>

-->
		<property name="username" value="sa"/>
		<property name="password" value="您自己的数据库密码"/>
		<property name="maxActive" value="30"/>
		<property name="maxIdle" value="8"/>
		<property name="maxWait" value="3000"/>
	</bean>
2.	创建sessionFactory
在<beans></beans>中加入
<!--创建sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		
		<!--配置hibernate属性 -->
		<property name="hibernateProperties">
			<props>
		<!—根据数据库配置相应的hibernate方言,各种数据库的方言不同-->
				<prop key="hibernate.dialect">
				org.hibernate.dialect.SQLServerDialect		
				</prop>
				<!--事务是否自动提交 -->
				<propkey="hibernate.connection.autocommit">true</prop>
				
				<!--显示sql语句 -->
				<prop key="hibernate.show_sql">true</prop>
				
				<!--解决提交乱码问题 -->
				<prop key="connection.useUnicode">true</prop>
				<prop key="connection.characterEncoding">UTF-8</prop>
				
				<!--格式化sql语句 -->
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.order_updates">true</prop>
				
				<!--事务自动管理 -->
				<prop key="hibernate.connection.release_mode">
					after_transaction
				</prop>
				
			</props>
		</property>
		
		<!--配置pojo和数据库的关系映射文件 -->
		<property name="mappingResources">
			<list>
			<!-- 
				<value>这里填Hibernate生成的映射文件的路径(以'/'分隔)</value>
			 -->
			</list>
		</property>
	</bean>
3.	创建事务管理器
创建sessionFactory之后,增加事务管理器代码:如下
	<!--创建事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
4.	创建事务拦截器
创建事务管理器之后,增加事务拦截器代码:如下
<!--创建事务拦截器 -->
	<bean id="transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<!--事务拦截器bean需要依赖注入一个事务管理器 -->
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<!--下面定义事务传播属性-->
			<props>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
5.	创建BeanNameAutoProxyCreator
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<!--指定对满足哪些bean name的bean自动生成业务代理 -->
		<!--
<property name="beanNames">
			下面是所有需要自动创建事务代理的bean-->
			<!--  
				<value>ipagerService</value>
			-->			<!--此处可增加其他需要自动创建事务代理的bean
		</property>
-->
		<!--下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
		<property name="interceptorNames">
			<list>
				<!--此处可增加其他新的Interceptor -->
				<value>transactionInterceptor</value>
			</list>
		</property>
	</bean>

 


6.配置applicationContext-action.xml,applicationContext-dao.xml,applicationContext-service.xml文件

 

步骤九:配置applicationContext-action.xml,applicationContext-dao.xml,applicationContext-service.xml文件

 

1.在这三个配置文件中加入

<beansxmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"></beans>

2.使用的时候。在里面添加,根据实际更改bean

<!--如角色Action

    <bean id="tbRoleAction"class="com.gzqx.dev.web.action.TbRoleAction">

       <propertyname="tbRoleService">

           <refbean="tbRoleService"/>

       </property>

    </bean>

   -->

 

步骤十:在src中添加spring.tld,spring-form.tld,log4j.properties,message.properties,xwork-conversion.properties文件

 

 

 

 

 

 

 步骤十一:在src文件中加入struts.xml

 

其内容是:

<?xmlversion="1.0" encoding="UTF-8" ?>
<!DOCTYPEstruts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<!—汉字乱码处理这里设置UTF-8-->
<constant name="struts.custom.i18n.resources" value="UTF-8">
</constant>

<!--  -->
	<constant name="struts.locale" value="zh_CN"></constant>
	
	<constant name= "struts.multipart.maxSize" value="99999991474985"/>
	<constant name="struts.i18n.encoding" value="UTF-8" />
<!—嵌入另外一个xml文件
	<include file="自己写的其他struts配置文件"></include>
</struts>

 

 


1.  编辑web.xml文件

 

步骤十二:  编辑web.xml文件,在其中加入:

 

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml, classpath:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter>
<filter-name>characterEncodingFilter</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>
</filter>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


 

最后在发布(deploy)到Tomcat服务器中,单击run server,测试是否搭建成功。

说明:

1.这只是一个空的框架,其中的jar包也不完全,以后使用到哪些jar包再添加进去;

2.自己在写配置文件中时,里面出现的中文要注释掉,不然会报错;

3.添加Struts的jar包,有些在spring里才找得到;

4.在添加jar包的时候,版本可能不同,关系不大;

5.如果在添加jar包的时候,在Struts2.1.6+Hibernate3.2+Spring2.5.6找不到,推荐到新浪爱问知识共享资料下载;

http://ishare.iask.sina.com.cn/

6.添加Struts2的jar时,还使用了Xwork的jar包,需要自己额外下载;

7.如果出现什么问题,可以留言,我尽我所能为你们解决;

 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值