基於SSH框架的小型論壇項目 html
1、項目入門 傳送門java
2、框架整合 傳送門mysql
3、用戶模塊 傳送門web
4、頁面顯示 傳送門spring
5、帖子模塊 傳送門sql
6、點贊模塊 傳送門數據庫
7、輔助模塊 傳送門express
導入Jar包apache
導入完別忘了Add Pathtomcat
查看Eclipse IDE中導入的Jar包
Gary_SSHForum->Build Path->Configure Build Path
添加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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> </beans>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">
整合Spring與Struts
<!-- 讓spring隨着Web項目的啓動而啓動 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 讀取配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 擴大session的範圍 --> <filter> <filter-name>openSession</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 讓struts啓動 --> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Gary_SSHForum</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> <!-- 讓spring隨着Web項目的啓動而啓動 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 讀取配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 擴大session的範圍 --> <filter> <filter-name>openSession</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 讓struts啓動 --> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
整合Spring與Hibernate
MySQL中添加數據庫
<!-- 配置數據源 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name ="jdbcUrl" value="jdbc:mysqL:///garyssh_forum"></property> <property name ="driverClass" value="com.mysql.jdbc.Driver"></property> <property name ="user" value="root"></property> <property name ="password" value="123456"></property> </bean> <!-- 配置sessionFactory --> <bean name = "sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sqp">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="mappingDirectoryLocations" value="classpath:com/Gary/domain"></property>
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置數據源 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name ="jdbcUrl" value="jdbc:mysqL:///garyssh_forum"></property> <property name ="driverClass" value="com.mysql.jdbc.Driver"></property> <property name ="user" value="root"></property> <property name ="password" value="123456"></property> </bean> <!-- 配置sessionFactory --> <bean name = "sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sqp">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="mappingDirectoryLocations" value="classpath:com/Gary/domain"></property> </bean> </beans>
整合Spring與Hibernate配置事務
<!-- 配置事務的核心管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 通知 --> <tx:advice id="advice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 織入 --> <aop:config> <!-- 切入點 --> <aop:pointcut expression="execution(* com.Gary.service.*.*(..))" id="pc"/> <!-- 配置切面 切入點+通知 --> <aop:advisor advice-ref="advice" pointcut-ref="pc"/> </aop:config>
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置數據源 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name ="jdbcUrl" value="jdbc:mysqL:///garyssh_forum"></property> <property name ="driverClass" value="com.mysql.jdbc.Driver"></property> <property name ="user" value="root"></property> <property name ="password" value="123456"></property> </bean> <!-- 配置sessionFactory --> <bean name = "sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sqp">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="mappingDirectoryLocations" value="classpath:com/Gary/domain"></property> </bean> <!-- 配置事務的核心管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 通知 --> <tx:advice id="advice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 織入 --> <aop:config> <!-- 切入點 --> <aop:pointcut expression="execution(* com.Gary.service.*.*(..))" id="pc"/> <!-- 配置切面 切入點+通知 --> <aop:advisor advice-ref="advice" pointcut-ref="pc"/> </aop:config> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Gary_SSHForum</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> <!-- 讓spring隨着Web項目的啓動而啓動 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 讀取配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 擴大session的範圍 --> <filter> <filter-name>openSession</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 讓struts啓動 --> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置數據源 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name ="jdbcUrl" value="jdbc:mysqL:///garyssh_forum"></property> <property name ="driverClass" value="com.mysql.jdbc.Driver"></property> <property name ="user" value="root"></property> <property name ="password" value="123456"></property> </bean> <!-- 配置sessionFactory --> <bean name = "sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sqp">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="mappingDirectoryLocations" value="classpath:com/Gary/domain"></property> </bean> <!-- 配置事務的核心管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 通知 --> <tx:advice id="advice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 織入 --> <aop:config> <!-- 切入點 --> <aop:pointcut expression="execution(* com.Gary.service.*.*(..))" id="pc"/> <!-- 配置切面 切入點+通知 --> <aop:advisor advice-ref="advice" pointcut-ref="pc"/> </aop:config> </beans>
項目過程當中的問題
1、ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
Log4j2:用於輸出日誌
出現文件提早結束:xml的問題
struts添加了約束 必需要有<struts> </struts>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> </struts>
2、Tomcat啓動超時問題Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds 傳送門
把tomcat啓動時間適當延長:
打開tomcat設置頁,修改啓動時間:
經上一步若是仍是啓動超時的話,能夠對項目進行clean,它的做用是將工程中的.class文件所有刪除,同時從新編譯工程:
而後再次從新部署tomcat,啓動便可
1、項目入門 傳送門
2、框架整合 傳送門
3、用戶模塊 傳送門
4、頁面顯示 傳送門
5、帖子模塊 傳送門
6、點贊模塊 傳送門
7、輔助模塊 傳送門