【问题标题】:Config two controllers配置两个控制器
【发布时间】:2019-02-18 08:39:50
【问题描述】:

我有两个控制器。

我写了这个控制器,我必须在哪里写配置来纠正工作。

@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 "";
        }
    }

【问题讨论】:

  • 不需要写配置。您只需要@Controller 注释,并在您的dispatcher-servlet.xml 中进行组件扫描。检查你的xml中是否有这个<context:component-scan base-package="path.to.your.controllers"/>

标签: spring spring-mvc


【解决方案1】:

你可以像这样自动装配服务:

    @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";
        }

【讨论】:

  • 类型状态报告消息 /books 描述 源服务器没有找到目标资源的当前表示或不愿意透露存在的表示。
  • 404 没有找到所以...?
  • 控制器方法(“book/list”)的返回是某个 .html 文件的路径,因此您必须将此“book”作为文件夹,将“list.html”作为文件。所有这些都必须在资源文件夹中。
  • 我希望您使用的是模板引擎 (thymeleaf)。
  • 看这个link问题开头的图片,在层次结构中注意
【解决方案2】:

如果您使用基于 Xml 的配置,请尝试将此配置添加到您的 dispatcherServlet.xml

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

如果您使用基于 Java 的配置,请将此代码添加到您的 WebMvcConfigurerAdapter 实现配置类中

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

例子:

@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();
    }
}

【讨论】:

  • 两个控制器都可以使用吗?很抱歉提出愚蠢的问题,但我对这项技术真的很不了解。
  • 当然。您可以根据需要在同一个包中声明控制器。
【解决方案3】:

嗯,我相信添加就够了

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

pom.xml 并在AppConfig 文件中添加@EnableWebMvc 注释

UPD 假设您有一个应用程序com.foo.app.AppName 然后要解决问题,您必须创建一个类com.foo.app.AppName.AppConfig 并至少添加以下内容:

@Configuration
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {
}

实现WebMvcConfigurerAdapter 不是铁律——您可以使用任何配置器,具体取决于 ypu 需求

【讨论】:

  • 我必须在@EnableWebMvc 处替换@Controller?
  • @Jackson750 没有。项目中的某个位置必须放置一个配置文件,该配置文件是项目的主配置文件。我会更新我的答案来澄清这个案例
  • 好的,我等你的答复
  • 该类中必须包含什么?我只是以前没见过这种结构。
  • 没有必要存在。尝试创建它,如下所示。
猜你喜欢
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多