Coder Social home page Coder Social logo

spring-pug4j's People

Contributors

chbloemer avatar dbelyaev avatar kwon37xi avatar maxcom avatar naltatis avatar planetk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spring-pug4j's Issues

Unable to use templates from the classpath -- exception on "illegal char :"

Method named getResourceName(String name) from SpringTemplateLoader class will always throw an exception whenever user is trying to define path for template loader as classpath.

In order to reproduce:

  1. define SpringTemplateLoader as a Bean
  2. use it in PugConfiguration initialization step
  3. next try user tries to call renderTemplate method from pug configuration instance it will fail

initialization example:

    @Bean
    public SpringTemplateLoader templateLoader() {
        SpringTemplateLoader templateLoader = new SpringTemplateLoader();
        templateLoader.setTemplateLoaderPath("classpath:/templates/email");
        templateLoader.setEncoding("UTF-8");
        templateLoader.setSuffix(".pug");
        return templateLoader;
    }

    @Bean
    public PugConfiguration pugConfiguration() {
        PugConfiguration configuration = new PugConfiguration();
        configuration.setPrettyPrint(true);
        configuration.setTemplateLoader(templateLoader());
        return configuration;
    }

usage (that throws exception):

        // load template
        PugTemplate template = pugConfiguration.getTemplate("info-mail.pug");

        // render to html
        String htmlMessage = pugConfiguration.renderTemplate(template, model);

exception (thrown when trying to parse classpath:/templates/email/ path via sun.nio.fs.WindowsPathParser.normalize):
"Illegal char <:>"

Am I using it wrongly or it is not possible to read templates from classpath by default?

I see there is a ClasspathTemplateLoader that gives me the same issue, when I substitute SpringTemplateLoader with it (using ClasspathTemplateLoader.setTemplateLoaderPath("/templates/email");).

What should be file structure and TemplateLoaderPath?

I am getting

I am using Spring Boot and my folder structure like that: There was an unexpected error (type=Not Found, status=404).

I put a breakpoint to return and breakpoint hit.

@Controller
public class IndexController {

    @GetMapping()
    public String greeting() {
        return "index";
    }
}

My file structure:
fileStructure

I tried these combinations but no luck.

 @Bean
    public SpringTemplateLoader templateLoader() {
        SpringTemplateLoader templateLoader = new SpringTemplateLoader();
        // templateLoader.setTemplateLoaderPath("/WEB-INF/");
        // templateLoader.setTemplateLoaderPath(
        // "/Users/emre/Documents/Free/SocketServer/socketserver/src/main/resources/templates/");
        //  templateLoader.setTemplateLoaderPath("/Users/emre/Documents/Free/SocketServer/socketserver/src/main/resources/templates/");
        templateLoader.setTemplateLoaderPath("/WEB-INF/resources/");

        templateLoader.setEncoding("UTF-8");
        templateLoader.setSuffix(".pug");
        return templateLoader;
    }

Use jakarta servlet api instead of javax servlet api for compatibility with new spring versions

Currently, I am trying to use pug with Spring 6.0.3, but I'm running into this exception:

java.lang.AbstractMethodError: Receiver class de.neuland.pug4j.spring.view.PugView does not define or inherit an implementation of the resolved method 'abstract void renderMergedTemplateModel(java.util.Map, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)' of abstract class org.springframework.web.servlet.view.AbstractTemplateView

I did some digging in and the only change necessary should be to change this dependency:

<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>javax.servlet-api</artifactId>
        <version>${servlet-api-version}</version>
         <scope>provided</scope>
</dependency>

to:

<dependency>
        <groupId>jakarta.servlet</groupId>
        <artifactId>jakarta.servlet-api</artifactId>
        <version>6.0.0</version>
        <scope>provided</scope>
</dependency>

and then in the PugView class, the HttpServletRequest and HttpServletResponse should be imported from the jakarta.servlet.http package.

Can't set GraalJsExpressionHandler as expression handler

Bean instantiation fails when in try to set the expressionhandler to GraalJsExpressionHandler as shown in the JavaConfig code example.

@configuration
public class PugConfig {

@Bean
public SpringTemplateLoader templateLoader() {
    SpringTemplateLoader templateLoader = new SpringTemplateLoader();
    templateLoader.setTemplateLoaderPath("classpath:/templates");
    templateLoader.setEncoding("UTF-8");
    templateLoader.setSuffix(".pug");
    return templateLoader;
}

@Bean
public PugConfiguration pugConfiguration() {
    PugConfiguration configuration = new PugConfiguration();
    configuration.setCaching(false);
    configuration.setTemplateLoader(templateLoader());
    //To use the new GraalJsExpressionHandler add this:
    configuration.setExpressionHandler(new GraalJsExpressionHandler());
    return configuration;
}

}

Note : Commenting the line which sets expression handler works.

Can't load styles

I Try to load style from my resources folder (/resources/static/css/styles.css) but get 404.

@Configuration
@EnableWebMvc
public class PugTemplateConfiguration {
    @Bean
    public SpringTemplateLoader templateLoader() {
        SpringTemplateLoader templateLoader
                = new SpringTemplateLoader();
        templateLoader.setTemplateLoaderPath("classpath:/templates/");
        templateLoader.setEncoding("UTF-8");
        templateLoader.setSuffix(".pug");
        return templateLoader;
    }
    @Bean
    public PugConfiguration pugConfiguration() {
        PugConfiguration configuration = new PugConfiguration();
        configuration.setCaching(false);
        configuration.setTemplateLoader(templateLoader());
        return configuration;
    }
    @Bean
    public ViewResolver viewResolver() {
        PugViewResolver viewResolver = new PugViewResolver();
        viewResolver.setConfiguration(pugConfiguration());
        return viewResolver;
    }
}

templates/index.pug

doctype html
html
  head
    meta(charset="UTF-8")
    link(rel="stylesheet" type="text/css" href="css/style.css")
    title document title
  body
    p hello, world!

styles located in resources/static/css/styles.css, in target folder I can see it in /static/css/styles.css

spring log:

DEBUG org.springframework.web.servlet.DispatcherServlet : GET "/css/style.css", parameters={} 
WARN org.springframework.web.servlet.PageNotFound : No mapping for GET /css/style.css 
DEBUG org.springframework.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND

I tried to move style.css to 'templates' folder, but it did not help

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.