在Spring PetClinic示例应用程序中映射URL

时间:2013-09-06 00:07:01

标签: java spring spring-mvc

我正在使用 this version of the spring PetClinic sample application的主分支。我将以下方法添加到OwnerController类:

@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(Map<String, Object> model) {
    // find owners of a specific type of pet
    Integer typeID = 1;//this is just a placeholder
    Collection<Owner> results = this.clinicService.findOwnerByPetType(typeID);
    model.put("selections", results);
    return "owners/catowners";
 }  

我通过应用程序中的各种资源绘制了控制流,并且似乎有其他代码更改我工作,所以现在出现的错误似乎与URL映射有关。

当我在浏览器中输入以下网址时:

http://localhost:8080/petclinic/owners/catowners  

我收到400错误消息,说明:

"The request sent by the client was syntactically incorrect."  

我希望上面的方法使用一个名为catowners.jsp的文件,我位于WEB-INF / jsp / owners / catowners.jsp

有人能告诉我如何修复上面的代码,以便我能够输入一个合理的网址并获取通过catowners.jsp呈现的内容吗?

编辑:

根据lebolo的请求,我将包括整个OwnerController类,如下所示:

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<String, Object> 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<String, Object> model) {
        model.put("owner", new Owner());
        return "owners/findOwners";
    }

    @RequestMapping(value = "/owners", method = RequestMethod.GET)
    public String processFindForm(Owner owner, BindingResult result, Map<String, Object> 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<Owner> results = this.clinicService.findOwnerByLastName(owner.getLastName());
        if (results.size() < 1) {
            // no owners found
            result.rejectValue("lastName", "notFound", "not found");
            return "owners/findOwners";
        }
        if (results.size() > 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<String, Object> model) {
        // find owners of a specific type of pet
        Integer typeID = 1;//this is just a placeholder
        Collection<Owner> 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;
    }
}

输入以下网址:

http://localhost:8080/petclinic/owners/catowners  

现在给出一条404消息如下:

message /petclinic/WEB-INF/jsp/owners/catowners.jsp  
description The requested resource is not available.  

但是,肯定有一个名为catowners.jsp的文件位于WEB-INF / jsp / owners / catowners.jsp

还有其他建议吗?

第二次编辑:

根据Sotirios的提问,mvc-view-config.xml中的以下代码列出了InternalResourceResolver:

    <property name="viewResolvers">
        <list>
            <!-- Default viewClass: JSTL view (JSP with html output) -->
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <!-- Example: a logical view name of 'vets' is mapped to '/WEB-INF/jsp/vets.jsp' -->
              <property name="prefix" value="/WEB-INF/jsp/"/>
              <property name="suffix" value=".jsp"/>
          </bean>
          <!-- Used here for 'xml' and 'atom' views  -->
          <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
        </list>
    </property>

1 个答案:

答案 0 :(得分:2)

我不确定您使用的是哪个版本的OwnerController。因此,如果您发布整个班级(或至少是班级的签名,例如班级的任何注释),它可能会有所帮助。

如果您使用的版本没有班级@RequestMapping(例如this),那么您的方法级别映射应为

@RequestMapping(value = "/owners/catowners", method = RequestMethod.GET)

因为您的网址(发布了petclinic app上下文)为/owners/catowners