java利用myeclipse自帶三大框架搭建三大框架(Hibernate+Struts2+Spring)過程詳解
阿新 • • 發佈:2018-02-12
sun 過程 9.png att alt 分享圖片 struts apach sch
搭建過程因人而異,我的搭建過程大致是這樣的:
1.創建一個javaweb項目;
2.導入Spring框架,上圖:
2.1:
2.2:
2.3:
3.導入struts2框架,上圖:
3.1:
3.2:
next:
3.3:
4.導入Hibernate框架,說明:由於hibernate屬於持久層,和數據庫密切相關,所以需要我們提前出創建好數據庫對應視圖,然後再開始下面的操做。上圖:
4.1:
4.2:
4.3:
4.4:
4.5:
4.6:
4.7:利用數據庫相關表和hibernate的orm生成實體類和對應配置文件信息
4.7.1:
4.7.2:
4.7.3:
4.7.4:
4.7.5:
4.7.6:
5.查看初步搭建結果:
6.進一步對配置文件進行配置:
6.1:配置struts.xml文件
6.1.1:選擇要修改的配置文件路徑進行修改
6.1.2:配置struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!--將struts的控制權交給spring --> <constant name="struts.objectFactory" value="spring"></constant> <!-- 修改默認路徑為該項目跟路徑 --> <constant name="struts.convention.result.path" value="/"/> </struts>
6.2.修改application.xml文件
6.2.1原文件
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.OracleDriver"> </property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"> </property> <property name="username" value="gwb"></property> <property name="password" value="gwb"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> </props> </property> </bean> <bean id="UsersDAO" class="UsersDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean></beans>
6.2.2.修改後application.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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/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:annotation-config> <!-- 添加註解掃描支持 --> <context:component-scan base-package="com.cn"></context:component-scan> <!-- 添加事務註解支持 --> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.OracleDriver"> </property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"> </property> <property name="username" value="gwb"></property> <property name="password" value="gwb"></property> </bean> <!--配置事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 添加HibernateTemplate模板 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> </props> </property> <property name="annotatedClasses"> <list> <value>com.cn.entity.Users</value> </list> </property> </bean> <bean id="UsersDAO" class="UsersDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean></beans>
6.3.修改web.xml文件
6.3.1修改前
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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>
6.3.2.修改後
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!-- 添加Spring上下文參數 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter> <filter-name>OpenSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <!-- 添加Spring的上下文環境監聽 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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>
java利用myeclipse自帶三大框架搭建三大框架(Hibernate+Struts2+Spring)過程詳解