两周的实训周。做一个图书管理的网站。有用户管理,图书管理,图片管理,评论管理的功能。底层代码方法实现就是对数据库的增删查改,通过这些方法的测试个人分类管理。
配置:MyEclipse8.5 hibernate3.3 jdk1.6 (发现jdk1.8和框架不兼容) struts架包基本都是2.1.6版本的,只要不重复就不会有冲突。
错误:不能加载spring配置文件。web.xml spring路径后加*
重新加载项目,删除缓存。每次修改完spring配置文件后都要再重启一下tomcat服务器,如果代码没问题还总是有错误,就要重启软件了。spring配置文件里要定义hibernate的配置文件的路径,还有事务管理器,定义规则,定义哪些方法使用这些规则。错误:不能加载spring配置文件。web.xml spring路径后加*
不能加载spring配置文件,一定是文件没写正确,或者hibernate路径删除了。实体类不能映射,方法没有成功,hql语句可能写错了,把各个类的映射文件xml数据库名删掉一下。设置lazy=false可以延时加载,提高效率。
重新加载项目,删除缓存。每次修改完spring配置文件后都要再重启一下tomcat服务器,如果代码没问题还总是有错误,就要重启软件了。spring配置文件里要定义hibernate的配置文件的路径,还有事务管理器,定义规则,定义哪些方法使用这些规则。
贴一下自己的项目的几个配置文件仅供参考。
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property value="classpath:hibernate.cfg.xml" name="configLocation"></property>
<!-- Druid连接池 -->
</bean>
<!-- 配置事务管理器-->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 定义事务通知,需要指定一个事务管理器-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- 定义属性,声明事务规则-->
<tx:attributes>
<!-- 对get/find/search/query开头的方法要求只读事务 -->
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="search*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="register*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="post*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 定义哪些方法应用这些规则 -->
<aop:pointcut id="ServiceMethod"
expression="execution (* demo.service.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="ServiceMethod" />
</aop:config>
<!-- <bean id="baseDao" class="demo.dao.impl.BaseDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="baseService" class="demo.service.impl.BaseServiceImpl">
<property name="baseDao" ref="baseDao"></property>
</bean> -->
<bean id="userInfoDao" class="demo.dao.impl.UserInfoDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userInfoService" class="demo.service.impl.UserInfoServiceImpl">
<property name="userInfoDao" ref="userInfoDao"></property>
</bean>
<bean id="userInfoAction" class="demo.action.UserInfoAction">
<property name="userInfoService" ref="userInfoService"></property>
</bean>
<!--
<bean id="userDao" class="demo.dao.impl.UserDaoImpl"> <property
name="sessionFactory" ref="sessionFactory"></property> </bean> <bean
id="userService" class="demo.service.impl.UserServiceImpl"> <property
name="userDao" ref="userDao"></property> </bean> <bean id="userAction"
class="demo.action.UserAction"> <property name="userService"
ref="userService"></property> </bean>
-->
<bean id="bookDao" class="demo.dao.impl.BookDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="bookService" class="demo.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"></property>
<property name="baseDao" ref="bookDao"></property>
</bean>
<bean id="bookAction" class="demo.action.BookAction">
<property name="bookService" ref="bookService"></property>
<property name="baseService" ref="bookService"></property>
</bean>
<bean id="packageInfoDao" class="demo.dao.impl.PackageInfoDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="packageInfoService" class="demo.service.impl.PackageInfoServiceImpl">
<property name="packageInfoDao" ref="packageInfoDao"></property>
</bean>
<bean id="packageInfoAction" class="demo.action.PackageInfoAction">
<property name="packageInfoService" ref="packageInfoService"></property>
</bean>
<!--
<bean id="reviewDao" class="demo.dao.impl.ReviewDaoImpl"> <property
name="sessionFactory" ref="sessionFactory"></property> </bean> <bean
id="reviewService" class="demo.service.impl.ReviewServiceImpl">
<property name="reviewDao" ref="reviewDao"></property> </bean> <bean
id="reviewAction" class="demo.action.ReviewAction"> <property
name="reviewService" ref="reviewService"></property> </bean>
-->
</beans>
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<filter>
<filter-name>Spring character encoding filter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- Spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<filter-mapping>
<filter-name>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 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>
<!-- Dwr Ajax-->
<servlet>
<servlet-name>DwrServlet</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DwrServlet</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<!-- 根据默认配置文件来初始化Spring容器,配置Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
struts文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" >
<struts>
<constant name="struts.objectFactory" value="spring"></constant>
<constant name="struts.ui.theme" value="simple"></constant>
<!--
<action name="findAllBooks" class="bookAction" method="findAllBooks">
<result name="success" type="redirectAction">/firstPage.jsp</result>
</action>
-->
<!--
<action name="login" class="bookAction" method="login"> <result
name="success" type="redirectAction">findAllBooks</result> </action>
-->
<package name="book" extends="struts-default" namespace="/">
<action name="saveBook" class="bookAction" method="saveBook">
<result name="success" type="redirectAction">findAllBook</result>
</action>
<action name="editBook" class="bookAction" method="editBook">
<result name="success">/add_book.jsp</result>
</action>
<action name="deleteBook" class="bookAction" method="deleteBook">
<result name="success" type="redirectAction">findAllBook</result>
</action>
<action name="manage" class="bookAction" method="manage">
<result name="success" type="dispatcher">/manager.jsp</result>
</action>
<action name="findAllBooks" class="bookAction" method="findAllBooks">
<result name="success">/firstPage.jsp</result>
</action>
<action name="findAllBook" class="bookAction" method="findAllBook">
<result name="right">/book.jsp</result>
</action>
</package>
<package name="user" extends="struts-default" namespace="/">
<action name="findAllUsers" class="userInfoAction" method="findAllUsers">
<result name="success">/manager.jsp</result>
</action>
<action name="saveUser" class="userInfoAction" method="saveUser">
<result name="success" type="redirectAction">findAllUsers</result>
</action>
<action name="saveRegister" class="userInfoAction" method="saveRegister">
<result name="success">findAllBooks</result>
</action>
<action name="editUser" class="userInfoAction" method="editUser">
<result name="success">/addmanager.jsp</result>
</action>
<action name="deleteUser" class="userInfoAction" method="deleteUser">
<result name="success" type="redirectAction">findAllUsers</result>
</action>
<action name="login" class="userInfoAction" method="login">
<result name="success" type="redirectAction">findAllBooks</result>
<result name="error">/fail.jsp</result>
</action>
<action name="register" class="userInfoAction" method="register">
<result name="success">/success.jsp</result>
<result name="error">/fail.jsp</result>
</action>
<action name="findUsersByPage" class="userInfoAction" method="findUsersByPage">
<result name="success">/manager.jsp</result>
</action>
<action name="findAllBooks" class="userInfoAction" method="findAllBooks">
<result name="success">/firstPage.jsp</result>
</action>
</package>
<package name="postUser" namespace="/" extends="json-default">
<action name="postLogin" class="userInfoAction" method="postLogin">
<result type="json">
<param name="json">rows</param>
</result>
</action>
<action name="postLoginUser" class="userInfoAction" method="postLoginUser">
<result type="json">
<param name="json">rows</param>
</result>
</action>
<action name="postRegister" class="userInfoAction" method="postRegister">
<result type="json">
<param name="json">rows</param>
</result>
</action>
</package>
<package name="postPackage" namespace="/remote" extends="json-default">
<action name="postPackageScan" class="packageInfoAction" method="postPackageScan">
<result type="json">
<param name="json">row</param>
</result>
</action>
<action name="postFindBySn" class="packageInfoAction" method="postFindBySn">
<result type="json">
<param name="json">row</param>
</result>
</action>
<action name="postDelivery" class="packageInfoAction" method="postDelivery">
<result type="json">
<param name="json">row</param>
</result>
</action>
<action name="postSign" class="packageInfoAction" method="postSign">
<result type="json">
<param name="json">row</param>
</result>
</action>
</package>
</struts>
这几个文件在网上是有现成的,拷过来改改符合自己的项目条件就好了。