关于调用Dao层Save方法后产生Write operations are not allowed in read-only mode (FlushMode.MANUAL)错误导致访问页面显示404

最近才开始使用SSH三大框架做一个管理系统的小项目,由于都是自学,难免会遇到各种大大小小的问题,例如这个问题就是一个初学者会犯的典型的错误,当我们调用数据保存这个业务操作的时候,网页产生了这样的错误提示:

**type** :Status report
**message** :No result defined for action cn.MyItem.web.action.LinkManAction and result error
**description** :The requested resource is not available.

如何去判断什么原因造成这样的情况

判断的方法很简单,我们在我们的页面的底部加入一个S标签的Debug标签,并且在Struts.xml中配置一个全局的错误结果集以及在用到的Action中加入error的结果,当我们产生错误的时候就会跳转到一个页面中,然后我们点击[Debug]就可以看到错误的类型,也就是去值栈中心去找错误的信息。

struts2.xml中的全局异常结果集:
<global-exception-mappings>
            <exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
        </global-exception-mappings>
使用的Action的配置
<action name="LinkManAction_*" class="linkManAction" method="{1}">
            <result name="error">/jsp/linkman/add.jsp</result>
            <result name="add">/jsp/linkman/add.jsp</result>
            <result name="list">/jsp/linkman/list.jsp</result>
            <result name="tolist" type="redirectAction" >
            <param name="namespace">/</param>
            <param name="actionName">LinkManAction_list</param>
            </result>
        </action>
debug标签
 <s:debug/>

为什么会产生数据写入时的错误?

1、首先想到的就是Struts.xml和spring配置文件的内容是否写错以及是否对spring管理的对象设置了setter方法。
2、DAO采用了Spring容器的事务管理策略,如果操作方法的名称和事务策略中指定的被管理的名称不能够匹配上,spring 就会采取默认的事务管理策略(PROPAGATION_REQUIRED,read only).如果是插入和修改操作,就不被允许的,会报异常,但是这个异常往往只能通过调试才能查出来。按照正常的逻辑思维,保存操作是对数据库进行写操作,如果Action层调用Service层的方法再调用Dao层的方法,当在Dao利用hibernate5的模板方法操作写入的时出现了错误,那么在Action层中没办法返回一个自定义的字符串,也就找不到资源了。
3、在网上搜了一下,其中大多数文章又是提OpenSessionInViewFilter又是提OpenSessionInViewInterceptor的,大多云山雾罩、不知所云。(网上提到OpenSessionInViewFilter,因为缺省分配的Session是FlushMode.NEVER的,所以需要在程序中修改自己期望的Mode。收录这篇文章是因为和前一篇文章有关联。)
其实这个异常的提示还是很明确的:在只读模式下(FlushMode.NEVER/MANUAL)写操作不被允许:把你的Session改成FlushMode.COMMIT/AUTO或者清除事务定义中的readOnly标记。

如何进行修改

1、对于第一种错误,那就是一一对应检查,是否URL或者字母大小写或者加了空格等
2、对于第二种方法,就是事务管理配置的问题,对于事务配置有两种方法,推荐用配置文件进行配置。

<!-- 核心事务管理器 -->
    <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" >
        <property name="sessionFactory" ref="sessionFactory" ></property>
    </bean>

    <!-- 配置通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager" >
        <tx:attributes>
            <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
            <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!-- 配置将通知织入目标对象
    配置切点
    配置切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* cn.MyItem.service.impl.*ServiceImpl.*(..))" id="txPc"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
    </aop:config>

3、在web.xml中扩大session作用是应该添加

<!-- 扩大session作用范围
        注意: 任何filter一定要在struts的filter之前调用
     -->
    <filter>
        <filter-name>openSessionInView</filter-name>
        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>flushMode</param-name>
            <param-value>AUTO</param-value>
        </init-param>
    </filter>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值