对于Spring MVC和Eclipse来说,这很奇怪

时间:2016-06-26 05:29:16

标签: java eclipse spring

Eclipse标记的奇怪错误 这是代码

@RequestMapping ("/hello")
public ModelAndView showMessage(@PathVariable(value="name", required=false, defaultValue="World") String name) {

}

所以必需和defaultValue标有错误

  

注释类型PathVariable

未定义所需的属性

EM ??

Spring版本是4.3.0.RELEASE 这是我的进口商品:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

是。当我使用RequestParam时 - 一切正常。但我的片段

@RequestMapping ("/hello")
public ModelAndView showMessage(@RequestParam(value="name", required=false, defaultValue="World") String name) {
    ModelAndView model = new ModelAndView("helloworld");
    model.addObject("name", name);

    return model;
}

打印带有$ {name}的字符串 - 而不是name的值 - 这是以某种方式链接的? 这个参考工作吧

<a href="hello?name=Eric">Click Here</a>

1 个答案:

答案 0 :(得分:0)

value是注释PathVariable Spring提供的唯一属性

org.springframework.web.bind.annotation.PathVariable

因此,您在requireddefaultValue

上收到错误消息

如果您使用支持JAX-RSPathVariable required注释的任何其他defaultValue,请将导入语句更改为正确的注释。

修改

RequestParamrequireddefaultValue,我认为您需要使用RequestParam代替PathVariable才能拥有默认值