Java Spring MVC 3.0 Rest问题

Java Spring MVC 3.0 Rest问题,java,spring-mvc,rest,Java,Spring Mvc,Rest,我第一次试用SpringMVC3.0,我想让它恢复完整 这是我的控制器: @Controller @RequestMapping(value = "/product") @SessionAttributes("product") public class ProductController { @Autowired private ProductService productService; public void setProductValidator(Pr

我第一次试用SpringMVC3.0,我想让它恢复完整

这是我的控制器:

    @Controller
@RequestMapping(value = "/product")
@SessionAttributes("product")
public class ProductController {

    @Autowired
    private ProductService productService;

    public void setProductValidator(ProductValidator productValidator, ProductService productService) {
        this.productService = productService;
    }

    @RequestMapping(method = RequestMethod.GET)
    public Product create() {
        //model.addAttribute(new Product());
        return new Product();
    }

    @RequestMapping(method = RequestMethod.POST)
    public String create(@Valid Product product, BindingResult result) {
        if (result.hasErrors()) {
            return "product/create";
        }
        productService.add(product);
        return "redirect:/product/show/" + product.getId();
    }

    @RequestMapping(value = "/show/{id}", method = RequestMethod.GET)
    public Product show(@PathVariable int id) {
        Product product = productService.getProductWithID(id);
        if (product == null) {
            //throw new ResourceNotFoundException(id);
        }
        return product;
    }

    @RequestMapping(method = RequestMethod.GET)
    public List<Product> list()
    {
        return productService.getProducts();
    }

}
@控制器
@请求映射(value=“/product”)
@会期贡献(“产品”)
公共类产品控制器{
@自动连线
私人产品服务;
public void setProductValidator(ProductValidator ProductValidator,ProductService ProductService){
this.productService=productService;
}
@RequestMapping(method=RequestMethod.GET)
公共产品创建(){
//model.addAttribute(新产品());
退回新产品();
}
@RequestMapping(method=RequestMethod.POST)
创建公共字符串(@Valid Product,BindingResult){
if(result.hasErrors()){
返回“产品/创建”;
}
productService.add(产品);
返回“重定向:/product/show/”+product.getId();
}
@RequestMapping(value=“/show/{id}”,method=RequestMethod.GET)
公共产品展示(@PathVariable int-id){
Product=productService.getProductWithID(id);
如果(产品==null){
//抛出新的ResourceNotFoundException(id);
}
退货产品;
}
@RequestMapping(method=RequestMethod.GET)
公开名单()
{
return productService.getProducts();
}
}
关于这一点,我有两个问题

我相信约定优先于配置,因此我的视图位于jsp/product/folder中,称为create.jsp、list.jsp和show.jsp。在添加@PathVariable属性之前,这些视图工作得比较好。当我点击root/product/show/1时,出现以下错误: ../jsp/product/show/1.jsp“未找到”如何告诉此方法使用show.jsp视图


如果我不在类级别添加RequestMapping,我的show方法将映射到root/show,而不是root/owner/show。我想避免使用类级别的RequestMapping。

将您的“产品”添加到
模型
,并在
show.jsp中返回
字符串/product/show
,而不是
产品
,您可以访问产品对象窗体
pageContext

查看手册中有关“”的部分

基本上,当您的
@RequestMapping
方法只返回一个对象时,Spring将其用作单个模型属性,并且,我猜,尝试使用请求URL作为视图名称的基础


从同一方法返回所需视图和数据的最简单方法可能是让该方法返回一个
ModelAndView
,这样您就可以显式地指定视图名称和模型数据。

我知道我可以使用返回类型字符串,但我不希望这样做。我希望使用现在的返回类型。应该可以因为它适用于所有其他方法。感谢我上面提到的快速回复思想,我知道有各种不同的方法来解决这个问题。但是我确实喜欢使用我当前的方法,并且喜欢使用当前的方法/返回类型约定来解决问题。我当前的返回类型约定应该可以解决这个问题n而不是void/String/Model/ModelAndView返回类型。感谢您的回复,我知道您的解决方案。但我担心这就是带注释的Spring MVC希望您通过方法的返回类型返回viewName的方式。create()方法的返回类型为Product,但与list()一样使用create.jsp方法使用list.jsp,那么为什么show()方法不能使用show.jsp呢?我很感谢您的时间,我是Spring的新手,但是当其他两个示例都起作用时,我不明白show()为什么不起作用。其他方法“起作用”“因为当您不指定逻辑视图名称时,DispatcherServlet默认使用RequestToViewNameTranslator,该Translator只需删除请求URL的最后一部分即可形成视图名称(“/root/owner/list”->“list”,“root/owner/show/1”->“1”)。要使用默认转换器将确定的视图名称以外的视图名称(可能90%的时间是这样),您需要自己指定视图名称。您只需更改返回ModelAndView的方法、设置viewName并将产品添加到模型中,其他所有操作都将保持不变。
ModelAndView
只是viewName和模型数据的简单持有者。它起源于MVC的预注释版本;控制器必须返回ModelAndView对象(因为控制器负责确定视图和模型,而方法不能返回两件事)。我对使用新注释不是很有经验,但我相信选择可以归结为风格,或者方法需要返回多少模型数据。从功能上讲,如果您仍然可以使用
@modeldattribute
s指定其他模型数据,那么它在功能上应该是相同的。