1.Project:
ER图:
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: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-4.3.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"> <!-- dataSource数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"> </property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> <!-- sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> <property name="mappingResources"> <list> <value>cn/zzsxt/myblog/model/TblPhoto.hbm.xml</value> <value>cn/zzsxt/myblog/model/TblUser.hbm.xml</value> <value>cn/zzsxt/myblog/model/TblArticle.hbm.xml</value> <value> cn/zzsxt/myblog/model/TblArticletype.hbm.xml </value> </list> </property> </bean> <!-- hibernateTemplate ,并注入sessionFactory--> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- DAO --> <bean id="tblUserDao" class="cn.zzsxt.myblog.dao.impl.TblUserDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <bean id="tblArticleTypeDao" class="cn.zzsxt.myblog.dao.impl.TblArticleTypeDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <bean id="tblArticleDao" class="cn.zzsxt.myblog.dao.impl.TblArticleDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <bean id="tblPhotoDao" class="cn.zzsxt.myblog.dao.impl.TblPhotoDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <!-- Service --> <bean id="tblUserService" class="cn.zzsxt.myblog.service.impl.TblUserServiceImpl"> <property name="tblUserDao" ref="tblUserDao"></property> </bean> <bean id="tblArticleTypeService" class="cn.zzsxt.myblog.service.impl.TblArticleTypeServiceImpl"> <property name="tblArticletypeDao" ref="tblArticleTypeDao"></property> </bean> <bean id="tblArticleService" class="cn.zzsxt.myblog.service.impl.TblArticleServiceImpl"> <property name="articleDao" ref="tblArticleDao"></property> </bean> <bean id="tblPhotoService" class="cn.zzsxt.myblog.service.impl.TblPhotoServiceImpl"> <property name="tblPhotoDao" ref="tblPhotoDao"></property> </bean> <!-- Action --> <bean id="tblUserAction" class="cn.zzsxt.myblog.action.TblUserAction" scope="prototype"> <property name="tblUserService" ref="tblUserService"></property> </bean> <bean id="tblArticleTypeAction" class="cn.zzsxt.myblog.action.ArticleTypeAction" scope="prototype"> <property name="articleTypeService" ref="tblArticleTypeService"></property> </bean> <bean id="tblArticleAction" class="cn.zzsxt.myblog.action.ArticleAction" scope="prototype"> <property name="articleTypeService" ref="tblArticleTypeService"></property> <property name="articleService" ref="tblArticleService"></property> </bean> <bean id="tblPhotoAction" class="cn.zzsxt.myblog.action.TblPhotoAction" scope="prototype"> <property name="photoService" ref="tblPhotoService"></property> </bean> <!-- spring声明式事务 --> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* cn.zzsxt.myblog.service.*.*(..))" id="serviceMethod"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/> </aop:config> </beans>
struts.xml:
<?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> <constant name="struts.multipart.maxSize" value="20971520"></constant> <package name="myblog" extends="struts-default"> <!-- struts2.3以前的版本不需要添加,struts2.5以后的版本添加通配符调用的访问限制 --> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="user-*" class="tblUserAction" method="{1}"> <result name="success">/index.jsp</result> <result name="login">/login.jsp</result> </action> <action name="type-*" class="tblArticleTypeAction" method="{1}"> <result name="success" type="redirectAction">type-list</result> <result name="list">/type_list.jsp</result> </action> <action name="article-*" class="tblArticleAction" method="{1}"> <result name="add">/article_add.jsp</result> <result name="success" type="redirectAction">article-list</result> <result name="list">/article_list.jsp</result> </action> <action name="photo-*" class="tblPhotoAction" method="{1}"> <result name="success" type="redirectAction">photo-list</result> <result name="list">/photo_list.jsp</result> </action> </package> </struts>
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>myBlog</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> <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> <!-- 解决Post请求乱码的过滤器 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 解决session拦截加载问题:OSIV(OpenSessionInViewFilter) --> <filter> <filter-name>isovFilter</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>isovFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- struts2核心过滤器 struts2.3.3的核心过滤器:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2.5核心过滤器:org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>