@H_404_1@我进行了搜索和搜索,但没有找到任何说不可能的东西,也没有找到解释如何做的事情.
@H_404_1@如果您有一个扩展的基本控制器,我理解请求映射方法也是继承的.
@H_404_1@所以….
@H_404_1@
public abstract class BaseController
{
@RequestMapping(value="hello",method=RequestMethod.GET)
public String hello(Model model) {
return "hello-view;
}
@H_404_1@……像这样的控制器……
@H_404_1@
@Controller
@RequestMapping("/admin/")
public abstract class AdminController
{
....
}
@H_404_1@…将继承侦听/ admin / hello的方法,该方法返回hello-view.
@H_404_1@这一切都很好.
@H_404_1@但是如果我有一个重定向的BaseController方法怎么办:
@H_404_1@
public abstract class BaseController
{
@RequestMapping(value="hello",method=RequestMethod.POST)
public String hello(Model model) {
return "redirect:/success/;
}
@H_404_1@据我了解,重定向需要相对或绝对URL而不是视图名?
@H_404_1@那么我的AdminController如何确保重定向发生在/ admin / success /?
@H_404_1@BaseController方法如何获取AdminController上类级别@requestMapping的句柄?
@H_404_1@这可能吗?最佳答案
一种选择:
@H_404_1@
原文链接:https://www.f2er.com/spring/432727.htmlpublic abstract class BaseController
{
/** get the context for the implementation controller **/
protected abstract String getControllerContext();
@RequestMapping(value="hello",method=RequestMethod.GET)
public String hello(Model model) {
return "redirect:"+getControllerContext()+"success/";
}
}
@H_404_1@这是管理员控制器.
@H_404_1@
@Controller
@RequestMapping(AdminController.context)
public abstract class AdminController
{
static final String context = "/admin/";
@Override
protected String getControllerContext() {
return context;
}
....
}
@H_404_1@涉及可能有效的反射的另一种选择……:
@H_404_1@
public abstract class BaseController
{
String context = null;
@RequestMapping(value="hello",method=RequestMethod.GET)
public String hello(Model model) {
return "redirect:"+getControllerContext()+"success/";
}
// untested code,but should get you started.
private String getControllerContext() {
if ( context == null ) {
Class klass = this.getClass();
Annotation annotation = klass.getAnnotation(RequestMapping.class);
if ( annotation != null ) {
context = annotation.value();
}
}
return context;
}
}