MVC Web应用程序如何在java中工作? [英] How MVC web application works in java?

查看:108
本文介绍了MVC Web应用程序如何在java中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用程序中定义视图,控制器,服务和Dao层时,数据如何在它们之间流动。



例如假设我们添加了struts,spring,hibernate等。项目中的罐子,然后他们如何一起工作?

解决方案

MVC模式本身没有描述如何实现您的web应用程序。它所描述的是您的组件应该如何相互交互以实现具有可替换组件的模块化体系结构。



该模式在Martin Fowler的 POEAA Wikipedia 。有关MVC的更多信息,请参阅 Wikipedia

使用Java,Spring和Hibernate的简单示例

在这种情况下,Spring MVC提供了一个可插入的框架,您可以在其中定义模型,控制器和视图,而不必将它们紧密结合在一起(这可以通过 IOC / DI )。

首先要注意的是 DispatcherServlet ,它是一个常规的servlet,通过处理所有传入的HTTP请求和将它们路由到它们各自的控制器。通过它们的映射查找适当的控制器,例如。通过 @ RequestMapping 注释。

控制器的责任是确定应该执行哪些操作来作为对传入请求的响应。这通常通过检查标题,参数,会话信息,路径来获得用户想要做的事情。这是一个非常简单的例子:

  if(session.getAttribute(authenticated)== false){
//我们需要重定向到登录页面
} else {
//一切都很好,所以我们在模型中做一些业务逻辑
importantService.doSomethingReallyImportant(productOrder)
然后,控制权被传递到业务逻辑发生的 model 图层。这可能包括更改模型状态的任何操作,如更新密码,注册预订,清算交易等。在Web应用程序中,这些操作通常涉及使用持久性API,例如。休眠。

  public class ImportantService {
public void doSomethingVeryImportant(final ProductOrder order){
//这里我们定义业务操作
getCurrentBasket()。add(order);

//额外的伪持久化操作
getSession()。update(order);






$ b

实际上,当模型完成后,控制返回到决定如何更新视图(例如重定向浏览器或仅显示结果页面)的控制器,用户可以在其中看到他/她的操作结果。

As we define view,controller,service and Dao layer in application then how data flow in between them.

e.g. suppose we add struts,spring,hibernate,etc. jars in project then how they work together?

解决方案

The MVC pattern itself does not describe how should you implement your web application. What it describes is how your components should interact with each other in order to achieve a modular architecture with replaceable components.

The pattern is explained in details in Martin Fowler's POEAA and in Wikipedia. More info about MVC can be found in Wikipedia

A simple example using Java, Spring and Hibernate

In this case Spring MVC provides a pluggable framework where you can define your models , controllers and views without coupling them together too tightly (this is achieved through IOC/DI).

The first thing to notice is the DispatcherServlet which is a regular servlet that serves as an entry point by handling all the incoming HTTP requests and routing them to their respective controllers. The appropriate controller is looked up by their mappings, eg. via @RequestMapping annotations.

The controller's responsibility is to determine what actions should be performed as a response to the incoming request. This is usually done by checking headers, parameters, session info, path for information what the user wanted to do. Here is an extremely simple example:

if (session.getAttribute("authenticated") == false) {
    // we need to redirect to the login page
} else {
    // everything was fine, so we do some business logic in the model
    importantService.doSomethingReallyImportant(productOrder)
}

Then the control is passed to the model layer where business logic happens. This may include any operation that changes the model's state, like updating a password, registering a booking, clearing transactions, and so on. In web applications these operations often involve the use of a persistence API, eg. Hibernate.

public class ImportantService {
    public void doSomethingVeryImportant(final ProductOrder order) {
        // Here we define the business operation
        getCurrentBasket().add(order);

        // An additional pseudo-persistence operation
        getSession().update(order);
    }
}

In practice when the model has finished then control is returned to the controller which decides how to update the view (eg. redirecting the browser or just simply display a result page) where the user sees the result of his/her action.

这篇关于MVC Web应用程序如何在java中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆