ssh 框架整合试例 (spring+struts2+hibernate)

1.首先用Eclipse创建一个web项目(Eclipse EE 版)

new->Other->

输入web 然后选择Dynamic Web Project->next->

输入项目名(这里新建一个项目名叫ssh)

 

 

->Next->next

->finish

接下来就是导入我们所需的jar包

先导入strus2的jar包

然后修改web.xml配置文件

 

[html][/html] view plaincopyprint?

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://java.sun.com/xml/ns/javaee” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd” id=”WebApp_ID” version=”3.0″>
  3.   <display-name>ssh</display-name>
  4.   <welcome-file-list>
  5.     <welcome-file>index.html</welcome-file>
  6.     <welcome-file>index.htm</welcome-file>
  7.     <welcome-file>index.jsp</welcome-file>
  8.     <welcome-file>default.html</welcome-file>
  9.     <welcome-file>default.htm</welcome-file>
  10.     <welcome-file>default.jsp</welcome-file>
  11.   </welcome-file-list>
  12.   <!– 配置struts2核心过滤器 –>
  13.   <filter>
  14.     <filter-name>struts2</filter-name>
  15.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  16.   </filter>
  17.   <filter-mapping>
  18.     <filter-name>struts2</filter-name>
  19.     <url-pattern>/*</url-pattern>
  20.   </filter-mapping>
  21. </web-app>
[html][/html] view plaincopyprint?

  1. 加入struts.xml配置文件
[html][/html] view plaincopyprint?

  1. <pre name=”code” class=”html”><?xml version=”1.0″ encoding=”UTF-8″ ?>
  2. <!DOCTYPE struts PUBLIC
  3.     “-//Apache Software Foundation//DTD Struts Configuration 2.3//EN”
  4.     “http://struts.apache.org/dtds/struts-2.3.dtd”>
  5. <struts>
  6.     <constant name=”struts.objectFactory” value=”spring” />
  7.     <package name=”default” namespace=”/” extends=”json-default”>
  8.         <!–
  9.         <default-action-ref name=”index” />
  10.         <global-exception-mappings>
  11.             <exception-mapping result=”error” exception=”java.lang.Exception” />
  12.         </global-exception-mappings>
  13.         <action name=”error”>
  14.             <result>error.jsp</result>
  15.         </action>
  16.          <action name=”login” class=”com.accp.actions.UserAction” method=”login”>
  17.             <result name=”teacher”>/WEB-INF/content/Teacher/main.jsp</result>
  18.             <result name=”student”>/WEB-INF/content/Student/main.jsp</result>
  19.             <result name=”admin”>/WEB-INF/content/Admin/main.jsp</result>
  20.             <result name=”input”>/login.jsp</result>
  21.          </action>
  22.          <action name=”register” class=”com.accp.actions.UserAction” method=”register”>
  23.             <result name=”success”>/login.jsp</result>
  24.             <result name=”input”>/register.jsp</result>
  25.          </action>
  26.     </package>
  27. </struts>
  28. </pre><br><br>

 

接着导入spring的jar包

 

由于spring和struts2整合还要加入

struts2-spring-plugin-2.3.15.jar

接着还是修改web.xml配置文件

 

[html][/html] view plaincopyprint?

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://java.sun.com/xml/ns/javaee” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd” id=”WebApp_ID” version=”3.0″>
  3.   <display-name>ssh</display-name>
  4.   <welcome-file-list>
  5.     <welcome-file>index.html</welcome-file>
  6.     <welcome-file>index.htm</welcome-file>
  7.     <welcome-file>index.jsp</welcome-file>
  8.     <welcome-file>default.html</welcome-file>
  9.     <welcome-file>default.htm</welcome-file>
  10.     <welcome-file>default.jsp</welcome-file>
  11.   </welcome-file-list>
  12.   <!– 配置struts2核心过滤器 –>
  13.   <filter>
  14.     <filter-name>struts2</filter-name>
  15.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  16.   </filter>
  17.   <filter-mapping>
  18.     <filter-name>struts2</filter-name>
  19.     <url-pattern>/*</url-pattern>
  20.   </filter-mapping>
  21.   <!– 告诉spring容器spring配置文件的位置 –>
  22.   <context-param>
  23.     <param-name>contextConfigLocation</param-name>
  24.     <param-value>classpath:applicationContext.xml</param-value>
  25.   </context-param>
  26.   <!– 配置监听 –>
  27.   <listener>
  28.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  29.   </listener>
  30. </web-app>

增加applicationContext.xml配置文件

 

 

[html][/html] view plaincopyprint?

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <beans xmlns=”http://www.springframework.org/schema/beans”
  3.        xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  4.        xmlns:context=”http://www.springframework.org/schema/context”
  5.        xmlns:aop=”http://www.springframework.org/schema/aop”
  6.        xmlns:tx=”http://www.springframework.org/schema/tx”
  7.        xsi:schemaLocation=”http://www.springframework.org/schema/beans
  8.             http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  9.             http://www.springframework.org/schema/aop
  10.             http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  11.             http://www.springframework.org/schema/context
  12.             http://www.springframework.org/schema/context/spring-context-3.1.xsd
  13.             http://www.springframework.org/schema/tx
  14.             http://www.springframework.org/schema/tx/spring-tx-3.1.xsd”
  15. >
  16.     <context:annotation-config />
  17.     <context:component-scan base-package=”com.accp” />
  18.     <bean id=”myDataSource” class=”org.apache.commons.dbcp.BasicDataSource” destroy-method=”close”>
  19.         <property name=”driverClassName” value=”com.mysql.jdbc.Driver”/>
  20.         <property name=”url” value=”jdbc:mysql://localhost:3306/exam”/>
  21.         <property name=”username” value=”root”/>
  22.         <property name=”password” value=”123456″/>
  23.         <property name=”maxActive” value=”40″ />
  24.         <property name=”maxIdle” value=”10″ />
  25.         <property name=”maxWait” value=”10000″ />
  26.     </bean>
  27.     <bean id=”sessionFactory” class=”org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean”>
  28.         <property name=”dataSource” ref=”myDataSource”/>
  29.         <property name=”annotatedClasses”>
  30.             <list>
  31.                 <value>com.entities.User</value>
  32.                 <value>com.entities.Subject</value>
  33.                 <value>com.entities.Answer</value>
  34.                 <value>com.entities.Category</value>
  35.             </list>
  36.         </property>
  37.         <property name=”hibernateProperties”>
  38.             <props>
  39.                 <!– <prop key=”hibernate.current_session_context_class”>thread</prop> –>
  40.                 <prop key=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</prop>
  41.                 <prop key=”hibernate.show_sql”>true</prop>
  42.             </props>
  43.         </property>
  44.     </bean>
  45.     <bean id=”txManager” class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
  46.         <property name=”sessionFactory” ref=”sessionFactory” />
  47.     </bean>
  48.     <tx:advice id=”txAdvice” transaction-manager=”txManager” >
  49.         <tx:attributes>
  50.             <!– <tx:method name=”get*” read-only= “true” /> –>
  51.             <tx:method name=”*”
  52.                 rollback-for=”HibernateException”
  53.                 propagation=”REQUIRED”
  54.             />
  55.         </tx:attributes>
  56.     </tx:advice>
  57.     <aop:config>
  58.         <aop:pointcut id=”serviceMethods”
  59.             expression=”execution(public * com.accp.biz..*.*(..))”/>
  60.         <aop:advisor pointcut-ref=”serviceMethods” advice-ref=”txAdvice”/>
  61.     </aop:config>
  62. </beans>

 

然后加入hibernate所需的hibernate3.jar和必须jar包如下:

 

javassist-3.12.0.GA.jar这个jar包不用加,前面加过

如果要用的注解的话还要导入hibernate-jpa-2.0-api-1.0.0.Final.jar

因为要用到连接池和数据库所以还要导入commons-pool-1.6.jar和commons-dbcp-1.4.jar

还有数据库驱动mysql-connector-java-5.1.18-bin.jar我用的是mysql

然后启动项目

发现有点问题还要加入aopalliance-1.0.jar和aspectjweaver.jar,因为项目可能要用到ajax所以还要加入struts2-json-plugin-2.3.7.jar

呵呵成功了!里面有些配置文件相信能看懂吧。。。

标签