Reputation: 11
In Application.java, I have read each document in a collection from a MongoDB database. And I stored all these documents in an ArrayList. I am wondering how to pass this ArrayList to the Controller class?
This is a Spring Boot web application. And I am using Spring MVC to work on this project.
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");
}
}
This class is my Controller class:
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";
}
}
Who can tell me how to pass the list variable in the Application class to the Controller class? I want to visualize the data stored in the list variable in the View.
Upvotes: 0
Views: 449
Reputation: 6216
You are missing the main point here. A controller class is different from a main application class in spring-boot. The controller class is used to serve the http request according to the path. Each request is served by a different thread. The springboot follows the MVC architecture for this.So, you should have a Controller class to serve the request , a Service class to create the model and a Repository layer to access the database whichever it may be. And also the Model class is returned from the controller.
@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";
}
}
You could call the repository method from the controller to access the data which should be the right way to go ,or better yet , have a service class in between if you have more logic to be implemented.
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);
}
Upvotes: 1