Spring MVC: Redirection from the base controller - How to get the redirection path?

I searched and searched and did not find anything that says it is impossible, and nothing that explains how to do it.

If you have an advanced base controller, I understand that query matching methods are also inherited.

So, with ....

public abstract class BaseController
{

    @RequestMapping(value="hello", method=RequestMethod.GET)
    public String hello(Model model) {
        return "hello-view;
}

... a controller like this ...

@Controller
@RequestMapping("/admin/")
public abstract class AdminController
{
 ....
}

... inherits a method listening in / admin / hello that returns a hi-view.

Everything is fine.

But what if I have a BaseController method that redirects:

public abstract class BaseController
{

    @RequestMapping(value="hello", method=RequestMethod.POST)
    public String hello(Model model) {
        return "redirect:/success/;
}

As I understand it, redirects require either a relative or an absolute URL, not a view name?

So how does my AdminController provide a redirect to / admin / success /?

How does the BaseController method get the descriptor at the class level @requestMapping on my AdminController?

Is it possible?

+5
4

:

public 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/";
    }
}

.

@Controller
@RequestMapping(AdminController.context)
public abstract class AdminController
{
    static final String context = "/admin/";
    @Override
    protected String getControllerContext() {
        return context;
    }
 ....
}

, , ...:

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;
    }
}
+7

Spring url,

"redirect:/admin/success/";

,

"redirect:/success/";

.

+1

http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-redirecting-redirect-prefix redirect:/myapp/some/resource .

: topjava, meals, http://localhost:8080/topjava/meals/update?...

redirect:meals http://localhost:8080/topjava/meals/meals

redirected redirect:/mealsto http: // localhost: 8080 / topjava / meals

+1
source

As long as you don’t need anything dynamic, I just redirect to the request in line. I haven't used redirection yet, but matching for your admin success would be as follows:

return "redirect:/admin/success/"

This should create a browser redirect to your controller with the appropriate request.

Another possibility is to use reflection and capture your parent's requestmapping annotation.

-1
source

All Articles