Spring Boot无法解析视图

huangapple 未分类评论45阅读模式
英文:

Spring Boot does not resolve View

问题

我正在尝试使用Spring Boot创建一个简单的控制器
配置如下:

    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = {"ru.spb.chat.controller"})
    public class WebConfig implements WebMvcConfigurer {

       @Bean
        public ViewResolver viewResolver() {
           InternalResourceViewResolver bean = new InternalResourceViewResolver();
           bean.setPrefix("/WEB-INF/view/");
           bean.setSuffix(".html");
           return bean;
         }
    }

以及Servlet部分:

     public class MainWebAppInitializer implements WebApplicationInitializer {
       @Override
       public void onStartup(final ServletContext sc) throws ServletException {

           AnnotationConfigWebApplicationContext root =
                   new AnnotationConfigWebApplicationContext();

           root.scan("ru.spb");
           sc.addListener(new ContextLoaderListener(root));

           ServletRegistration.Dynamic appServlet =
                sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext()));
           appServlet.setLoadOnStartup(1);
           appServlet.addMapping("/");
        }
   }

我的控制器:

     @Controller
     public class RootController {

        @GetMapping("/")
        public String root() {
             return "login";
        }
    }
但是当我尝试映射到“/”时,它返回404错误
这是我的项目结构:
[![structure][1]][1]

  [1]: https://i.stack.imgur.com/PbK7h.png
英文:

I'am trying to create a simple controller with Spring Boot
Congifuration is:

@Configuration
@EnableWebMvc
@ComponentScan (basePackages = { "ru.spb.chat.controller" })
public class WebConfig implements WebMvcConfigurer {

   @Bean
    public ViewResolver viewResolver() {
       InternalResourceViewResolver bean = new InternalResourceViewResolver();
       bean.setPrefix("/WEB-INF/view/");
       bean.setSuffix(".html");
       return bean;
     }
}

and for servlet:

 public class MainWebAppInitializer implements WebApplicationInitializer {
   @Override
   public void onStartup(final ServletContext sc) throws ServletException {

       AnnotationConfigWebApplicationContext root =
               new AnnotationConfigWebApplicationContext();

       root.scan("ru.spb");
       sc.addListener(new ContextLoaderListener(root));

       ServletRegistration.Dynamic appServlet =
            sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext()));
       appServlet.setLoadOnStartup(1);
       appServlet.addMapping("/");
    }

}

My controller.

 @Controller
 public class RootController {

    @GetMapping ("/")
    public String root() {
         return "login";
    }
}

But when I try to map on "/" it returns 404-ERROR
This is my project-structure:
Spring Boot无法解析视图

答案1

得分: 0

移除你的WebConfig,以及移除你的ServletInitializerMainWebAppInitializer(你可能也可以移除WebSocketConfig并使用Spring Boot的自动配置!)。

让你的ChatApplication扩展SpringBootServletInitializer并实现configure方法。

@SpringBootApplication
public class ChatApplication extends SpringBootServletInitializer {

  public static void main(String[] args) {
    SpringApplication.run(ChatApplication.class, args);
  }

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(ChatApplication.class);
  } 

}

然后在你的application.properties文件中添加以下内容:

spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.html

现在你正在使用适当的Spring Boot自动配置和正确的创建可执行WAR文件的方式。

话虽如此,一般来说,你不希望使用WAR文件(只有在使用JSP时才需要,但在内嵌容器中使用JSP是不推荐的)。

英文:

Remove your WebConfig and remove your ServletInitializer and MainWebAppInitializer. (You can probably also remove the WebSocketConfig and use the auto-configuration from Spring Boot!).

Let your ChatApplication extend SpringBootServletInitializer and implement the configure method.

@SpringBootApplication
public class ChatApplication extends SpringBootServletInitializer {

  public static void main(String[] args) {
    SpringApplication.run(ChatApplication.class, args);
  }

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(ChatApplication.class);
  } 

}

Then in your application.properties add

spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.html

Now you are using the proper Spring Boot auto-configuration AND proper way of creating a WAR which is executable.

That being said, you generally don't want a WAR (only if you use JSP, which is discouraged with embedded containers).

huangapple
  • 本文由 发表于 2020年4月8日 20:47:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/61101040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定