如何更新特定的Grails域类属性?

时间:2019-07-08 20:30:43

标签: java grails

我正在尝试更新当前登录用户的密码。但这向我显示了空点异常。

配置文件控制器:

def doPasswordChange(){
    def response = authenticationService.getMember()
    if (!response) {
        flash.message = AppUtil.infoMessage(g.message(code: "invalid.entity"), false)
        redirect(controller: "profile", action: "changePassword")
    } else {
        response = profileService.doPasswordChange(response, params)
        if (!response.isSuccess) {
            flash.redirectParams = response.model
            flash.message = AppUtil.infoMessage(g.message(code: "password.not.change"), false)
            redirect(controller: "profile", action: "changePassword")
        } else {
            flash.message = AppUtil.infoMessage(g.message(code: "updated"))
            redirect(controller: "profile", action: "index")
        }
    }
} 

ProfileService:

def doPasswordChange(Member member, GrailsParameterMap params){
    params.password = params.password.encodeAsMD5()
    params.newPassword = params.newPassword.encodeAsMD5()
    params.confirmPassword = params.confirmPassword.encodeAsMD5()
    def response = AppUtil.saveResponse(false, member)
    if(member.password==params.password && params.confirmPassword==params.newPassword){
        member.password = params.newPassword     //this line is gives me  null point exception
        if(member.validate()){
            log.warn myDomainObj.errors.allErrors.join(' \n')
            member.save(flush: true, failOnError:true)
            response.isSuccess = true
        }
        else println("ok")
    }
    return response
}

给我这个错误:

  

2019-07-09 01:55:04.284错误--- [nio-8080-exec-8]   o.g.web.errors.GrailsExceptionResolver:NullPointerException   处理请求时发生:[POST] / profile / doPasswordChange-   参数:密码:abcdqrty newPassword:12345 ConfirmPassword:   12345登录:更改密码堆栈跟踪如下:

     

java.lang.reflect.InvocationTargetException:为null   org.grails.core.DefaultGrailsControllerClass $ ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:211)     在   org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:188)     在   org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)     在   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)     在   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)     在   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)     在   org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)     在   org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)     在   org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)     在   org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)     在   org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)     在   java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)     在java.lang.Thread.run(Thread.java:748)造成原因:   java.lang.NullPointerException:在时为null   org.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor.getModifiedPropertiesWithAutotimestamp(ClosureEventTriggeringInterceptor.java:206)     在   org.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor.synchronizeHibernateState(ClosureEventTriggeringInterceptor.java:188)     在   org.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor.onPreUpdate(ClosureEventTriggeringInterceptor.java:249)     在   org.hibernate.action.internal.EntityUpdateAction.preUpdate(EntityUpdateAction.java:244)     在   org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:118)     在   org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:589)     在   org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)     在   org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)     在   org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)     在org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1295)     在   org.grails.orm.hibernate.AbstractHibernateGormInstanceApi.flushSession(AbstractHibernateGormInstanceApi.groovy:289)     在   org.grails.orm.hibernate.AbstractHibernateGormInstanceApi $ _performSave_closure3.doCall(AbstractHibernateGormInstanceApi.groovy:254)     在   org.grails.orm.hibernate.GrailsHibernateTemplate.doExecute(GrailsHibernateTemplate.java:299)     在   org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:243)     在   org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:117)     在   org.grails.orm.hibernate.AbstractHibernateGormInstanceApi.performSave(AbstractHibernateGormInstanceApi.groovy:251)     在   org.grails.orm.hibernate.AbstractHibernateGormInstanceApi.save(AbstractHibernateGormInstanceApi.groovy:168)     在   org.grails.datastore.gorm.GormEntity $ Trait $ Helper.save(GormEntity.groovy:151)     在   com.santho.bitmascot.ProfileService.doPasswordChange(ProfileService.groovy:15)     在   com.santho.bitmascot.ProfileController.doPasswordChange(ProfileController.groovy:24)     ...省略了14个共同的框架

我只尝试给其他成员类属性分配一个新值。同样的错误。 我该怎么解决?

1 个答案:

答案 0 :(得分:0)

在视野中

以表格

添加属性useToken =“ true”
<g:form  name="applyForm" useToken="true">
  //ur code
  </g:form>
在控制器中

将您的操作代码放入withForm {}

withForm {
//ur origin code in action
}.invalidToken {
//what to do when token invalidate
        }