当前位置: 首页 > 知识库问答 >
问题:

SpringMvc和Ajax Restful Web服务

慕项明
2023-03-14
http://localhost:8084/crimeTrack/validateUserName.htm?userName=hello"
        $(document).ready(function(){
            $('#userName').blur(function(evt){
                CheckAvailability();                    
            });
        });         

        function CheckAvailability() {
            $.getJSON(
                "validateUserName.htm",
                {userName: $('#userName').val()},
                function(){
                    alert('Sorry UserName Taken');
            });
        }
 @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:无效的处理程序方法返回值: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.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(AnnotationMethodAdapter.java:438)org.springframework.web.servlet.mvc.annotation.AnnotationMethodAdapter.java:424)servlet.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.servet.http.httpservlet.service(httpservlet.java:617)javax.servet.http.servlet.service(httpservlet.java:717)

共有1个答案

赖渊
2023-03-14

评论正在失去控制,所以要创建一个事情的答案,以尝试,并将编辑,如果事情改变。

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

queryForMap(String sql, Object args...)

要尝试的一件事是不是地图,也许只是尝试查询对象,因为您知道您只是查询用户名。

getJdbcTemplate().queryForObject("select username...", String.class, userName)
@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()));
  }
}
 类似资料:
  • 正在使用Spring MVC创建Restful Web服务... 下面是我的pom.xml: WEB-INF/web.xml: web-inf/mvc-dispatcher-servlet.xml: src/main/resources/database_db.xml: @Repository public class UserDAO{ } SRC/Test/Java: 这就像在print()语句

  • 网络.xml 控制器 当我访问 http://mydomain/car/baojia.html 并出现此错误时: spring.xmlViewResolver 我在/views/boajia.jsp中有一个文件 不管我写什么,都不行 我还有另一个问题,我不想匹配这个 url 模式:/api/*,控制器是: 但它不能工作

  • 我试图理解Spring MVC Restful架构。想知道有多少种方法可以将SpringMVC与RESTfulWeb服务集成在一起。我可以看到其中一个是使用Rest模板。 这是在Spring MVC中使用rest Web服务的唯一方法吗? 我们可以单独使用SpringMVC开发web应用程序而不使用任何web服务吗。 如果我说错了,请指正。 谢谢你的帮助。

  • 本文向大家介绍SpringMVC图片文件跨服务器上传,包括了SpringMVC图片文件跨服务器上传的使用技巧和注意事项,需要的朋友参考一下 图片文件跨服务器上传(我使用的公司云桌面作为上传服务器,自己的笔记本作为存储服务器测试的) 一、上传服务器: 1、pom文件(使用jersey插件) 2、前端页面: 3、后台controller: 二、存储图片文件服务器: 1、Tomcat的conf目录下的w

  • 我正在学习springsecurity(基于java的配置),我无法使注销正常工作。当我点击注销时,我看到URL更改为http://localhost:8080/logout并获取“HTTP 404-/logout”。登录功能工作正常(即使使用自定义登录表单),但问题是注销,我怀疑重定向的url“localhost:8080/logout”应该类似于“localhost:8808/springte

  • 我的样本代码在这里 尝试运行junit测试时,收到以下错误消息。 JAVAlang.IllegalStateException:未能加载ApplicationContext 原因:org。springframework。豆。工厂BeanCreationException:创建名为“nameDao”的bean时出错:调用init方法失败;嵌套的异常是java。lang.IllegalArgument