o.s.boot.context.web。ErrorPage:当响应已经提交时,不能向错误页面发送请求[/]。

时间:2021-12-28 16:55:47

I keep getting a blank page when I render this application in tomcat 8. The only error I see is:

当我在tomcat 8中呈现这个应用程序时,我一直得到一个空白页。我看到的唯一错误是:

o.s.boot.context.web.ErrorPageFilter: Cannot forward to error page for 
request [/] as the response has already been committed.

Therefore my issue is I am unable to render the index.html page.

因此我的问题是我无法呈现索引。html页面。

I took a static page and added a spring-boot module in it. Ever since I made the change I have not been able to render the page again. I have the index.html file under templates

我选择了一个静态页面,并在其中添加了一个spring引导模块。自从我做了改变,我就再也不能渲染页面了。我有索引。html文件模板下

o.s.boot.context.web。ErrorPage:当响应已经提交时,不能向错误页面发送请求[/]。

I added comments in my controller to see if they are even being hit and they are not:

我在我的控制器中添加了注释,看看它们是否被击中,它们不是:

@Controller
public class ApplicationController {


    @Autowired
    ContactService contactService;

    @RequestMapping(value="/", method= RequestMethod.GET)
    public String contactForm() throws IOException {
        System.out.println("Inside GET in Application Controller");

        //model.addAttribute("contact", new Contact());

        return "index";
    }

    @RequestMapping(value="/contact", method= RequestMethod.POST)
    public String contactSubmit() throws IOException {

        System.out.println("Inside POST in Application Controller");

        return "index";
    }


}

I was using Thymeleaf but decided against it which is why the parameters in the methods are blank.

我使用的是Thymeleaf,但决定不使用它,这就是为什么方法中的参数是空白的。

I have uploaded the project to GIT to make it easier for someone to poke around and download if so desired.

我已经把这个项目上传到GIT,让别人可以更容易地浏览和下载。

Project on GIT

GIT项目

----------------Update 1----------------------

更新1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    private static final Logger LOGGER = Logger.getLogger(WebConfig.class);

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);

        registry.addResourceHandler("/css/**").addResourceLocations("/resources/css/");

        registry.addResourceHandler("/images/**").addResourceLocations("/resources/images/");

        registry.addResourceHandler("/image/**").addResourceLocations("/resources/image/");

        registry.addResourceHandler("/javascripts/**").addResourceLocations("/resources/javascripts/");

    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

There is really nothing else to show that I can think of. You can look at it on GIT for the other files if you so choose too.

真的没有别的东西可以证明我能想到的。如果您愿意,可以在GIT中查看其他文件。

3 个解决方案

#1


1  

I was playing with your application. (git repository helped to solve your configuration issues).

我在玩你的程序。(git存储库有助于解决配置问题)。

The problem was your mix of configuration properties and annotations. The following steps will help to restore default Spring Boot behavior.

问题是配置属性和注释的混合。以下步骤将帮助恢复默认的Spring引导行为。

  1. Move your index.htm to the webapp directory.
  2. 移动你的索引。htm到webapp目录。
  3. Copy all files from webapp/resources to the webapp
  4. 将所有的文件从webapp/资源复制到webapp。
  5. Remove the following configuration property - spring.mvc.static-path-pattern=/resources/*
  6. 删除以下配置属性- spring.mvc.static-path-pattern=/resources/*。
  7. Delete mapping to the / @RequestMapping(value="/", method= RequestMethod.GET) and the method itself. Boot will handle the index.html mapping to the ROOT. / mapping confused Boot Spring MVC auto configuration.
  8. 删除映射到/ @RequestMapping(值="/",方法= RequestMethod.GET)和方法本身。引导将处理索引。html映射到根。/映射混乱的启动Spring MVC自动配置。
  9. You can delete your WebConfig completely.
  10. 你可以完全删除你的WebConfig。

The next step would be to map static resource as you want:

下一步是将静态资源映射到您想要的位置:

public class AppMvcConfiguration extends WebMvcConfigurerAdapter {
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    // Including all static resources.

    registry.addResourceHandler("/assets/**", 
              "/css/**", 
              "/img/**",
              "/js/**"
         ).addResourceLocations("/assets/",
              "/css/", 
              "/img/",
              "/js/"
    ).resourceChain(true)
     .addResolver(new PathResourceResolver());

     super.addResourceHandlers(registry);
  }
}

See my git repository.

看到我的git存储库。

See more information about custom Spring MVC configuration.

有关自定义Spring MVC配置的更多信息。

#2


4  

Methods contactForm() and contactSubmit() return String,
try to use @RestController instead of @Controller
It fix my problem

方法contactForm()和contactSubmit()返回字符串,尝试使用@RestController而不是@Controller来解决我的问题。

#3


1  

There may be possibility that you are missing '@ResponseBody' on your method. for an example

在你的方法中可能有你丢失了“@ResponseBody”的可能性。为一个例子

@RequestMapping(value = "/myMethod", method = RequestMethod.POST, headers = "Accept=application/json") 
@ResponseBody 
public String myMethod(HttpServletRequest request, HttpServletResponse response) 
{ 
    // some business logic

    retutn "Hi" 
}

#1


1  

I was playing with your application. (git repository helped to solve your configuration issues).

我在玩你的程序。(git存储库有助于解决配置问题)。

The problem was your mix of configuration properties and annotations. The following steps will help to restore default Spring Boot behavior.

问题是配置属性和注释的混合。以下步骤将帮助恢复默认的Spring引导行为。

  1. Move your index.htm to the webapp directory.
  2. 移动你的索引。htm到webapp目录。
  3. Copy all files from webapp/resources to the webapp
  4. 将所有的文件从webapp/资源复制到webapp。
  5. Remove the following configuration property - spring.mvc.static-path-pattern=/resources/*
  6. 删除以下配置属性- spring.mvc.static-path-pattern=/resources/*。
  7. Delete mapping to the / @RequestMapping(value="/", method= RequestMethod.GET) and the method itself. Boot will handle the index.html mapping to the ROOT. / mapping confused Boot Spring MVC auto configuration.
  8. 删除映射到/ @RequestMapping(值="/",方法= RequestMethod.GET)和方法本身。引导将处理索引。html映射到根。/映射混乱的启动Spring MVC自动配置。
  9. You can delete your WebConfig completely.
  10. 你可以完全删除你的WebConfig。

The next step would be to map static resource as you want:

下一步是将静态资源映射到您想要的位置:

public class AppMvcConfiguration extends WebMvcConfigurerAdapter {
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    // Including all static resources.

    registry.addResourceHandler("/assets/**", 
              "/css/**", 
              "/img/**",
              "/js/**"
         ).addResourceLocations("/assets/",
              "/css/", 
              "/img/",
              "/js/"
    ).resourceChain(true)
     .addResolver(new PathResourceResolver());

     super.addResourceHandlers(registry);
  }
}

See my git repository.

看到我的git存储库。

See more information about custom Spring MVC configuration.

有关自定义Spring MVC配置的更多信息。

#2


4  

Methods contactForm() and contactSubmit() return String,
try to use @RestController instead of @Controller
It fix my problem

方法contactForm()和contactSubmit()返回字符串,尝试使用@RestController而不是@Controller来解决我的问题。

#3


1  

There may be possibility that you are missing '@ResponseBody' on your method. for an example

在你的方法中可能有你丢失了“@ResponseBody”的可能性。为一个例子

@RequestMapping(value = "/myMethod", method = RequestMethod.POST, headers = "Accept=application/json") 
@ResponseBody 
public String myMethod(HttpServletRequest request, HttpServletResponse response) 
{ 
    // some business logic

    retutn "Hi" 
}