java - Spring MVC : autowiring a controller in another controller -
i have 2 controllers loginviewcontroller , userviewcontroller
@controller public class loginviewcontroller { @autowired private userviewcontroller userviewcontroller; //can't autowire, since spring creates proxy userviewcontroller class @requestmapping(value="/login", method=post) public string login(){ //username , password checking etc if(login_successfull){ //when login successfull, need redirect screen user dashboard model.addattribute("loginmessage", "you 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 , send user details dashboard model.addattribute("user", userservice.get(userid)); return "userdashboard"; } } after successful user login, need redirect screen user dashboard login success message.
for can use 2 approaches
since have method load user dashboard in
userviewcontroller, autowireduserviewcontrollerinloginviewcontroller, leadsnosuchbeandefinitionexception, since spring creates proxyuserviewcontroller.i can use
redirect/forwardroute user dashboard return"redirect:/user/dashboard?id=123". when change url ofviewdashboard()method, need identify , correctredirect/forwardstatements.
so, there way invoke userviewcontroller.viewdashboard() loginviewcontroller ? using spring 3.1.4 , thymeleaf
if want output of controller send user controller, can perform 'redirect':
if(login_successfull){ //when login successfull, need redirect screen user dashboard model.addattribute("loginmessage", "you loggined successfully") return "redirect:/user/dashboard"; } if decide change url of controller, need replace html links in site. if doing global search/replace, should able find redirect instances well.
alternatively, try this:
public class urlconstants { public static final string user_path = "/user"; public static final string dashboard_path = "/dashboard"; } then change controller:
@controller @requestmapping(urlconstants.user_path) public class userviewcontroller { ... @requestmapping(value=urlconstants.dashboard_path, method=get) and redirect:
return "redirect:" + urlconstants.user_path + urlconstants.dashboard_path;
Comments
Post a Comment