【问题标题】:How to convert the data from MongoDB to the View through the Controller in Spring MVCSpring MVC中如何通过Controller将数据从MongoDB转换到View
【发布时间】:2019-07-08 21:59:34
【问题描述】:

在 Application.java 中,我从 MongoDB 数据库中读取了集合中的每个文档。我将所有这些文档存储在一个 ArrayList 中。我想知道如何将此 ArrayList 传递给 Controller 类?

这是一个 Spring Boot Web 应用程序。我正在使用 Spring MVC 来处理这个项目。

这个类是Application.java。我从 MongoDB 中取出所有鞋子并将它们存储在 list 变量中。

public class Application implements CommandLineRunner {

    @Autowired
    private ShoeRepository repository;
    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
    }
    @Override
    public void run(String... args) throws Exception {

        // fetch all shoes
        List<Shoe> list = new ArrayList<>();
        System.out.println("Shoes found with findAll():");
        System.out.println("-------------------------------");
        for (Shoe shoe : repository.findAll()) {
            //System.out.println(customer);
            list.add(shoe);
        }
        System.out.println("all shoes have been saved to the list");
    }
}

这个类是我的控制器类:


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class GreetingController {

    @RequestMapping(value = "/greeting" ,method = RequestMethod.GET)
    public String greeting(@RequestParam(name="name", required=true) List<Shoe> list, Model model) {
        model.addAttribute("name", list.get(0).asin);
        return "greeting";
    }
}

谁能告诉我如何将Application类中的list变量传递给Controller类?我想在视图中可视化存储在列表变量中的数据。

【问题讨论】:

  • 生活忙碌,但你有没有试过阅读:spring.io/guides/gs/accessing-data-mongodb
  • @emotionlessbananas。是的,我已经阅读了它,但它只向我展示了如何读取 Application.java 中的数据。如果我在 Application.java 中获取数据,我想知道如何将这些数据传递给控制器​​和视图?我想知道如何在 Controller 类的方法中读取数据?你能回答我的问题吗?

标签: java mongodb spring-boot spring-mvc


【解决方案1】:

你错过了这里的要点。控制器类与 spring-boot 中的主应用程序类不同。控制器类用于根据路径服务于 http 请求。每个请求由不同的线程提供服务。 springboot 遵循 MVC 架构。因此,您应该有一个 Controller 类来服务请求,一个 Service 类来创建模型和一个 Repository 层来访问数据库,无论它可能是什么。并且模型类也是从控制器返回的。

@Controller
public class GreetingController {

    @Autowired
    private ShoeRepository repository;

    @RequestMapping(value = "/greeting" ,method = RequestMethod.GET)
    public String greeting(@RequestParam(name="name", required=true) List<Shoe> list, Model model) {
        model.addAttribute("name", list.get(0).asin);
        List<Shoe> list = new ArrayList<>();
        list.addAll(repository.findAll());
        return "greeting";
    }
}

您可以从控制器调用存储库方法来访问数据,这应该是正确的方法,或者更好的是,如果您需要实现更多逻辑,则在两者之间有一个服务类。

public interface ShoeRepository extends MongoRepository<Shoe, Long> {

    //Custom Query
    @Query("SELECT o FROM Object o WHERE o.field = somethingThatValidsIt")
    public List<Shoe> customFindAll();

    //Default CRUD repository implementation
    public List<Shoe> findAll()

    //Other methods
    public Shoe findByNameAndAvailable(String name, boolean available);

}

【讨论】:

  • 您好,我已经创建了模型类和存储库类。 public class Shoe { @Id public String id;公共字符串asin;公共字符串名称;公共字符串网址; public HashMap> colorSizes = new HashMap(); public HashMap colorPrice = new HashMap(); }public interface ShoeRepository extends MongoRepository { //public Customer findByFirstName(String firstName); public List findByASIN(String ASIN); } 你能告诉我下一步该怎么做吗?
  • 您可以将列表添加到模型对象中,然后它将在ui端可用。
  • @Ananthapadmanabhan,这是我的控制器类。@Controller public class GreetingController { @RequestMapping(value = "/greeting" ,method = RequestMethod.GET) public String greeting(Model model) { ShoeRepository repository =无效的; List list = repository.findAll(); model.addAttribute("name", list.get(0).asin);返回“问候”;在这个项目开始运行并打开'localhost:8080/greeting/'后,我看到了'Whitelabel Error Page'。你能告诉我如何正确编写代码
  • @EricWen 您能否更具体地说明您的要求。我对您的要求感到困惑。
  • 感谢您的回答!我已经创建了一个模型类,它叫做 Shoe。我还创建了一个从 MongoRespository 扩展的 ShoeRepository 类。我希望你能告诉我我需要编写的确切代码,以便获取 MongoDB 数据库中集合中的所有文档。我计划使用 ArrayList 来存储所有文档。我希望你能帮助我解决这个要求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
  • 2019-06-15
  • 2020-02-17
  • 1970-01-01
相关资源
最近更新 更多