0

我正在使用这个版本的 spring PetClinic 示例应用程序的主分支。我在 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

谁能告诉我如何修复上面的代码,以便我能够输入合理的 url 并获取通过 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>
4

1 回答 1

2

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

如果您使用的版本没有类级别@RequestMapping(例如这个),那么您的方法级别映射应该是

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

因为您的网址(发布宠物诊所应用程序上下文)是/owners/catowners

于 2013-09-06T00:10:35.080 回答