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

时间:2022-03-28 05:03:06

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

new->Other->ssh 框架整合试例 (spring+struts2+hibernate)ssh 框架整合试例 (spring+struts2+hibernate)ssh 框架整合试例 (spring+struts2+hibernate)

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

ssh 框架整合试例 (spring+struts2+hibernate)输入项目名(这里新建一个项目名叫ssh)


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

->Next->next

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

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

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

->finish

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

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

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

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

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

先导入strus2的jar包ssh 框架整合试例 (spring+struts2+hibernate)

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

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

然后修改web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <display-name>ssh</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置struts2核心过滤器 -->
  <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>
</web-app>
加入struts.xml配置文件
 
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<constant name="struts.objectFactory" value="spring" />

	<package name="default" namespace="/" extends="json-default">
		<!-- 
		<default-action-ref name="index" />
		
		<global-exception-mappings>
			<exception-mapping result="error" exception="java.lang.Exception" />
		</global-exception-mappings>
		
		<action name="error">
			<result>error.jsp</result>
		</action>
		 <action name="login" class="com.accp.actions.UserAction" method="login">
		 	<result name="teacher">/WEB-INF/content/Teacher/main.jsp</result>
		 	<result name="student">/WEB-INF/content/Student/main.jsp</result>
		 	<result name="admin">/WEB-INF/content/Admin/main.jsp</result>
		 	<result name="input">/login.jsp</result>
		 </action>
		 
		 <action name="register" class="com.accp.actions.UserAction" method="register">
		 	<result name="success">/login.jsp</result>
		 	<result name="input">/register.jsp</result>
		 </action>
	</package>

</struts>



接着导入spring的jar包

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

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

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

由于spring和struts2整合还要加入

struts2-spring-plugin-2.3.15.jar

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

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <display-name>ssh</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置struts2核心过滤器 -->
  <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容器spring配置文件的位置 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- 配置监听 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

增加applicationContext.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-3.1.xsd 
       		http://www.springframework.org/schema/aop 
       		http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       		http://www.springframework.org/schema/context
			http://www.springframework.org/schema/context/spring-context-3.1.xsd
			http://www.springframework.org/schema/tx 
			http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
>
	<context:annotation-config />
	<context:component-scan base-package="com.accp" />
	
	<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://localhost:3306/exam"/>
		<property name="username" value="root"/>
		<property name="password" value="123456"/>
		<property name="maxActive" value="40" />  
	    <property name="maxIdle" value="10" />  
	    <property name="maxWait" value="10000" /> 
	</bean>

	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
	    <property name="dataSource" ref="myDataSource"/>
	    <property name="annotatedClasses">
			<list>
				<value>com.entities.User</value>
				<value>com.entities.Subject</value>
				<value>com.entities.Answer</value>
				<value>com.entities.Category</value>
			</list>
	    </property>
	    <property name="hibernateProperties">
		    <props>
		    	<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
		    	<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
		    	<prop key="hibernate.show_sql">true</prop>
		    </props>
	    </property>
	</bean>
	
	 
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<tx:advice id="txAdvice" transaction-manager="txManager" >
	    <tx:attributes>
	        <!-- <tx:method name="get*" read-only= "true" /> -->
	        <tx:method name="*" 
	        	rollback-for="HibernateException"
	        	propagation="REQUIRED" 
	        />
	    </tx:attributes>
	</tx:advice>
	
	<aop:config>
	    <aop:pointcut id="serviceMethods" 
	        expression="execution(public * com.accp.biz..*.*(..))"/> 
	    <aop:advisor pointcut-ref="serviceMethods" advice-ref="txAdvice"/> 
    </aop:config>	
</beans>

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

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

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

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

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

如果要用的注解的话还要导入ssh 框架整合试例 (spring+struts2+hibernate)hibernate-jpa-2.0-api-1.0.0.Final.jar

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

ssh 框架整合试例 (spring+struts2+hibernate)ssh 框架整合试例 (spring+struts2+hibernate)还有数据库驱动ssh 框架整合试例 (spring+struts2+hibernate)mysql-connector-java-5.1.18-bin.jar我用的是mysql

然后启动项目

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

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

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

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