简体   繁体   English

Spring thymeleaf静态内容HTTP 405

[英]spring thymeleaf static content HTTP 405

I am working with spring boot an thymeleaf and try to get an image from the application. 我正在使用百里香春季靴子,尝试从应用程序中获取图像。

   <img th:src="@{/static/img/png-test.png}"/>

this is my tag in my template file. 这是我模板文件中的标签。 which gets rendered to 呈现给

<img src="/static/img/png-test.png">

i already tried to add my own ResourceHandler 我已经尝试添加自己的ResourceHandler

  @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}

which seems to work because on startup i get the following message 这似乎有效,因为在启动时我收到以下消息

2017-08-04 20:16:02.926  INFO 15234 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/static/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

but there is no image showing up inside the browser. 但浏览器中没有显示图像。 instead i get the following error in the browser console 相反,我在浏览器控制台中收到以下错误

GET http://localhost:8080/static/img/png-test.png 405 ()

Spring logs the following Spring记录以下内容

2017-08-04 19:59:20.265  WARN 14705 --- [nio-8080-exec-5] o.s.web.servlet.PageNotFound             : Request method 'GET' not supported

EDIT: Could it be a problem that i added this to my build.gradle file 编辑:可能是我将其添加到build.gradle文件中的问题

jar {
    from('src/main/') {
        include 'static/'

    }
}

because otherwise gradle never put the static folder into the jar 因为否则gradle永远不会将静态文件夹放入jar

EDIT: I found out what is needed to reproduce the failure 编辑:我发现了重现失败所需的内容

i have a function inside my controller wich looks like this 我的控制器内部有一个函数,看起来像这样

@RequestMapping(name = "/", method = RequestMethod.POST)
public String func(@RequestParam(name = "var1") String var1, @RequestParam(name = "var2") String var2){
    return "templatename";
}

When i uncomment the function i am able to receive static content. 当我取消注释该功能时,我可以接收静态内容。 at least method not allowed makes sense now. 至少现在不允许使用方法。 because it is expecting a POST. 因为它正在等待POST。

So how can i fix this without to remap the function? 那么,如何在不重新映射功能的情况下解决此问题?

By default, Spring Boot will serve static resources from either your_app_dir/srce/main/resources/static or your_app_dir/srce/main/resources/public . 默认情况下,Spring Boot将提供来自your_app_dir/srce/main/resources/staticyour_app_dir/srce/main/resources/public your_app_dir/srce/main/resources/static your_app_dir/srce/main/resources/public If you are putting your static resources elsewhere, move the static resources into one of these directories and you should be good to go. 如果要将静态资源放在其他位置,请将静态资源移动到这些目录之一中,您应该会很好。

I just noticed that i was just stupid. 我只是注意到我只是愚蠢。 i wrote 我写

@RequestMapping(name = "/", method = RequestMethod.POST)

instead of 代替

@RequestMapping(value = "/", method = RequestMethod.POST)

over a function in my controller. 在我的控制器中的功能上。 which mapped this function to /** So my requests to any static content was routed to this function which only accepted the POST method. 该函数将此函数映射到/**因此我对任何静态内容的请求都路由到该函数,该函数仅接受POST方法。

But still a big thnx to @Kirby 但是对于@Kirby还是很大的感谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM