neuland / spring-pug4j Goto Github PK
View Code? Open in Web Editor NEWpug4j spring integration
pug4j spring integration
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:
SpringTemplateLoader
as a BeanPugConfiguration
initialization steprenderTemplate
method from pug configuration instance it will failinitialization 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");
).
Hello, @chbloemer,
Can you, please, release new version of spring-pug4j
that uses latest pug4j dependency v2.1.0?
SpringTemplateLoader templateLoader = new SpringTemplateLoader();
templateLoader.setTemplateLoaderPath("/WEB-INF/views/");
@chbloemer, can you, please, release spring-pug4j
of version v2.0.4
that uses corresponding pug4j version?
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";
}
}
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;
}
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.
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.
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
The current version of spring-pug4j (3.0.0) is using an outdated dependency, org.springframework:[email protected]
, which is known to have vulnerabilities:
SEVERITY | VULNERABILITY | CWE | CVE | SNYK | CVS SCORE | FIXED IN |
---|---|---|---|---|---|---|
HIGH | Improper Access Control | CWE-284 | CVE-2023-20860 | SNYK-JAVA-ORGSPRINGFRAMEWORK-3369852 | CVSS 9.1 | org.springframework:spring-webmvc , @5.3.26 , @6.0.7 and higher |
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.