简体   繁体   中英

SpringMvc and Ajax Restful Web Service

I am trying to get validation done when i tab out of the username field, however being new to ajax and jquery. Under is my code i may have alot of errors due to being new at jquery. I am getting the following error:

"NetworkError: 500 Internal Server Error -

http://localhost:8084/crimeTrack/validateUserName.htm?userName=hello"

Javascript:

        $(document).ready(function(){
            $('#userName').blur(function(evt){
                CheckAvailability();                    
            });
        });         

        function CheckAvailability() {
            $.getJSON(
                "validateUserName.htm",
                {userName: $('#userName').val()},
                function(){
                    alert('Sorry UserName Taken');
            });
        }

@Controller: OfficerRegistrationController

 @RequestMapping(value="validateUserName.htm", method=RequestMethod.GET)
 public boolean validateUserName(@RequestParam String userName) throws Exception{
     if (officerdao.OfficerExist(userName)) {
         return true;
     }
     return false;
 }

Servlet:

  <bean name="/validateUserName.htm" class="com.crimetrack.web.OfficerRegistrationController"/>

OfficerDao

public boolean OfficerExist(String userName){

    logger.info("About to check if officers existing");

    String sql = "SELECT userName FROM crimetrack.tbloffficers WHERE userName = ?";
    Map<String, Object> results = getJdbcTemplate().queryForMap(sql, userName);
    String dbUserName = (String)results.get("userName");

    logger.info("Checking if officers exist "+sql);

    if (dbUserName.equals(userName)) {

        return true;

    }else{

        return false;
    }       
}

Error log:

31693 [http-8084-1] DEBUG org.springframework.web.servlet.DispatcherServlet  - Bound request context to thread: org.apache.catalina.connector.RequestFacade@64e5b2  
31693 [http-8084-1] DEBUG org.springframework.web.servlet.DispatcherServlet  - DispatcherServlet with name 'crimetrack' processing GET request for [/crimeTrack/validateUserName.htm]  
31693 [http-8084-1] DEBUG org.springframework.web.servlet.DispatcherServlet  - Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@18ddc48] in DispatcherServlet with name 'crimetrack'  
31693 [http-8084-1] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping  - No handler mapping found for [/validateUserName.htm]  
31693 [http-8084-1] DEBUG org.springframework.web.servlet.DispatcherServlet  - Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@f6852d] in DispatcherServlet with name 'crimetrack'  
31703 [http-8084-1] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping  - Mapping [/validateUserName.htm] to HandlerExecutionChain with handler [com.crimetrack.web.OfficerRegistrationController@1735602] and 1 interceptor  
31703 [http-8084-1] DEBUG org.springframework.web.servlet.DispatcherServlet  - Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@15fc606]  
31703 [http-8084-1] DEBUG org.springframework.web.servlet.DispatcherServlet  - Testing handler adapter [org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@1966070]  
31703 [http-8084-1] DEBUG org.springframework.web.servlet.DispatcherServlet  - Testing handler adapter [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter@1959352]  
31703 [http-8084-1] DEBUG org.springframework.web.servlet.DispatcherServlet  - Last-Modified value for [/crimeTrack/validateUserName.htm] is: -1  
31713 [http-8084-1] DEBUG org.springframework.web.bind.annotation.support.HandlerMethodInvoker  - Invoking request handler method: public boolean com.crimetrack.web.OfficerRegistrationController.validateUserName(java.lang.String) throws java.lang.Exception  
31713 [http-8084-1] DEBUG org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver  - Resolving exception from handler [com.crimetrack.web.OfficerRegistrationController@1735602]: java.lang.NullPointerException  
31723 [http-8084-1] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver  - Resolving exception from handler [com.crimetrack.web.OfficerRegistrationController@1735602]: java.lang.NullPointerException  
31723 [http-8084-1] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver  - Resolving exception from handler [com.crimetrack.web.OfficerRegistrationController@1735602]: java.lang.NullPointerException  
31723 [http-8084-1] DEBUG org.springframework.web.servlet.DispatcherServlet  - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@64e5b2  
31723 [http-8084-1] DEBUG org.springframework.web.servlet.DispatcherServlet  - Could not complete request  
java.lang.NullPointerException  
    at com.crimetrack.web.OfficerRegistrationController.validateUserName(OfficerRegistrationController.java:125)  
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)  
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)  
    at java.lang.reflect.Method.invoke(Unknown Source)  
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)  
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)  
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)  
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)  
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)  
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)  
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)  
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)  
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)  
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)  
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)  
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)  
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)  
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)  
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)  
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)  
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)  
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)  
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)  
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)  
    at java.lang.Thread.run(Unknown Source)  
31723 [http-8084-1] DEBUG org.springframework.web.context.support.XmlWebApplicationContext  - Publishing event in WebApplicationContext for namespace 'crimetrack-servlet': ServletRequestHandledEvent: url=[/crimeTrack/validateUserName.htm]; client=[127.0.0.1]; method=[GET]; servlet=[crimetrack]; session=[null]; user=[null]; time=[30ms]; status=[failed: java.lang.NullPointerException]  
31723 [http-8084-1] DEBUG org.springframework.web.context.support.XmlWebApplicationContext  - Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/crimeTrack/validateUserName.htm]; client=[127.0.0.1]; method=[GET]; servlet=[crimetrack]; session=[null]; user=[null]; time=[30ms]; status=[failed: java.lang.NullPointerException]  

Error in browser

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Invalid handler method return value: false org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.lang.IllegalArgumentException: Invalid handler method return value: false org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.getModelAndView(AnnotationMethodHandlerAdapter.java:971) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:438) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Comments are getting out of hand, so going to create an answer of things to try and will edit if things change.

I'm going to assume you're probably using a SimpleJdbcTemplate from your queryForMap method signature

queryForMap(String sql, Object args...)

One thing to try is instead of a Map, maybe try just querying for Object since you know you're just querying for the username.

ie

getJdbcTemplate().queryForObject("select username...", String.class, userName)

I would also create a JUnit integration test specifically for this DAO which hits against the database in order to more easily debug what is causing the issue. Sometimes exceptions can get consumed rethrown, etc. and it can get confusing as to what is actually causing the issue.

Try these out and let me know what happens, might make things easier to debug.

A sample JUnit might look as follows

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"path/to/app/config.xml"})
public class IntegrationTestOfficerDao {
  @Autowired
  private OfficerDao officerDao;

  @Test
  @Rollback(value=true)
  @Transactional
  public void testUserExists()
  {
     // This would create the user and persist it
     OfficerUser user = createUser(...);
     assertTrue(officerDao.OfficerExist(user.username()));
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM