Controller belongs to the Presentation layer?

后端 未结 3 1819
鱼传尺愫
鱼传尺愫 2020-12-24 14:43

I heard that controller belongs to the presentation layer. How is it possible?

I thought that :

  • view is for presentation
  • model is for business
3条回答
  •  忘掉有多难
    2020-12-24 15:35

    The controller controls the presentation layer logic. For all the business code, transactional use cases, persistence, etc., it typically delegates to a service layer.

    A typical way of doing that is to implement transactional services as spring beans and inject those spring beans in controllers. Typical use case: create a new product:

    1. The controller receives a command bean from the browser
    2. It validates that all the required data is present, and if not, redisplays the product creation page with error messages
    3. It calls a service bean to create the product
    4. The service bean runs in a transaction. It gets the product category from the database, attaches the product to its category, computes the price for the product based on current pricing strategies, sends a JMS message to an external application, and returns the ID of the created product
    5. The controller redirects to the product detail page, using the ID of the created product as a URL parameter.

提交回复
热议问题