Note that there are some explanatory texts on larger screens.

plurals
  1. POmapping URLs in Spring PetClinic sample application
    primarykey
    data
    text
    <p>I am working with the master branch of <a href="https://github.com/SpringSource/spring-petclinic/" rel="nofollow"> this version of the spring PetClinic sample application</a>. I added the following method to the OwnerController class: </p> <pre><code>@RequestMapping(value = "/catowners", method = RequestMethod.GET) public String findOwnersOfPetType(Map&lt;String, Object&gt; model) { // find owners of a specific type of pet Integer typeID = 1;//this is just a placeholder Collection&lt;Owner&gt; results = this.clinicService.findOwnerByPetType(typeID); model.put("selections", results); return "owners/catowners"; } </code></pre> <p>I mapped out the flow of control through the various resources in the application, and seem to have the other code changes I made working, so that now the error that comes up seems to be related to URL mapping. </p> <p>When I type the following url in my browser: </p> <pre><code>http://localhost:8080/petclinic/owners/catowners </code></pre> <p>I get a 400 error message stating that: </p> <pre><code>"The request sent by the client was syntactically incorrect." </code></pre> <p>I want the method above to utilize a file called catowners.jsp, which I located at WEB-INF/jsp/owners/catowners.jsp </p> <p>Can anyone show me how to fix the code above so that I am able to type in a reasonable url and get the content that is rendered through catowners.jsp? </p> <p>EDIT: </p> <p>As per lebolo's request, I am including the entire OwnerController class as follows: </p> <pre><code>package org.springframework.samples.petclinic.web; @Controller @SessionAttributes(types = Owner.class) public class OwnerController { private final ClinicService clinicService; @Autowired public OwnerController(ClinicService clinicService) { this.clinicService = clinicService; } @InitBinder public void setAllowedFields(WebDataBinder dataBinder) { dataBinder.setDisallowedFields("id"); } @RequestMapping(value = "/owners/new", method = RequestMethod.GET) public String initCreationForm(Map&lt;String, Object&gt; model) { Owner owner = new Owner(); model.put("owner", owner); return "owners/createOrUpdateOwnerForm"; } @RequestMapping(value = "/owners/new", method = RequestMethod.POST) public String processCreationForm(@Valid Owner owner, BindingResult result, SessionStatus status) { if (result.hasErrors()) { return "owners/createOrUpdateOwnerForm"; } else { this.clinicService.saveOwner(owner); status.setComplete(); return "redirect:/owners/" + owner.getId(); } } @RequestMapping(value = "/owners/find", method = RequestMethod.GET) public String initFindForm(Map&lt;String, Object&gt; model) { model.put("owner", new Owner()); return "owners/findOwners"; } @RequestMapping(value = "/owners", method = RequestMethod.GET) public String processFindForm(Owner owner, BindingResult result, Map&lt;String, Object&gt; model) { // allow parameterless GET request for /owners to return all records if (owner.getLastName() == null) { owner.setLastName(""); // empty string signifies broadest possible search } // find owners by last name Collection&lt;Owner&gt; results = this.clinicService.findOwnerByLastName(owner.getLastName()); if (results.size() &lt; 1) { // no owners found result.rejectValue("lastName", "notFound", "not found"); return "owners/findOwners"; } if (results.size() &gt; 1) { // multiple owners found model.put("selections", results); return "owners/ownersList"; } else { // 1 owner found owner = results.iterator().next(); return "redirect:/owners/" + owner.getId(); } } //'''''''''CodeMed added this next method 9/5/2013 @RequestMapping(value = "/owners/catowners", method = RequestMethod.GET) public String findOwnersOfPetType(Map&lt;String, Object&gt; model) { // find owners of a specific type of pet Integer typeID = 1;//this is just a placeholder Collection&lt;Owner&gt; results = this.clinicService.findOwnerByPetType(typeID); model.put("selections", results); return "owners/catowners"; } //''''''''''''''''''' @RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.GET) public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { Owner owner = this.clinicService.findOwnerById(ownerId); model.addAttribute(owner); return "owners/createOrUpdateOwnerForm"; } @RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.PUT) public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, SessionStatus status) { if (result.hasErrors()) { return "owners/createOrUpdateOwnerForm"; } else { this.clinicService.saveOwner(owner); status.setComplete(); return "redirect:/owners/{ownerId}"; } } @RequestMapping("/owners/{ownerId}") public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { ModelAndView mav = new ModelAndView("owners/ownerDetails"); mav.addObject(this.clinicService.findOwnerById(ownerId)); return mav; } } </code></pre> <p>Typing in the following url: </p> <pre><code>http://localhost:8080/petclinic/owners/catowners </code></pre> <p>Now gives a 404 message as follows: </p> <pre><code>message /petclinic/WEB-INF/jsp/owners/catowners.jsp description The requested resource is not available. </code></pre> <p>However, there is DEFINITELY a file called catowners.jsp located at WEB-INF/jsp/owners/catowners.jsp </p> <p>Any more suggestions? </p> <p>SECOND EDIT: </p> <p>As per Sotirios' question, the following code in mvc-view-config.xml spells out InternalResourceResolver: </p> <pre><code> &lt;property name="viewResolvers"&gt; &lt;list&gt; &lt;!-- Default viewClass: JSTL view (JSP with html output) --&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;!-- Example: a logical view name of 'vets' is mapped to '/WEB-INF/jsp/vets.jsp' --&gt; &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt; &lt;property name="suffix" value=".jsp"/&gt; &lt;/bean&gt; &lt;!-- Used here for 'xml' and 'atom' views --&gt; &lt;bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/&gt; &lt;/list&gt; &lt;/property&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