将前缀附加到Annotated值,并在Overrided方法中使用Java Spring Framework中的另一个Annotated值

时间:2017-02-22 05:49:44

标签: java spring-mvc interface annotations mapping

我正在研究Spring MVC框架,我使用注释来映射控制器我在每个控制器类中使用一些常见的映射字和常用方法。

example:
@RequestMapping(value = "/employee_job/list")
@RequestMapping(value = "/employee_job/list_json")
@RequestMapping(value = "/employee_job/add")
@RequestMapping(value = "/employee_job/update")
@RequestMapping(value = "/employee_job/delete")

这是employee_job控制器。 所以我正在创建一个带有部分映射的接口,实现者会在该值中附加前缀。 这是接口代码:

@Controller
public interface CommonController {

    @RequestMapping(value = "/list")
    public String showList(Model model);
    @RequestMapping(value = "/list_json")
    public String showListJson(Model model);
    @RequestMapping(value = "/add")
    public String add(Model model);
    @RequestMapping(value = "/update")
    public String update(Model model);
    @RequestMapping(value = "/delete")
    public String delete(Model model);    
}

我想像这样实现它:

public class EmployeeJobController implements CommonController{

    private static final String prefix = "/employee_job";

        @Override
        @RequestMapping(value = prefix+value)
        public String showList(Model model){
            //some code here
        }

        @Override
        @RequestMapping(value = prefix+value)
        public String showListJson(Model model){
            //some code here
        }

        @Override
        @RequestMapping(value = prefix+value)
        public String add(Model model){
            //some code here
        }

        @Override
        @RequestMapping(value = prefix+value)
        public String update(Model model){
            //some code here
        }

        @Override
        @RequestMapping(value = prefix+value)
        public String delete(Model model){
            //some code here
        }
    }

但我收到错误"value cannot be resolved to a variable"

有什么方法可以做到这一点?

感谢。

2 个答案:

答案 0 :(得分:0)

您在哪里定义value

在注释中,您只能使用static final字符串。像prefix+"/add"这样的东西应该可以工作

更好的解决方案是

@RequestMapping("/employee_job")
public class EmployeeJobController  {
    @RequestMapping("/add")
    public String add(...) {...}
    ...
}

答案 1 :(得分:-1)

在CommonController接口中声明变量。在EmployeeJobController类中使用这些变量。

@Controller public interface CommonController {

public static final String showListStr="/list";

@RequestMapping(value = "/list")
public String showList(Model model);
@RequestMapping(value = "/list_json")
public String showListJson(Model model);
@RequestMapping(value = "/add")
public String add(Model model);
@RequestMapping(value = "/update")
public String update(Model model);
@RequestMapping(value = "/delete")
public String delete(Model model);    

}

并在子类中使用此变量

公共类EmployeeJobController实现CommonController {

private static final String prefix = "/employee_job";

    @Override
    @RequestMapping(value = prefix+showListStr)
    public String showList(Model model){
        //some code here
    }

    @Override
    @RequestMapping(value = prefix+value)
    public String showListJson(Model model){
        //some code here
    }

    @Override
    @RequestMapping(value = prefix+value)
    public String add(Model model){
        //some code here
    }

    @Override
    @RequestMapping(value = prefix+value)
    public String update(Model model){
        //some code here
    }

    @Override
    @RequestMapping(value = prefix+value)
    public String delete(Model model){
        //some code here
    }
}