Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a series of Controllers with Request Mappings that match certain URL's. I also want a Controller that will match any other URL not matched by the other Controllers. Is there a way to do this in Spring MVC? For example, could i have a controller with @RequestMapping(value="**") and change the order in which Spring Controllers are processed so this Controller is processed last to catch all unmatched requests? Or is there another way to achieve this behaviour?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
192 views
Welcome To Ask or Share your Answers For Others

1 Answer

If your base url is like that= http://localhost/myapp/ where myapp is your context then myapp/a.html, myapp/b.html myapp/c.html will get mapped to the first 3 method in the following controller. But anything else will reach the last method which matches **. Please note that , if you put ** mapped method at the top of your controller then all request will reach this method.

Then this controller servrs your requirement:

@Controller
@RequestMapping("/")
public class ImportController{

    @RequestMapping(value = "a.html", method = RequestMethod.GET)
    public ModelAndView getA(HttpServletRequest req) {
        ModelAndView mv;
        mv = new ModelAndView("a");
        return mv;
    }

    @RequestMapping(value = "b.html", method = RequestMethod.GET)
    public ModelAndView getB(HttpServletRequest req) {
        ModelAndView mv;
        mv = new ModelAndView("b");
        return mv;
    }

    @RequestMapping(value = "c.html", method = RequestMethod.GET)
    public ModelAndView getC(HttpServletRequest req) {
        ModelAndView mv;
        mv = new ModelAndView("c");
        return mv;
    }

@RequestMapping(value="**",method = RequestMethod.GET)
public String getAnythingelse(){
return "redirect:/404.html";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...