本文介绍了春季:使用注解@Controller控制器继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想能够创建我的春天应用基地控制器,除其他外,确定用户是注册用户与否。这个基地控制器,下面的模板设计模式,将包含该控制器的子类会实现一个抽象protected方法。

I'd like to be able to create a base controller in my Spring app that, among other things, determines if a user is a registered user or not. This base controller, following the template design pattern, would contain an abstract protected method that controller subclasses would implement.

抽象方法会传递给它的用户的一个实例,注册或其它。不过,我不知道我会怎么做,因为它似乎是通过使用控制器纯粹使用注解@Controller每个控制器是免费的但他们喜欢来定义他们的请求处理方法。

The abstract method would have passed to it an instance of User, registered or otherwise. However, I have no idea how I would do this since it seems that by using controllers purely using the @Controller annotation each controller is free to define their request handling method however they like.

就像创建某种用户服务类的注入到每个控制器,并用于验证用户是解决这个的一种方式?这引出了一个问题(至少对我来说)如何这样的控制器获得的HttpServletRequest或Session对象的举行?

Would creating some sort of user service class that is injected into each controller and used to validate a user be one way to get around this? This begs the question (at least for me) how does such a controller get a hold of a HttpServletRequest or the Session object?

感谢。

推荐答案

我觉得基本控制器是不是一个好主意,如果只有code这是有是UserAuthentication ...而不是使用Spring的安全性。这是最好的选择。

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

}

有没有帮助?

-SB

这篇关于春季:使用注解@Controller控制器继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 19:16
查看更多