- ssh搭建
- struts2搭建
- 1、略
- spring集成到struts2项目中
- 1、拷贝spring的jar包,到struts2项目中的lib文件夹下,同时到struts下拷贝加struts2-spring-plugin到工程中
- 2、在web.xml中,添加一个listener,使用的类是spring提供的:ContextLoaderListener
- 3、在web.xml中,配置context-param ,param-name:contextConfigLocation,param-value:class path:spring-*.xml
- 4、创建一个spring-test.xml文件,在classes中,同时创建一个bean标签,该标签指代struts中所创建的action类:<bean id=“testAction” class=“com.lanou.ssh.controller.TestAction” scope=“prototype”></bean>
- 5、修改struts.xml,添加content标签,name=struts.objectFactory
- 6、修改struts.xml中配置的action,将action标签中的class内容改成spring中配置的action的id 例:<action name=“*” class=“testAction” method=“{1}”>
- 7、如果Action中写了service,需要在spring配置文件中,添加对应的service的bean:例:<bean id=“testservice” class=“com.lanou.service.TestAction”></bean> 并且需要将这个对象添加到action的bean中(DI)
- 将hibernate添加到ss框架中
-
- 1、将hibernate的夹包拷贝到工程的lib下,同时拷贝jdbc的jar包
- 2、在spring中,添加需要配置DataSource,我们以c3p0为例
- <bean id=“dadaSource” class=“com.mchange.v2.c3p0.ComboPooledDataSource”></bean>
- 配置子标签:driverClass:XXX,jdbcUrl:XXXXX
- 需要配置session factory,我们以hibernate5支持包中的LocalSessionFactoryBean为例
- dataSource:就是刚才配置的datasource,使用ref引入这个
- hibernateProperties:将hibernate配置文件中,在数据元里面没有出现的所有内容,添加到这条配置里(props)
- mappingLocations设置mapping文件的位置:classpath:com/XXXXXXX
- 配置我们写的dao,将sessionfactory作为属性通过DI注入dao中
- 3、写一个dao出来
- struts.xml 配置文件
-
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPE strutsPUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constantname="struts.enable.DynamicMethodInvocation"value="false"/>
<constantname="struts.devMode"value="true"/>
<constantname="struts.objectFactory"value="spring"/>
<packagename="default"namespace="/user"extends="struts-default">
<actionname="user_*" class="com.lanou.ssh.controller.UserAction"method="{1}">
<result name="success">
/success.html
</result>
<resultname="error">
/error.html
</result>
</action>
</package>
-
</struts>
-
spring-test.xml配置文件
-
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!--配置service -->
<beanid="userService"class="com.lanou.ssh.services.impl.UserService">
<propertyname="userDao"ref="userDao"></property>
</bean>
<!-- 配置action -->
<beanid="UserAction"class="com.lanou.ssh.controller.UserAction"
scope="prototype">
<propertyname="userService"ref="userService"></property>
</bean>
<!--配置到dao -->
<beanid="userDao"class="com.lanou.ssh.dao.impl.UserDao">
<propertyname="sessionFactory"ref="sessionFactory"></property>
</bean>
<!-- 配置sessionFactory,供dao用 -->
<beanid="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--注入数据源,-->
<propertyname="dataSource"ref="dataSource"></property>
<!-- 配置别的属性 -->
<propertyname="hibernateProperties">
<props>
<propkey="show_sql">true</prop>
<propkey="hibernate.hbm2ddl.auto">update</prop>
<propkey="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<propkey="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<!--hibernate的映射文件路径 -->
<propertyname="mappingLocations" value="classpath:com/lanou/ssh/entity/mappings/*.hbm.xml"></property>
</bean>
<!--以下是c3p0的配置文件 ,供sessionFactory用 -->
<beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<propertyname="driverClass"value="com.mysql.jdbc.Driver"/>
<propertyname="jdbcUrl"
value="jdbc:mysql://localhost:3306/ssh?characterEncoding=utf-8&useSSL=false"/>
<propertyname="user"value="root"/>
<propertyname="password"value="root"/>
<propertyname="maxPoolSize"value="30"></property>
<propertyname="minPoolSize"value="5"/>
<propertyname="acquireIncrement"value="1"></property>
</bean>
</beans>
user.hbm.xml配置文件
-
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEhibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.lanou.ssh.entity.User" table="tb_user">
<id name="id" >
<generatorclass="uuid"></generator>
</id>
<propertyname="username" ></property>
<propertyname="pwd" ></property>
</class>
</hibernate-mapping>
- struts2搭建
ssh框架整合步骤
最新推荐文章于 2022-08-16 08:50:04 发布