Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带变量的springmvc重定向_Spring_Spring Mvc_Redirect_Spring Data - Fatal编程技术网

带变量的springmvc重定向

带变量的springmvc重定向,spring,spring-mvc,redirect,spring-data,Spring,Spring Mvc,Redirect,Spring Data,我有以下Spring MVC 3.2.4方法: @RequestMapping(value=“/products/{product}”,method=RequestMethod.POST) 公共字符串更新(Product Product,@Valid@ModelAttribute(“productForm”)productForm productForm,BindingResult BindingResult,Model Model){ if(bindingResult.hasErrors())

我有以下Spring MVC 3.2.4方法:

@RequestMapping(value=“/products/{product}”,method=RequestMethod.POST)
公共字符串更新(Product Product,@Valid@ModelAttribute(“productForm”)productForm productForm,BindingResult BindingResult,Model Model){
if(bindingResult.hasErrors()){
返回“产品/视图”;
}
map(productForm,product);
productService.saveProduct(产品);
返回“重定向:/products/{product}”;
}
成功后,它应该将用户重定向回产品的详细信息。问题是,我没有重定向到页面“/products/1”,而是被重定向到页面“/products/Product[code=1234567890,name=Nejaky]”。占位符{product}似乎被product.toString()替换,而不是URL中的原始ID。 我使用内置的弹簧数据转换器:



我应该怎么做才能使它正常工作并将我重定向回“/products/1”,而不执行诸如“redirect:/product”+product.getId()之类的操作?

我们的故事从
RedirectView
源代码中的
replaceUriTemplateVariables
方法开始

protected StringBuilder replaceUriTemplateVariables(
        String targetUrl, Map<String, Object> model, Map<String, String> currentUriVariables, String encodingScheme)
        throws UnsupportedEncodingException {

    StringBuilder result = new StringBuilder();
    Matcher m = URI_TEMPLATE_VARIABLE_PATTERN.matcher(targetUrl);
    int endLastMatch = 0;
    while (m.find()) {
        String name = m.group(1);
        Object value = model.containsKey(name) ? model.remove(name) : currentUriVariables.get(name);
        Assert.notNull(value, "Model has no value for '" + name + "'");
        result.append(targetUrl.substring(endLastMatch, m.start()));
        result.append(UriUtils.encodePathSegment(value.toString(), encodingScheme));
        endLastMatch = m.end();
    }
    result.append(targetUrl.substring(endLastMatch, targetUrl.length()));
    return result;
}
添加名为
“productId”
的模型属性,并在视图名称中使用该属性

model.addAttribute("productId", product.getId());
"redirect:/product/{productId}"

或者使用uri变量。我还没有这些方面的信息。

我们的故事从
重定向视图
源代码开始,方法是
replaceUriTemplateVariables

protected StringBuilder replaceUriTemplateVariables(
        String targetUrl, Map<String, Object> model, Map<String, String> currentUriVariables, String encodingScheme)
        throws UnsupportedEncodingException {

    StringBuilder result = new StringBuilder();
    Matcher m = URI_TEMPLATE_VARIABLE_PATTERN.matcher(targetUrl);
    int endLastMatch = 0;
    while (m.find()) {
        String name = m.group(1);
        Object value = model.containsKey(name) ? model.remove(name) : currentUriVariables.get(name);
        Assert.notNull(value, "Model has no value for '" + name + "'");
        result.append(targetUrl.substring(endLastMatch, m.start()));
        result.append(UriUtils.encodePathSegment(value.toString(), encodingScheme));
        endLastMatch = m.end();
    }
    result.append(targetUrl.substring(endLastMatch, targetUrl.length()));
    return result;
}
添加名为
“productId”
的模型属性,并在视图名称中使用该属性

model.addAttribute("productId", product.getId());
"redirect:/product/{productId}"

或者使用uri变量。我还没有关于这些的信息。

好的,终于找到了原因。我必须用@PathVariable注释产品参数。想知道没有它它它还能工作。

好的,终于找到了原因。我必须用@PathVariable注释产品参数。想知道没有它它还能工作。

您的模型中是否有一个名为“product”的属性,可能是通过ModelAttribute注释的方法?您的模型中是否有一个名为“product”的属性,可能是通过ModelAttribute注释的方法?在这种特殊情况下,可能是,因为product对象最终出现在模型中,然后在替换字符串时使用它,但一般来说,您甚至不必在替换{product}的处理程序方法中使用product作为参数,就可以在重定向字符串中工作,因为product对象最终出现在模型中,然后在替换字符串时使用它,但一般来说,您甚至不必在替换{product}的处理程序方法中使用product作为参数,就可以在重定向字符串中工作。