0

I am making a blog which can customize page links,fisrt function is the backstage entrance, It like this

    @GetMapping("/admin/**")
    public String index(){
        return "index";
    }

and

    @GetMapping("/{page_url}")
    public String page(@PathVariable String page_url, Model model){
        doSomeThing...
    }

Goal:

I want when i browse '/admin' to the first function.

Problem:

it to the second, what should I do (Looks like '/favicon.ico' too)

Community
  • 1
  • 1

2 Answers2

0

I am not very sure whats your intention, as I understand, properly you want to handle a redirect to index view as a welcome page on one path and logic codes on others. You can do something like this (just make sure your path is distinct).

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);
    return "index";
}

@RequestMapping(value = "/admin/{myVariable}", method = RequestMethod.GET) // or POST ...
public String admin(Model model, @PathVariable String myVariable, ...) {
    // some logic, eg. displaying current date on admin page
    LocalDate localDate = LocalDate.now();
    model.addAttribute("serverTime", localDate);
    return "admin";
}
Hoang Vu
  • 66
  • 2
  • thanks,like this https://stackoverflow.com/questions/14440991/how-do-i-set-priority-on-spring-mvc-mapping , I solved it – feather black Feb 07 '20 at 11:34
0

I would define a class pathmapping as /admin and use the methods to give them the sub mappings.

Like so:

@Controller
@RequestMapping(path = "/admin")
public class AdminController 
{
    @PostMapping(path = "/members")
    public void addMember() {
        //code
    }
}
Quadrivics
  • 181
  • 2
  • 10