Spring MVC Pages HTTP Status 400 and Incorrect URL's

前端 未结 2 1114
予麋鹿
予麋鹿 2021-01-24 09:02

I am experiencing some issues with the application. I have a registration form which is post to another page from the controller this page displays the results of the query fro

相关标签:
2条回答
  • 2021-01-24 09:45

    The problem is you are using relative paths here when you say action="citizen_registration.htm".

    Change it to

    document.getElementById("citizenRegistration").action="/crimeTrack/citizen_registration.htm";
    

    or

    document.getElementById("citizenRegistration").action="../citizen_registration.htm";
    
    0 讨论(0)
  • 2021-01-24 09:59

    1) change the input type to button instead of submit and add an onclick event that will call your submitPage function passing in the action to call

    <input class="button" id="save" type="button" name="user_request" value="Save"     onclick="submitPage('citizen_registration.htm');"/>
    <input class="button" id="update" type="button" name="user_request" value="Update" onclick="submitPage('citizen_registration.htm');"/>
    <input class="button" id="query" type="button" name="user_request" value="Query" onclick="submitPage('citizen_list.htm');"/>
    

    2) change your submitPage function to set the action based on which button was clicked

    function submitPage(action){   
        document.getElementById("citizenRegistration").action = action;
        document.getElementById("citizenRegistration").method = "POST";
        document.getElementById("citizenRegistration").submit();
    }
    

    3) have 2 controller methods, one mapped to save/update, the other mapped to list

    @RequestMapping(value="citizen_list.htm", method = RequestMethod.POST)
    public ModelAndView getCitizenList(@Valid @ModelAttribute Citizens citizen, BindingResult result, ModelMap m, Model model, @RequestParam(value="user_request") String user_request) throws Exception {
    
       try{
            logger.debug("about to preform query");
            //citizenManager.getListOfCitizens(citizen);
            if(citizenManager.getListOfCitizens(citizen).isEmpty()){
                 model.addAttribute("icon","ui-icon ui-icon-circle-close");
                 model.addAttribute("results","Notice: Query Caused No Records To Be Retrived!");                            
            }else{
                 model.addAttribute("citizens",citizenManager.getListOfCitizens(citizen));
                 return new ModelAndView("citizenList"); 
            } 
        }catch(Exception e){
            logger.error("Exception in CitizenRegistrationController - ModelAndView loadPage "+e);
            //request.setAttribute("error",e.getMessage());
             return new ModelAndView("citizen_registration");
        }
    }
    
    @RequestMapping(value="citizen_registration.htm", method = RequestMethod.POST)
    public ModelAndView handleRequest(@Valid @ModelAttribute Citizens citizen,  BindingResult result, ModelMap m, Model model, @RequestParam(value="user_request") String user_request) throws Exception {
    
        try{
             logger.debug("In Http method for CitizenRegistrationController - Punishment Registration");
             logger.debug("User Request Is " + user_request);
    
             if(result.hasErrors()){
                logger.debug("Has Errors");
                return new ModelAndView("citizen_registration");
             }else{
                 //check if its a save of an update
                 if(user_request.equals("Save")){
                     citizenManager.RegisterCitizen(citizen);   
                     model.addAttribute("icon","ui-icon ui-icon-circle-check");
                     model.addAttribute("results","Record Was Saved");
                     return new ModelAndView("citizen_registration");
                 } else if (user_request.equals("Update")){
                     logger.info("About to do update");
                     citizenManager.UpdateCitizen(citizen);
                     return new ModelAndView("citizen_registration");                        
                 }                  
             }
    
            logger.debug("Has No Errors");     
            return new ModelAndView("citizen_registration");
    
        }catch(Exception e){
            logger.error("Exception in CitizenRegistrationController - ModelAndView loadPage "+e);
            //request.setAttribute("error",e.getMessage());
             return new ModelAndView("citizen_registration");
        }
    }
    
    0 讨论(0)
提交回复
热议问题