如何在Spring 4上正确使用Models?

时间:2023-02-09 02:30:55

I am new using Spring's MVC (but Ive been using it for some years at other php frameworks).

我是使用Spring的MVC的新手(但我已经在其他php框架上使用了几年)。

I have many doubts, I read the spring info and seems to be right and all.. then Ichecked this tutorial http://javahash.com/spring-4-mvc-hello-world-tutorial-full-example/ and it works and all, but on the controller part, there is a code I dont understand, and Id like to know how to properly use the Models on Spring.

我有很多疑问,我阅读了弹簧信息,似乎是正确的所有..然后我检查了这个教程http://javahash.com/spring-4-mvc-hello-world-tutorial-full-example/它的工作原理和所有,但在控制器部分,有一个我不明白的代码,我想知道如何正确使用Spring上的模型。

As far as I know, the models should call make the calls to the db, so, what about the services (interfaces and implementations) and the DTOs?

据我所知,模型应调用对数据库的调用,那么,服务(接口和实现)和DTO呢?

At the example they make something like this:

在这个例子中,他们做了这样的事情:

@Controller
public class HelloWorldController { 

    @RequestMapping("/hello")
    public String hello(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "helloworld";
    }

}

It receives a model as parameter... I bet if there is any, Spring would use a default one, what if I want to add more interaction and lets say, specifiy a model to call the db? Any idea how I could do that?

它接收一个模型作为参数...我敢打赌,如果有,Spring会使用默认的,如果我想添加更多的交互,让我们说,指定一个模型来调用db?知道我怎么能这样做吗?

And if I want to add a service... I am a little bit clueless about it, if someone could help me to understand...

如果我想添加一项服务......我有点无能为力,如果有人能帮助我理解......

Thanks in advance

提前致谢

2 个解决方案

#1


Model is a map representing the data that the view needs. It can contain one or more entities, or simple objects, or strings, or whatever you want.

Model是表示视图所需数据的映射。它可以包含一个或多个实体,简单对象,字符串或任何您想要的内容。

MVC doesn't require the use of a database. The model does not "call the db". You can inject a repository into your controller to load data from a db into your model.

MVC不需要使用数据库。该模型不“调用数据库”。您可以将存储库注入控制器,以将数据从数据库加载到模型中。

@Controller
@RequestMapping("/foo")
public class FooController {

  @Autowired
  private FooRepository fooRepository;

  @RequestMapping
  String getFoos(Model model) {

    List<Foo> foos = fooRepository.findAll();
    model.addAttribute("foos", foos);

    model.addAttribute("someOtherDataYourViewNeeds", "bar");

    return "foo/list";

  }

}

#2


This Model Object is injected by spring, and his content will be sent to the view. You can see the documentation for Model interface here http://docs.spring.io/spring-framework/docs/current/javadoc-api/. If you wanna access some object on your view, you can you use model.addAttribute(object). I think you could read The IoC Container documentation to understand how spring works.

此模型对象由spring注入,其内容将发送到视图。您可以在http://docs.spring.io/spring-framework/docs/current/javadoc-api/上看到Model接口的文档。如果要访问视图中的某个对象,可以使用model.addAttribute(object)。我想你可以阅读IoC Container文档来了解spring的工作原理。

#1


Model is a map representing the data that the view needs. It can contain one or more entities, or simple objects, or strings, or whatever you want.

Model是表示视图所需数据的映射。它可以包含一个或多个实体,简单对象,字符串或任何您想要的内容。

MVC doesn't require the use of a database. The model does not "call the db". You can inject a repository into your controller to load data from a db into your model.

MVC不需要使用数据库。该模型不“调用数据库”。您可以将存储库注入控制器,以将数据从数据库加载到模型中。

@Controller
@RequestMapping("/foo")
public class FooController {

  @Autowired
  private FooRepository fooRepository;

  @RequestMapping
  String getFoos(Model model) {

    List<Foo> foos = fooRepository.findAll();
    model.addAttribute("foos", foos);

    model.addAttribute("someOtherDataYourViewNeeds", "bar");

    return "foo/list";

  }

}

#2


This Model Object is injected by spring, and his content will be sent to the view. You can see the documentation for Model interface here http://docs.spring.io/spring-framework/docs/current/javadoc-api/. If you wanna access some object on your view, you can you use model.addAttribute(object). I think you could read The IoC Container documentation to understand how spring works.

此模型对象由spring注入,其内容将发送到视图。您可以在http://docs.spring.io/spring-framework/docs/current/javadoc-api/上看到Model接口的文档。如果要访问视图中的某个对象,可以使用model.addAttribute(object)。我想你可以阅读IoC Container文档来了解spring的工作原理。