Spring: controller inheritance using @Controller annotation

。_饼干妹妹 提交于 2019-12-05 04:57:24

I think the Base Controller is not a good idea if the only code it is to have is for UserAuthentication...instead use Spring security. This is the best option.

Alternatively, you can have methods like this...take a look at the Spring reference..

@Controller("loginController")
public class LoginController {    

   @RequestMapping(value="/login.do", method=RequestMethod.POST)
   public String login(Model model, HttpServletRequest request) {

      String userIdFromRequest = (String)request.getParameter("userId");
      String password = (String)request.getParameter("password");

      boolean verified = ...send userIdFromRequest and password to the user service for 
      verification...

      if (verified){
        request.getSession().setAttribute("userId", userIdFromRequest);
      }

   }          

   //More Methods

}

Did it help?

-SB

  1. Define an abstract BaseController, with no annotations
  2. Define concrete and abstract methods
  3. Call these methods from subclasses (which are annotated with @Controller) whenever needed.

The basic problem is that annotational bootstrapping is not polymorphic. I found this paper useful: http://sanguinecomputing.com/design-pattern-for-hierarchical-controller-organization-with-annotational-configuration-spring-mvc-3/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!