Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring MVC Pages HTTP Status 400 and Incorrect URL's
    primarykey
    data
    text
    <p>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 from the registration form. On the results page i select a record and it returns me with the data to the registration page. The user should be able to update the record once its returned or execute a query again.</p> <p>The problems i am having is when the user is on the registration form and executes a query they are posted to the results page the results page is displayed however the url does not change. The registration url is <code>http://localhost:8084/crimeTrack/citizen_registration.htm</code> when posted to the results page by clicking the query button the url is still <code>http://localhost:8084/crimeTrack/citizen_registration.htm</code> when a record is clicked/selected on the results page (which has several records) the user is posted back to the registration page with the selected record and it is displayed for the user to now execute an update or a query again, the url is <code>http://localhost:8084/crimeTrack/getCitizen/1985121244.htm</code> and the user is now on the registration page.</p> <p>If i click query/update again i am getting an <strong>HTTP 400 error</strong> and the url is reading <code>http://localhost:8084/crimeTrack/getCitizen/citizen_registration.htm/</code> and this is not a valid URL mapping in the Controller. I think the url should be <code>http://localhost:8084/crimeTrack/citizen_registration.htm</code> when the registration page is requested. I am not sure when the POST from the results page takes the user back to the registration page is the url should be <code>http://localhost:8084/crimeTrack/getCitizen/1985121244.htm</code> the attached number is the citizen number. Under is my code i am not sure if i am doing these calls correctly and i would like an explanation for the results i am getting as well as a solution to the issues experienced;</p> <p>Pages are submitted using <strong>jquery</strong>:</p> <p>This is an example for the registration page and all other pages follow the same pattern</p> <p><strong>JScript</strong> </p> <pre><code>function submitPage(){ document.getElementById("citizenRegistration").action="citizen_registration.htm"; //document.getElementById("citizenRegistration").target="_self"; document.getElementById("citizenRegistration").method = "POST"; document.getElementById("citizenRegistration").submit(); } </code></pre> <p><strong>citizen_registration.jsp</strong></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0//EN"&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;title&gt;Citizen Registration&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="tab1" class="divGroup"&gt; &lt;form:form id="citizenRegistration" name ="citizenRegistration" commandName="citizens"&gt; ........................ &lt;div class="buttons"&gt; &lt;ol&gt; &lt;li&gt;&lt;input class="button" id="save" type="submit" name= "user_request" value="Save"/&gt; &lt;input class="button" id="update" type="submit" name= "user_request" value="Update"/&gt; &lt;input class="button" id="query" type="submit" name= "user_request" value="Query"/&gt; &lt;/li&gt; &lt;/form:form&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>citizenList.jsp</strong></p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; function submitPage(socialSecurityNumber){ document.getElementById("citizenList").action="getCitizen/1985121244.htm";//harded coded for testing //document.getElementById("citizenList").target="_self"; document.getElementById("citizenList").method = "POST"; document.getElementById("citizenList").submit(); } function GetCitizenTypeDescription(citizenTypeId){ $.ajax({ type:'GET', url:'getCitizenTypeDescription.htm', data:{citizenTypeId:citizenTypeId}, dataType: 'text', success: function (data) { $('.citizenTypeId').each(function(i){ if($(this).val() === citizenTypeId){ //finds parent div var parent = $(this).parent(); //search for child element wit class name citizenTypeDesc var thisCitizenTypeDesc = parent.children('.citizenTypeDesc'); thisCitizenTypeDesc.text(data); } }); } }); } &lt;title&gt;Citizen Search Results&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form:form id="citizenList" name ="citizenList"&gt; &lt;div id ="content"&gt; &lt;c:forEach items="${citizens}" var="citizen"&gt; &lt;div id="table"&gt; &lt;div&gt; &lt;p&gt;&lt;canvas class="canvas" height="240" width="320"&gt;&lt;/canvas&gt; &lt;/div&gt; &lt;label class="citizenTypeDesc"&gt;&lt;/label&gt;&lt;/br&gt; &lt;a class="socialSecurityNumber" href="${citizen.socialSecurityNumber}"&gt;${citizen.fName} ${citizen.lName}&lt;/a&gt; &lt;input type="hidden" id="photo" value="${citizen.photo}" class="photos"/&gt; &lt;input type="hidden" id="socialSecurityNumber" value="${citizen.socialSecurityNumber}" /&gt; &lt;input type="hidden" class="citizenTypeId" value="${citizen.citizenTypeId}"/&gt; &lt;/div&gt; &lt;/c:forEach&gt; &lt;/div&gt; &lt;/form:form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>CitizenRegistrationController.java</strong></p> <pre><code>@Controller public class CitizenRegistrationController { private final Logger logger = Logger.getLogger(getClass()); @Autowired private CitizenTypeManager citizenTypeManager; ............ Map&lt;String, Object&gt; myCitizenType = new HashMap&lt;String, Object&gt;(); ....... @InitBinder("citizens") 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()); binder.setValidator(citizenRegistrationValidation); binder.registerCustomEditor(Integer.class,new CustomIntEditor()); } @RequestMapping(value="citizen_registration.htm", method = RequestMethod.GET) public ModelAndView loadPage(@ModelAttribute Citizens citizen, BindingResult result, ModelMap m, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception { try{ logger.debug("In Http method for CitizenRegistrationController"); myCitizenType.put("citizenTypeList", this.citizenTypeManager.getCitizenType()); myGender.put("genderList", this.genderManager.getGenderList()); ...... return new ModelAndView("citizen_registration"); }catch(Exception e){ logger.error("Exception in CitizenRegistrationController - ModelAndView loadPage "+e); request.setAttribute("error",e.getMessage()); return new ModelAndView("error_page"); } } @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("Query")){ 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"); } }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"); } } @RequestMapping(value="getCitizen/{socialSecurityNumber}.htm", method = RequestMethod.POST) public ModelAndView getCitizen(@PathVariable Integer socialSecurityNumber,@ModelAttribute Citizens citizen, BindingResult result,ModelMap m,Model model,HttpServletRequest request, HttpServletResponse response) { try { model.addAttribute("citizens",citizenManager.getCitizen(socialSecurityNumber)); //model.addAttribute("citizens",citizenManager.getCitizen(socialSecurityNumber)); } catch (Exception e) { logger.error("Exception in CitizenRegistrationController - ModelAndView getCitizen "+e); } return new ModelAndView("citizen_registration"); } @RequestMapping(value="getCitizenTypeDescription.htm", method=RequestMethod.GET) public @ResponseBody String citizenTypeDescription(@RequestParam Integer citizenTypeId)throws Exception{ String data = "No Data Found"; try{ data = citizenTypeManager.getCitizenTypeDescription(citizenTypeId); }catch(Exception e){ data = e.getMessage(); logger.error("Exception In getCitizenTypeDescription.htm error : " + e); } return data; } //setter methods /** * @param citizenTypeManager the citizenTypeManager to set */ public void setCitizenTypeManager(CitizenTypeManager citizenTypeManager) { this.citizenTypeManager = citizenTypeManager; } ................................ } </code></pre> <p><strong>Edit</strong></p> <p>I tried using <code>return new ModelAndView("redirect:/citizenList.htm");</code> in the controller when the user clicks query however i am getting <code>404 Not Found - http://localhost:8084/crimeTrack/citizenList.htm"</code></p> <p><strong>Servlet.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans/spring-context-3.0.xsd"&gt; &lt;!-- __________________________________________________________________________________________________ --&gt; &lt;!-- Supports annotations and allows the use of @Controller, @Required, @RequestMapping --&gt; &lt;context:annotation-config/&gt; &lt;context:component-scan base-package="com.crimetrack.business"/&gt; &lt;context:component-scan base-package="com.crimetrack.jdbc"/&gt; &lt;context:component-scan base-package="com.crimetrack.service"/&gt; &lt;context:component-scan base-package="com.crimetrack.web" /&gt; &lt;mvc:annotation-driven /&gt; &lt;mvc:resources mapping="/resources/**" location="/public-resources/"/&gt; &lt;!-- __________________________________________________________________________________________________ --&gt; &lt;!-- Forwards requests to the "/" resource to the "login" view --&gt; &lt;mvc:view-controller path="/login" view-name="login"/&gt; &lt;!-- Forwards requests to the "/" resource to the "officer_registration" view --&gt; &lt;mvc:view-controller path="/officer_registration" view-name="officer_registration"/&gt; &lt;!-- Forwards requests to the "/" resource to the "citizenList" view --&gt; &lt;mvc:view-controller path="/citizenList" view-name="citizenList"/&gt; &lt;!-- Forwards requests to the "/" resource to the "citizen_registration" view --&gt; &lt;mvc:view-controller path="/citizen_registration" view-name="citizen_registration"/&gt; &lt;!-- __________________________________________________________________________________________________ --&gt; &lt;!-- &lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/&gt; --&gt; &lt;!-- Is used to process method level annotations --&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/&gt; &lt;!-- __________________________________________________________________________________________________ --&gt; &lt;!-- &lt;bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/&gt; --&gt; &lt;bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"&gt; &lt;property name="basename" value="messages"/&gt; &lt;/bean&gt; &lt;bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /&gt; &lt;!-- __________________________________________________________________________________________________ --&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"&gt;&lt;/property&gt; &lt;property name="prefix" value="/WEB-INF/jsp/"&gt;&lt;/property&gt; &lt;property name="suffix" value=".jsp"&gt;&lt;/property&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload