Faizal
Faizal

Reputation: 1345

Spring MVC : autowiring a controller in another controller

I have two controllers LoginViewController and UserViewController as

@Controller
public class LoginViewController {

    @Autowired
    private UserViewController userViewController;  //Can't autowire, since spring creates a proxy for UserViewController class

    @RequestMapping(value="/login", method=POST)
    public String login(){

        //username and password checking etc

        if(login_successfull){
            //When login successfull, i need to redirect the screen to user dashboard  
            model.addAttribute("loginMessage", "You are loggined successfully")
            return userViewController.viewDashboard(userId);
        }   
    }
}

@Controller
@RequestMapping("/user")
public class UserViewController {

    @Autowired 
    private UserService userService;

    @RequestMapping(value="/dashboard", method=GET)
    public String viewDashboard(Model model, @RequestParam(value="id", required=true) Long userId){
        //Fetch and send user details to dashboard
        model.addAttribute("user", userService.get(userId));
        return "userDashboard"; 
    }
}

After successful user login, I need to redirect the screen to user dashboard with a login success message.

For this can use 2 approaches

  1. Since I have a method to load user dashboard in UserViewController, I autowired UserViewController in LoginViewController, which leads to NoSuchBeanDefinitionException, since spring creates a proxy for UserViewController.

  2. I can use redirect/forward to route to user dashboard as return "redirect:/user/dashboard?id=123". But when I change the url of viewDashboard() method, I need to identify and correct all the redirect/forward statements.

So, is there any way to invoke UserViewController.viewDashboard() from LoginViewController ? I am using spring 3.1.4 and thymeleaf

Upvotes: 1

Views: 7022

Answers (1)

Jason
Jason

Reputation: 11822

If you want the output of a controller to send the user to another controller, you can perform a 'redirect':

if(login_successfull){
    //When login successfull, i need to redirect the screen to user dashboard  
    model.addAttribute("loginMessage", "You are loggined successfully")
    return "redirect:/user/dashboard";
}

If you decide to change the URL of your controller, you will need to replace all those HTML links in your site. If you are already doing a global search/replace, then you should be able to find the redirect instances as well.

Alternatively, you could try this:

public class UrlConstants {
    public static final String USER_PATH = "/user";
    public static final String DASHBOARD_PATH = "/dashboard";
}

Then you change your controller:

@Controller
@RequestMapping(UrlConstants.USER_PATH)
public class UserViewController {
    ...
    @RequestMapping(value=UrlConstants.DASHBOARD_PATH, method=GET)

And then for your redirect:

return "redirect:" + UrlConstants.USER_PATH + UrlConstants.DASHBOARD_PATH;

Upvotes: 6

Related Questions