0

当我退出用户名字段时,我试图完成验证,但是对 ajax 和 jquery 来说是新手。下面是我的代码,由于是 jquery 的新手,我可能会遇到很多错误。我收到以下错误:

“网络错误:500 内部服务器错误 -

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;
 }

小服务程序:

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

道官

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;
    }       
}

错误日志:

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]  

浏览器出错

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 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章)

根本原因

java.lang.IllegalArgumentException:无效的处理程序方法返回值: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)

4

1 回答 1

1

评论已经失控,因此要创建一个尝试的答案,并在事情发生变化时进行编辑。

我假设您可能正在使用您的 queryForMap 方法签名中的 SimpleJdbcTemplate

queryForMap(String sql, Object args...)

要尝试的一件事是代替 Map,也许只尝试查询 Object,因为您知道您只是在查询用户名。

IE

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

我还将专门为此 DAO 创建一个 JUnit 集成测试,该测试针对数据库,以便更轻松地调试导致问题的原因。有时异常可能会被重新抛出,等等,并且对于实际导致问题的原因可能会感到困惑。

试试这些,让我知道会发生什么,可能会使事情更容易调试。

一个示例 JUnit 可能如下所示

@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()));
  }
}
于 2012-09-19T14:27:29.697 回答