相关疑难解决方法(0)

使用Spring Boot和Thymeleaf加载静态资源

我想加载我的静态资源.首先我认为它已经有效,但这只是浏览器缓存的一个技巧.我只按预期加载了html文件,但我没有得到js,css,图像等等.

======

我的StartClass:

@Configuration
@Import({ ServiceConfig.class, WebMvcConfig.class })
@EnableHypermediaSupport(type = HAL)
@EnableAutoConfiguration
public class ApplicationClientMvc {
    public static void main(final String[] args) {
        SpringApplication.run(ApplicationClientMvc.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

======

WebMvcConfig

@Configuration
@ComponentScan
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Autowired public SpringTemplateEngine templateEngine;

    @Bean
    public ThymeleafTilesConfigurer tilesConfigurer() {
        final ThymeleafTilesConfigurer configurer = new ThymeleafTilesConfigurer();
        configurer.setDefinitions("classpath*:/templates/**/views.xml");
        return configurer;
    }

    @Bean
    public ThymeleafViewResolver thymeleafViewResolver() {
        final ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setViewClass(ThymeleafTilesView.class);
        resolver.setTemplateEngine(templateEngine);
        resolver.setCharacterEncoding(UTF_8);
        return resolver;
    }

    @Bean
    public TilesDialect tilesDialect() {
        return new …
Run Code Online (Sandbox Code Playgroud)

configuration static-resource thymeleaf spring-boot

6
推荐指数
2
解决办法
2万
查看次数

使用Spring和Thymeleaf的静态资源

我正在尝试使用以下代码访问HTML上的静态资源:

<link th:href="@{css/main.css}" rel="stylesheet" type="text/css" />
Run Code Online (Sandbox Code Playgroud)

但是当我放进去时才起作用@{static/css/main.css}。我知道,当您设置资源文件夹时,无需在每次调用静态文件时都设置静态文件夹。

我的文件夹结构:

/webapp
=== /static
==========/css
==========/js
=== /WEB-INF
==========/views
Run Code Online (Sandbox Code Playgroud)

在Spring上设置mvc配置:

....
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

    private TemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver());
        return engine;
    }

    private ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".html");
        resolver.setCacheable(false); // On production , turn TRUE
        resolver.setTemplateMode(TemplateMode.HTML); …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc thymeleaf

5
推荐指数
1
解决办法
6748
查看次数

不在Spring引导,Thymeleaf和AngularJs应用程序中加载静态资源

我有一个Spring Boot项目,必须启动角度水疗.资源文件夹的结构如下:

在此输入图像描述

在templates/src文件夹中有我从控制器开始的index.html文件:

@RequestMapping("/")
String index() {
    return "src/index";
}
Run Code Online (Sandbox Code Playgroud)

通过这种方式,我成功启动了index.html,但所有的反应都在:

  • bower_components
  • SRC/JS
  • SRC/CSS

未正确加载.所以我添加了配置:

@Configuration
public class WebConfig extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {

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

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(    "/js/**")
                .addResourceLocations(  "/templates/src/js/");
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我仍然无法加载所有需要的资源,因为我得到了404

在此输入图像描述

我不明白我做错了什么.

这里也是index.html:

<!DOCTYPE html>
<html lang="en" data-ng-app="app">
<head>
  <meta charset="utf-8" />
  <title>Angular version | Angulr</title>
  <meta name="description" content="Angularjs, Html5, Music, Landing, 4 in 1 ui kits package" />
  <meta name="keywords" content="AngularJS, angular, bootstrap, admin, dashboard, panel, app, charts, …
Run Code Online (Sandbox Code Playgroud)

spring thymeleaf spring-boot

3
推荐指数
1
解决办法
8666
查看次数

Spring 4 静态内容如 css/js 带来错误 405 Request method 'GET' not supported

我已经检查过这个问题,但是在尝试了 4 个小时之后,没有任何东西对我有用。

尝试访问我的 css 文件时出现 405 错误。这是我的 Config.java

package com.myapp.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration
@ComponentScan("com.myapp")
@EnableWebMvc
@EnableTransactionManagement
public class Config extends WebMvcConfigurerAdapter {

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/","/css/","/js/");

    }

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

}
Run Code Online (Sandbox Code Playgroud)

和目录结构:

META-INF
WEB-INF
resources
    |-css
    | …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-4

1
推荐指数
1
解决办法
3991
查看次数