无法从Grails中的服务执行保存操作

时间:2016-08-25 14:46:38

标签: grails gorm

我正在尝试从服务类中保存表单详细信息。我正在尝试使用remoteForm,但我无法从我的控制器转到服务。当我这样做时,发生以下错误。

.....Error
|
java.lang.NullPointerException
Error |
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130)
Error |
    at org.codehaus.groovy.grails.orm.support.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:85)
Error |
    at com.standout.utilityapplication.DataEntrySevice$$EPutsrXF.saveDataentryDetails(DataEntrySevice.groovy)
Error |
    at com.standout.utilityapplication.DataEntrySevice$$DPutsrXF.__execute(Unknown Source)
Error |
    at com.standout.utilityapplication.DataEntrySevice.__execute(DataEntrySevice.groovy)
Error |
    at com.standout.utilityapplication.DataEntrySevice$saveDataentryDetails.call(Unknown Source)
Error |
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
Error |
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
Error |
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
Error |
    at com.standout.utilityapplication.DataEntryController$$EPutsrXF.saveAjax(DataEntryController.groovy:21)
Error |
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Error |
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
Error |
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Error |
    at java.lang.reflect.Method.invoke(Method.java:606)
Error |
    at org.springsource.loaded.ri.ReloadedTypeInvoker$2.invoke(ReloadedTypeInvoker.java:122)
Error |
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1299)
Error |
    at org.codehaus.groovy.grails.web.servlet.mvc.MixedGrailsControllerHelper.invoke(MixedGrailsControllerHelper.java:154)
Error |
    at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleAction(AbstractGrailsControllerHelper.java:375)
Error |
    at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.executeAction(AbstractGrailsControllerHelper.java:252)
Error |
    at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleURI(AbstractGrailsControllerHelper.java:205)
Error |
    at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleURI(AbstractGrailsControllerHelper.java:126)
Error |
    at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController.handleRequest(SimpleGrailsController.java:72)
Error |
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:50)
Error |
    at org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet.doDispatch(GrailsDispatcherServlet.java:347)
Error |
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
Error |
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
Error |
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
Error |
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
Error |
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
Error |
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198)
Error |
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
Error |
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
Error |
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
Error |
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
Error |
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
Error |
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
Error |
    at org.codehaus.groovy.grails.web.mapping.UrlMappingUtils.forwardRequestForUrlMappingInfo(UrlMappingUtils.java:178)
Error |
    at org.codehaus.groovy.grails.web.mapping.UrlMappingUtils.forwardRequestForUrlMappingInfo(UrlMappingUtils.java:144)
Error |
    at org.codehaus.groovy.grails.web.mapping.UrlMappingUtils.forwardRequestForUrlMappingInfo(UrlMappingUtils.java:135)
Error |
    at org.codehaus.groovy.grails.web.mapping.filter.UrlMappingsFilter.doFilterInternal(UrlMappingsFilter.java:216)
Error |
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
Error |
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
Error |
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
Error |
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
Error |
    at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
Error |
    at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:146)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
Error |
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
Error |
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
Error |
    at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:62)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
Error |
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
Error |
    at grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.java:59)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
Error |
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
Error |
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
Error |
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
Error |
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
    at org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:69)
Error |
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
    at org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
Error |
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
Error |
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Error |
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
Error |
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
Error |
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
Error |
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
Error |
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
Error |
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
Error |
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
Error |
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
Error |
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
Error |
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
Error |
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
Error |
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
Error |
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
Error |
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
Error |
    at java.lang.Thread.run(Thread.java:745)
2016-08-25 19:56:55,051 [http-bio-8080-exec-6] INFO  org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/utilityapplication]  - GroovyPagesServlet:  "/WEB-INF/grails-app/views/dataEntry/saveAjax.gsp" not found

这是我的dataentry.gsp文件

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<meta name="layout" content="main"/>
<title>Data Entry</title>
</head>
<body>
  <div id="saveMsgDiv"></div>
    <g:formRemote name="dataEntryForm" update="saveMsgDiv" url="[controller: 'DataEntry', action:'saveAjax']">

     <table align="center">
         <tr>
         <td>
            <label for='employeeId'>EmployeeID:</label>
           </td>
           <td>
            <g:textField name='employee_id' required id='employee_id'/>
         </td>
         </tr>

         <tr>
         <td>
            <label for='team'>Team:</label>
         </td>
         <td>
           <g:select name='team' required id='team' from="${['Java', 'QA', 'Database']}"
           noSelection="['':'---select---']"/>
         </td>
         </tr>

         <tr>
           <td>
            <label for='receiptDate'>Receipt Date:</label>
           </td>
           <td>
            <g:textField name='receipt_dt' required id='receipt_dt'/>
           </td>
         </tr>

          <tr>
             <td>
            <label for='restaurantName'>Restaurant Name:</label>
            </td>
            <td>
            <g:textField name='restaurant_name' required id='restaurant_name'/>
             </td>
         </tr>
         <tr>
          <td>
            <label for='numberOfPersons'>Number of Persons:</label>
            </td>
            <td>
            <g:textField name='number_of_persons' required id='number_of_persons' maxlength="3"/>
         </td>
         </tr>
         <tr>
         <td>
            <label for='amount'>Amount:</label>
            </td>
            <td>
            <g:textField name='amount' required id='amount'/>
            </td>
         </tr>
         <tr>
         <td>
            <label for='billSubmittedDate'>Bill Submitted Date:</label>
            </td>
            <td>
            <g:textField name='bill_submitted_dt' required id='bill_submitted_dt'/>
         </td>
         </tr>
         <tr>
         <td>
            <label for='reImbursed'>Reimbursed:</label>
            </td>
            <td>
            <g:textField name='reimbursed' required id='reimbursed'/>
         </td>
         </tr>
         <tr>
         <td>
            <label for='submitBank'>Submitted to Bank:</label>
            </td>
            <td>
            <g:textField name='presented_bank_fl' required id='presented_bank_fl'/>
            </td>
         </tr>
         <tr>
         <td>
            <label for='dateOfSubmission'>Date of Bank Submission:</label>
            </td>
            <td>
            <g:textField name='presented_bank_dt' required id='presented_bank_dt'/>
            </td>
         </tr>
         <tr>
         <td colspan="2" style="text-align:center;">
         <g:submitButton name="Submit" style="cursor:pointer"/>
         </td>
         </tr>
      </table>
  </g:formRemote>
</body>
</html>

这是我的DataEntryController.groovy

package com.standout.utilityapplication

import grails.plugin.springsecurity.annotation.Secured
import groovy.sql.Sql


@Secured(['ROLE_DATAENTRY'])
class DataEntryController {

    def dataSource
    def index() { }
    def dataentry(){
        println("Inside DataEntryController")
    }

    def saveAjax(){
        def result
        try{
            DataEntrySevice service = new DataEntrySevice()
            service.saveDataentryDetails(params)
            }
            catch(Exception e){
                e.printStackTrace();
            }

    }

}

这是我的DataEntryService.groovy

package com.standout.utilityapplication

import grails.transaction.Transactional
import groovy.sql.Sql

@Transactional
class DataEntrySevice {

    def dataSource
    def int saveDataentryDetails(Map params) {
        def result
        try{

            Dataentry d = new Dataentry();
            d.amount = params.amount
            d.bill_submitted_dt = params.bill_submitted_dt
            d.employee_id = params.employee_id
            d.number_of_persons = params.number_of_persons
            d.presented_bank_dt = params.presented_bank_dt
            d.presented_bank_fl = params.presented_bank_fl
            d.receipt_dt = params.receipt_dt
            d.reimbursed = params.reimbursed
            d.restaurant_name = params.restaurant_name
            d.team = params.team
            d.save();

            }
            catch(Exception e){
                e.printStackTrace();
            }
            catch(Exception e){
                println(e)
            }
            return result
    }
}

2 个答案:

答案 0 :(得分:2)

您尚未正确注入服务。你必须以这种方式注入它

@Secured(['ROLE_DATAENTRY'])
class DataEntryController {

    def dataSource

    DataEntrySevice service

    def index() { }
    def dataentry(){
        println("Inside DataEntryController")
    }

    def saveAjax(){
        def result
        try{

            service.saveDataentryDetails(params)
            }
            catch(Exception e){
                e.printStackTrace();
            }

    }

}

答案 1 :(得分:0)

除了quindimidev指出的事实,你实例化你的服务而不是注入它。实际的错误消息指出“/WEB-INF/grails-app/views/dataEntry/saveAjax.gsp”未找到所以你saveAjax方法做了一些事情,但在它之后没有呈现任何东西。

def saveAjax(){
        def result
        int status=200
        try{

            service.saveDataentryDetails(params)
            }
            catch(Exception e){
                status=404
                e.printStackTrace();
            }
        render status: status
        //or
        response.status=status
        render (view: 'someView')

    }