简体   繁体   中英

How to separate Logics by method name in Springframework without extends MultiActionController?

As title says, I want to perform multiple function in single controller with out inheritance MultiActionController.

So I made code like Below.

package org.owls.replace;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value={"/test"})
public class TestController {
    private Logger log = Logger.getLogger(TestController.class);

    @RequestMapping(params={"m=doA"})
    public void doA() throws Exception{
        log.info("DoA !");
    }

    @RequestMapping(params={"m=doB"})
    public void doB() throws Exception{
        log.info("DoB !");
    }

    @RequestMapping(params={"m=doC"})
    public void doC() throws Exception{
        log.info("DoC !");
    }
};

this code works. if I call "~/m=doA", it prints "DoA". However, what I want is remove every @RequestMapping except on class.(because parameter "m" is fixed and only method name can be a parameter)

So What I expected was some code like Below(Does not work. But pay attention on params value in @RequestMappping).

package org.owls.replace;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value={"/test"}, params={"doA", "doB", "doC"})
public class TestController {
    private Logger log = Logger.getLogger(TestController.class);

    public void doA() throws Exception{
        log.info("DoA !");
    }

    public void doB() throws Exception{
        log.info("DoB !");
    }

    public void doC() throws Exception{
        log.info("DoC !");
    }
};

** how can I mapping method names with parameter without using MultiActionController or xml settings?

My English is so ugly, but I hope you can understand what I mean by reading code. Thanks for editing poor English and share my problem. :D

Spring MVC is customizable enough that you can probably achieve this by implementing and registering your custom HandlerMapping and HandlerAdapter , but it's just not worth it.

Use your first hunch and go with the @RequestMapping(params={"m=doXXX"}) annotated methods. This will also act as documentation on how your method is mapped instead of making assumptions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM