Java – how do I access stylesheets in Library jar files from the thymeleaf template?

I have a set of spring - based Java Web applications and a common framework library available through jar files I'm using thymeleaf to express my views

I have a common thymeleaf template used by several web applications The template HTML file and its associated stylesheet are in the library jar file When a web application displays a view based on a framework template, thymeleaf finds and processes the template

My problem is that I can't figure out how to import the stylesheet so that it can be loaded and passed back to the browser The end result is Unstyled (though correct) HTML

I'm using gradle as my build tool

This is the relevant project structure of my framework:

common-framework/src/main
    ../java                        - contains the framework code
    ../resources                   - contains the framework resources
       ../static
           ../css
               example.css         - Style sheet for use in templates
       ../templates
           ../fwk
               example.html        - Thymeleaf template

This is the relevant project structure for my sample application:

app/src/main
    ../java                        - contains the application code
    ../resources                   - contains the application resources
       ../templates
           ../app
               start.html          - Thymeleaf template
    ../webapp
        ../resources
            ../css
               app.css             - Style sheet for use in the app

This is the viewresolver bean that applications and frameworks use to find templates:

@Bean
public ViewResolver viewResolver()
{
    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setTemplateMode("HTML5");
    templateResolver.setPrefix("templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setOrder(1);

    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.setTemplateResolver(templateResolver);

    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(engine);
    viewResolver.setCharacterEncoding("UTF-8");
    viewResolver.setOrder(1);

    return viewResolver;
}

This is my resource handler configuration (from my webmvcconfigureradapter):

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

This is the framework thymeleaf template (FWK / example. HTML)

<!DOCTYPE html SYstem "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<head>
    <Meta charset="utf-8" />
    <title th:text="${title}"></title>
    <link rel="stylesheet" type="text/css" th:href="@{/resources/static/css/framework.css}" />   <!-- *** PROBLEM IS HERE *** -->
</head>
<body>
    ...
</body>
</html>

This is an application thymeleaf template (APP / start. HTML)

<!DOCTYPE html SYstem "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<head>
    <Meta charset="utf-8" />
    <title th:text="${title}"></title>
    <link rel="stylesheet" type="text/css" th:href="@{/resources/css/app.css}" />   <!-- *** This works correctly. *** -->
</head>
<body>
    ...
</body>
</html>

This is a sample controller from a web application Note: This is a stand - alone project that depends on the framework library

@Controller
public class ExampleController
{
    @GetMapping(path = "/start")
    public String start(Model model,HttpServletRequest request)
    {
        ...
        return "app/start"; // Uses an application template
    }

    @GetMapping(path = "/example")
    public String example(Model model)
    {
        ...
        return "fwk/example"; // Uses a framework template
    }
}

The web application runs under tomcat, and the framework library jar file is available for it When I tested the controller URL, both loaded correctly That is, in both cases, templates are found, loaded, and displayed

However, as I mentioned above, the framework template (triggered by / example URL) is non styled because the framework CSS file cannot be found and passed to the browser How can I achieve this goal?

(is my framework structure normal? Is my viewresolver configured correctly? Is my resource processing configured correctly? What href path should I use in the framework template to reference the style sheet in the same jar?)

Solution

In example Try replacing in HTML:

<link rel="stylesheet" type="text/css" th:href="@{/resources/static/css/framework.css}" />

By:

<link rel="stylesheet" type="text/css" th:href="@{/css/framework.css}"/>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>