1

I have two controllers.

I wrote this controllers and where i have to write config to correct work.

@Controller

public class BookController {

    private BookService bookService;

    @Autowired(required = true)
    @Qualifier(value = "bookService")
    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

    @RequestMapping(value = "books", method = RequestMethod.GET)
    public String listBooks(Model model){
        model.addAttribute("book", new Book());
        model.addAttribute("listBooks", this.bookService.listBooks());

        return "books";
    }

}

@Controller("controller1")
public class AuthorController {
        private AuthorService  authorService;

        @Autowired(required = true)
        @Qualifier(value = "authorService")
        public void setBookService(AuthorService authorService) {
            this.authorService = authorService;
        }

        @RequestMapping
        (value = "authors", method = RequestMethod.GET)
        public String listAuthors(Model model){
           model.addAttribute("author", new Author());
           model.addAttribute("listAuthors", this.authorService.list());

           return "";
        }
    }
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • 1
    You don't need to write configuration. You just need `@Controller` annotation, and have component scan in your `dispatcher-servlet.xml`. Check if you have this `` in your xml. – amicoderozer Sep 13 '18 at 15:25

3 Answers3

2

If you use Xml based configuration try add this config to your dispatcherServlet.xml

<context:component-scan base-package="com.example.controllers"/>

and if you use Java based config the add this code to your WebMvcConfigurerAdapter implementation config class

@EnableWebMvc
@ComponentScan(basePackages = "com.example.controllers")

Example:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example.controllers")
public class SpringConfig extends WebMvcConfigurerAdapter{

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/pages/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}
Seymur Asadov
  • 612
  • 5
  • 19
0

Well, I believe that it will be enough to add

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

into a pom.xml and add an @EnableWebMvc annotation into you AppConfig file

UPD Let's say you have an application com.foo.app.AppName Then to solve the problem you must create a class com.foo.app.AppName.AppConfig and add at least this:

@Configuration
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {
}

It's not an iron rule to implement WebMvcConfigurerAdapter - you can use any configurers depends of ypu needs

Yuriy Tsarkov
  • 2,461
  • 2
  • 14
  • 28
0

You could autowire services like this:

    @Controller
    @RequestMapping("book")
    public class BookController {

        @Autowired
        private BookService bookService;

        @GetMapping("find_all")
        public String list(Model model) {
            model.addAttribute("book", bookService.findAll());
            return "book/list";
        }
Guilherme Alencar
  • 1,243
  • 12
  • 21
  • Type Status Report Message /books Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. –  Sep 13 '18 at 17:25
  • 404 Not found so...? –  Sep 13 '18 at 17:26
  • The return of the controller method ("book/list") is a path to some .html file, so you gotta have this "book" as a folder and a "list.html" as a file. All of these has to be inside of resources folder. – Guilherme Alencar Sep 13 '18 at 19:54
  • And I hope you are using the template engine (thymeleaf). – Guilherme Alencar Sep 13 '18 at 19:57
  • Look this [link](https://stackoverflow.com/questions/27583278/spring-boot-spring-mvc-thymeleaf-apache-tiles/27680210#27680210) the image in the beginning of the question, pay attention in the hierarchy – Guilherme Alencar Sep 13 '18 at 20:16