Contradictory explanations of MVC in JSF

后端 未结 1 1963
Happy的楠姐
Happy的楠姐 2021-01-03 05:43

I\'d starting to learn JSF, but first I\'d like to understand the big picture of it as an MVC framework.

There are several answers with many upvotes explaining what

1条回答
  •  天涯浪人
    2021-01-03 06:31

    I'm mostly concerned about Managed beans here. Are they M or C?

    People consider them M when they look like this:

    @ManagedBean
    public class Bean {
    
        private String username; // +getter+setter
        private String password; // +getter+setter
    
        @Resource
        private DataSource dataSource;
    
        public void login() {
            try (
                Connection connection = dataSource.getConnection();
                PreparedStatement statement = connection.prepareStatement("SELECT * FROM User WHERE username = ? AND password = MD5(?)");
            ) {
                statement.setString(1, username);
                statement.setString(2, password);
    
                try (ResultSet resultSet = statement.executeQuery()) {
                    if (resultSet.next()) {
                        // Login.
                    }
                }
            }
        }
    
        // ...
    }
    

    But people consider them C when they look like this:

    @ManagedBean
    public class Bean {
    
        private User user // +getter
    
        @EJB
        private UserService userService;
    
        public void login() {
            if (userService.find(user) != null) {
                // Login.
            }
        }
    
        // ...
    }
    

    This is also mentioned in the very same MVC answer you found:

    Note that some starters and even some —very basic— tutorials mingle/copy/flatten the entity's properties in the managed bean, which would effectively make the controller a model. Needless to say that this is poor design (i.e. not a clean MVC design).

    See also:

    • Our JSF wiki page

    0 讨论(0)
提交回复
热议问题