Home Spring Refactor common controller redirection codes
Reply: 0

Spring Refactor common controller redirection codes

user1503
1#
user1503 Published in April 24, 2018, 6:38 am

I have some redirection codes in my Controller classes that I want to refactor. These codes just check if an object is null, and if it is... it would redirect it to a particular page.

Below are parts of the code:

@RequestMapping(value = "/user-details", method = RequestMethod.GET)
public ModelAndView userDetails(@RequestParam("id") int id) {

    ...

    User user = userService.getUserById(id);
    if (matter == null) {
        return new ModelAndView("redirect:/users-list.html");
    }

    ...
}

@RequestMapping(value = "/user-application-details", method = RequestMethod.GET)
public ModelAndView userApplicationDetails(@RequestParam("applicationId") int applicationId) {

    ...

    UserApplication application = userApplicationService.getApplicationById(applicationId);
    if (application == null) {
        return new ModelAndView("redirect:/users-list.html");
    }

    ...
}

@RequestMapping(value = "/user-activity-details", method = RequestMethod.GET)
public ModelAndView userActivityDetails(@RequestParam("activityId") int activityId) {

    ...

    UserActivity activity = userActivityService.getActivityById(activityId);
    if (activity == null) {
        return new ModelAndView("redirect:/users-list.html");
    }

    ...
}

I would like to refactor the null checking and redirection to a separate class, but I am not sure how I could call it inside the controller and would do the redirection to the specified page.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.334896 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO