SSJ框架集成

框架集成应分步集成,先集成Spring+JPA,测试确保无误后再集成Struts。同理,导包也应分步导入,先导入Spring和Hibernate所需jar包,再导入Struts所需jar包。注意别忘的导入数据库的驱动包。
beans.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
   
    <context:annotation-config/><!-- Spring依赖注入注解功能 -->

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="itcast"/>
    </bean>
   
    <!-- 配置事务管理器 -->
    <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <tx:annotation-driven transaction-manager="txManager"/><!-- 采用注解的方式配置事务 -->
   
    <bean id="personService" class="cn.itcast.service.imp.PersonServiceBean"/>
    <bean id="perService" class="cn.itcast.service.imp.PerServiceBean"/>
    <bean name="/person/list" class="cn.itcast.web.action.PersonAction"/>
    <bean name="/person/manage" class="cn.itcast.web.action.PersonManageAction"/>
</beans>
web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.xml</param-value>
  </context-param>
  <!-- 通过listener对Spring容器进行实例化 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- struts的配置 -->
  <servlet>
    <servlet-name>struts</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>struts</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
 
  <filter><!-- 解决struts1.3提交数据到数据库时的乱码问题 -->
  <filter-name>encoding</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-mapping>
  <filter-name>encoding</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  <display-name />
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
persistence.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/batch?useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
struts-config.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
<form-beans>
<form-bean name="personForm" type="cn.itcast.web.formbean.PersonForm"/>
</form-beans>

  <action-mappings>
<action path="/person/list" validate="false">
<forward name="list" path="/WEB-INF/page/personlist.jsp"></forward>
</action>
<action path="/person/manage" parameter="method" validate="false" scope="request" name="personForm">
<forward name="message" path="/WEB-INF/page/message.jsp"></forward>
</action>
</action-mappings>

<controller><!-- 配置一个控制器,由Spring提供,由此就可以把struts中的action交给spring管理 -->
<set-property value="org.springframework.web.struts.DelegatingRequestProcessor" property="processorClass"/>
</controller>
</struts-config>
附件中含有整个项目的配置树形图及相关jar包,若有重复包请选择最新版本。

你可能感兴趣的:(spring,Web,Hibernate,struts,jpa)