当我从用户名字段中删除时,我正在尝试完成验证,但是对ajax和jquery是新手。在我的代码下面,由于在jquery上是新手,我可能会有很多错误。我收到以下错误:
“NetworkError: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;
}
的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;
}
}
错误日志:
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:无效的处理程序方法返回值: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)
根本原因
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)
答案 0 :(得分:1)
评论已经失控,所以要创建一个可以尝试的事情的答案,并在事情发生变化时进行编辑。
我假设您可能正在使用queryForMap方法签名中的SimpleJdbcTemplate
queryForMap(String sql, Object args...)
要尝试的一件事是代替Map,可能只是尝试查询Object,因为您知道自己只是在查询用户名。
即
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()));
}
}