1

我正在尝试构建一个 RESTFUL Web 服务,但是在我的代码下出现错误:

jsp

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

    <style>
        <%@ include file="../css/forms.css" %>
    </style>
    <script type="text/javascript">
        <%@ include file="../js/off_reg.js"%>


        $(document).ready(function(){

            $('#userName').blur(function(evt){


                CheckAvailability();                    
            });
        });         

        function CheckAvailability(){

            $.ajax( {
                  type:'GET', //Could be 'get' depending on your needs
                  url:'validateUserName.htm',
                  data:{userName:$('#userName').val()},
                  dataType: 'json',
                  success:function(data) {
                    alert(data);
                  }
                });

    }



    </script>

public boolean OfficerExist(String userName){


    try{

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

        String sql = "SELECT userName FROM crimetrack.tblofficers WHERE userName = ?";

        logger.info("at this point 1");

        //String dbUserName = (String)results.get("userName");

        String dbUserName = (String)getJdbcTemplate().queryForObject(sql, new Object[]{userName},String.class);


        logger.info("after JdbcTemplate");

        if (dbUserName.equals(userName)) {

            logger.info("User Name Exists");
            return true;

        }else{
            logger.info("User Name Does NOT Exists");
            return false;
        }


    }catch(Exception e){

        logger.info(e.getMessage());
        return false;
    }
}

FireBug 控制台错误:

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

500 Internal Server Error
    70ms    
jquery.min.js (line 2)

"NetworkError: 500 Internal Server Error - http://localhost:8084/crimeTrack            /validateUserName.htm?userName=hello"

汤姆猫错误日志:

    com.crimetrack.web.OfficerRegistrationController.validateUserName(java.lang.String,com.crimetrack.business.Officers,org.springframework.validation.BindingResult,org.springframework.ui.ModelMap) throws java.lang.Exception
306270 [http-8084-1] DEBUG com.crimetrack.web.OfficerRegistrationController  - Inside Controller validateUserName
306270 [http-8084-1] DEBUG com.crimetrack.web.OfficerRegistrationController  - The user name that came in is hello
306270 [http-8084-1] INFO  com.crimetrack.service.ValidateUserNameManager  - Inside Do UserNameExist
306270 [http-8084-1] DEBUG com.crimetrack.jdbc.JdbcOfficersDAO  - About to check if officers existing
306270 [http-8084-1] INFO  com.crimetrack.jdbc.JdbcOfficersDAO  - User Name Found 1
306270 [http-8084-1] INFO  com.crimetrack.jdbc.JdbcOfficersDAO  - User Name Found 2
306270 [http-8084-1] DEBUG org.springframework.jdbc.core.JdbcTemplate  - Executing prepared SQL query
306270 [http-8084-1] DEBUG org.springframework.jdbc.core.JdbcTemplate  - Executing prepared SQL statement [SELECT userName FROM crimetrack.tblofficers WHERE userName = ?]
306270 [http-8084-1] DEBUG org.springframework.jdbc.datasource.DataSourceUtils  - Fetching JDBC Connection from DataSource
306285 [http-8084-1] DEBUG org.springframework.jdbc.core.StatementCreatorUtils  - Setting SQL statement parameter value: column index 1, parameter value [hello], value class [java.lang.String], SQL type unknown
306289 [http-8084-1] DEBUG org.springframework.jdbc.datasource.DataSourceUtils  - Returning JDBC Connection to DataSource
306289 [http-8084-1] INFO  com.crimetrack.jdbc.JdbcOfficersDAO  - Incorrect result size: expected 1, actual 0
306289 [http-8084-1] DEBUG org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver  - Resolving exception from handler [com.crimetrack.web.OfficerRegistrationController@a32cdd]: java.lang.IllegalArgumentException: Invalid handler method return value: false
306289 [http-8084-1] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver  - Resolving exception from handler [com.crimetrack.web.OfficerRegistrationController@a32cdd]: java.lang.IllegalArgumentException: Invalid handler method return value: false
306289 [http-8084-1] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver  - Resolving exception from handler [com.crimetrack.web.OfficerRegistrationController@a32cdd]: java.lang.IllegalArgumentException: Invalid handler method return value: false
306290 [http-8084-1] DEBUG org.springframework.web.servlet.DispatcherServlet  - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@1cbab62
306290 [http-8084-1] DEBUG org.springframework.web.servlet.DispatcherServlet  - Could not complete request
java.lang.IllegalArgumentException: Invalid handler method return value: false
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.getModelAndView(AnnotationMethodHandlerAdapter.java:971)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:438)
    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)
306290 [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=[28ms]; status=[failed: java.lang.IllegalArgumentException: Invalid handler method return value: false]
306290 [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=[28ms]; status=[failed: java.lang.IllegalArgumentException: Invalid handler method return value: false]

官员注册控制器

package com.crimetrack.web;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.crimetrack.business.Login;
import com.crimetrack.business.Officers;
import com.crimetrack.service.DivisionManager;
import com.crimetrack.service.GenderManager;
import com.crimetrack.service.OfficerRegistrationValidation;
import com.crimetrack.service.PositionManager;
import com.crimetrack.service.ValidateUserNameManager;

@Controller
public class OfficerRegistrationController {

    private final Logger logger = Logger.getLogger(getClass());
    private DivisionManager divisionManager;
    private PositionManager positionManager;
    private GenderManager genderManager;
    private Officers officer = new Officers();

    private ValidateUserNameManager validateUserNameManager;

    Map<String, Object> myDivision = new HashMap<String, Object>();
    Map<String, Object> myPosition = new HashMap<String, Object>();
    Map<String, Object> myGender = new HashMap<String, Object>();



    OfficerRegistrationValidation validateData = new OfficerRegistrationValidation();

    @InitBinder("officers")
    protected void initBinder(WebDataBinder binder){
        //removes white spaces 
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));

        //formats date 
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        //By passing true this will convert empty strings to null
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        dateFormat.setLenient(false);

        binder.setValidator(new OfficerRegistrationValidation());

    }


     @RequestMapping(value="officer_registration.htm", method = RequestMethod.GET)
        public ModelAndView loadPage(HttpServletRequest request,
                HttpServletResponse response,@ModelAttribute Officers officer, BindingResult result, ModelMap m, Model model) throws Exception {     


         try{            

             logger.debug("In Http method for OfficerRegistrationController");


                myDivision.put("divisionList", this.divisionManager.getDivisions());


                myPosition.put("positionList", this.positionManager.getPositionList());


                myGender.put("genderList", this.genderManager.getGenderList());

                model.addAttribute("division", myDivision);
                model.addAttribute("position", myPosition);
                model.addAttribute("gender", myGender);

             return new ModelAndView("officer_registration");            


         }catch(Exception e){

            request.setAttribute("error",e.getMessage());
            return new ModelAndView("error_page");           
         }       
     }

     @RequestMapping(value="officer_registration.htm", method=RequestMethod.POST)
     public ModelAndView handleRequest(@Valid @ModelAttribute Officers officer, BindingResult result, ModelMap m, Model model)throws Exception{

         if(result.hasErrors()){

             model.addAttribute("division", myDivision);
             model.addAttribute("position", myPosition);
             model.addAttribute("gender", myGender);
             return new ModelAndView("officer_registration");

        }else{

            return null;
        }   

     }

     @RequestMapping(value="validateUserName.htm", method=RequestMethod.GET)
     public boolean validateUserName(@RequestParam String userName, @ModelAttribute Officers officer, BindingResult result, ModelMap m)throws Exception{


         try{

             logger.debug("Inside Controller validateUserName");
             logger.debug("The user name that came in is " + userName);

            if (validateUserNameManager.DoesUserNameExist(userName)== true){

                 return true;
             }

                 return false; 

            // return true;


         }catch(Exception e){

             logger.debug("Error in validateUserName Controller " + e.getMessage());
             return false;

         }


     }



     public void setDivisionManager(DivisionManager divisionManager){

         this.divisionManager = divisionManager;
     }

     public void setPositionManager(PositionManager positionManager){

         this.positionManager = positionManager;

     }

     public void setGenderManager(GenderManager genderManager){

         this.genderManager = genderManager;
     }



    /**
     * @return the validateUserNameManager
     */
    public ValidateUserNameManager getValidateUserNameManager() {
        return validateUserNameManager;
    }

    /**
     * @param validateUserNameManager the validateUserNameManager to set
     */
    public void setValidateUserNameManager(
            ValidateUserNameManager validateUserNameManager) {
        this.validateUserNameManager = validateUserNameManager;
    }




    /**
     * @return the officer
     */
    public Officers getOfficer() {
        return officer;
    }



    /**
     * @param officer the officer to set
     */
    public void setOfficer(Officers officer) {
        this.officer = officer;
    }


}
4

2 回答 2

2

根据 Spring 的@RequestMapping文档:

任何其他返回类型将被视为向视图公开的单个模型属性,使用在方法级别通过 ModelAttribute 指定的属性名称(或基于返回类型的类名称的默认属性名称)。该模型将隐含地丰富命令对象和 ModelAttribute 注释的参考数据访问器方法的结果。

因为这个方法在一个官员对象上有 ModelAttribute 注释,所以返回类型应该是一个官员对象。

您可能希望此方法返回 void,并自己写入响应对象。(为此,您需要在方法中添加一个 HttpServletResponse 参数。)

或者,您可以返回 JSON 视图。见spring:从控制器返回 JSON 作为 ModelAndVIew

例如:

public View validateUserName(@RequestParam String userName, ModelMap m) {

    // Do validation

    m.addAttribute("isValid", validationResult);

    return new MappingJacksonJsonView();

}
于 2012-09-20T20:38:17.070 回答
1

看来您正在运行此查询:

SELECT userName FROM crimetrack.tblofficers WHERE userName = 'hello'

查询运行成功,但是表中没有匹配的记录userName = 'hello',所以返回一个空的结果集。然后创建官员失败,因为没有要创建的官员并且没有处理该条件。

独立运行查询并确认没有这样的记录。然后查看您的映射代码,看看应该如何处理这种情况。

跟进... JdbcTemplate.queryForObject状态的java文档:

此方法对于运行具有已知结果的静态 SQL 很有用。该查询预计为单行/单列查询;返回的结果会直接映射到对应的对象类型。

...

抛出:IncorrectResultSizeDataAccessException - 如果查询没有准确返回一行,或者没有准确返回该行中的一列

因此,如果您不确定数据库中的记录,则不应调用它,否则该行:

String dbUserName = (String)getJdbcTemplate().queryForObject(sql, new Object[]{userName},String.class);

需要用 try-catch 包装来处理这种情况。

于 2012-09-19T22:53:04.937 回答