Loading templates from external source

classic Classic list List threaded Threaded
42 messages Options
123
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

arrahman
Thanks, dear Emanuel.
let me try it.
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

arrahman
Hi Emanuel,
I hope you would be fine.
I have question regarding the path for jar file where i have to put it. As i mentioned that i have i two project one is creating the jar with html file and in the second project i want to view the content inside of the jar file in the thymeleaf template.

so now i have a jar file but i don't know where exactly i have to put it to set the prefix in the configuration.

Please guide me.

Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

Emanuel
Administrator
I'm going to assume the JAR is in the classpath of the second project since the discussion so far is around the ClassLoaderTemplateResolver.  With that, then the prefix should include things like the path to the package the template is on, eg: if the template lives in the com.mywebsite package then an appropriate prefix would be "com/mywebsite".

I just noticed the Thymeleaf Tutorial doc makes a small mention of the ClassLoaderTemplateResolver, so might provide some hints as to what the appropriate prefix values should be: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#template-resolvers
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

arrahman
Dear Emanuel,
The second question, I want to access the vis.html from the jar(visualizer) to this home.html Can i call the whole vis.html from the jar file to the home.html?

?

Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

Emanuel
Administrator
Ah, that pciture helps explain things :)

When I said "JAR as part of the classpath", I imagined it was either included as a dependency of your project, or included using the -cp flag when running a program from the command line, or inside a web application's WEB-INF/lib folder.  For a Spring Boot project, I'm not sure how to add a non-Maven JAR to the project.  One way might be to install the JAR to your local repository, which is outlined here: http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html  Then you can include that JAR as a dependency and use the ClassLoaderTemplateResolver as I mentioned before.
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

arrahman
This post was updated on .
Thanks.
For adding a third party jar I have found a video on youtube which is relevant to your provided link: https://www.youtube.com/watch?v=oFfnJ1yoDWc
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

arrahman
I added the ClassLoaderTemplateResolver in my main method which looks like in below Screenshot. I am new to spring boot that's why I asking many questions but I appreciate your help. So should I make a config file to add ClassLoaderTemplateResolve? or it's ok in the main method?



Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

Emanuel
Administrator
In Spring Boot, I think if you define your template resolver as an @Bean, then it'll be picked up and added to the template engine.  So in your @SpringBootApplication class:

@Bean
pulic TemplateResolver templateResolver() {
  ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
  // All your config here...
  return templateResolver;
}
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

arrahman
Thanks.
I am able to show the contents of the Iview project but I am not able to pull out the Vis.html from the jar inside the Iview project. The jar is actually created in the second project "Visualizer".
Here you can see both the project in the screenshot.

so only I am left with this task where I have to call the vis.html to the Iview project and show it on home.html.
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

arrahman
This post was updated on .
In my controller, I am returning
@RequestMapping(value="/")
        public String index(){

            return "vis";
        }
This shows an error "Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Dec 22 02:21:56 CET 2019
There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "templates/vis.html")"

or Is there any concept in Thymeleaf to call the vis.html to home.html through tags or i have to use like in the above controller?
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

Emanuel
Administrator
With the error message of "An error happened during template parsing (template: "templates/vis.html")", I'm led to believe that it managed to pull out vis.html, but Thymeleaf ran into an error while parsing vis.html.  The full stack trace should have the cause of the error which should have more details, like if it an expression wasn't balanced correctly (like missing a closing }) or it encountered some kind of null error.
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

arrahman
yes, you are right. if I remove the ClassLoaderTemplateResolver from the main application class the error is something like in the below Screenshot. it means the Iview side is able to pull out the HTML file from the jar.



Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

arrahman
Let me try a new jar file. if I have a newly created jar so every time I have to add it as a 3rd party jar into a Maven dependency?
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

arrahman
Hi Emanuel,
In the console the error is "java.io.FileNotFoundException: ClassLoader resource "templates/Vis.html" could not be resolved" have you any idea about it?


Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

Emanuel
Administrator
The JAR needs to be included as a project dependency, like what you did following the video before, for the ClassLoaderTemplateResolver to be able to look into it.  If it's sitting in the resources directory, then it won't be expanded as part of the classpath and so the ClassLoaderTemplateResolver won't find it.

If you really want to have your JAR sitting in the resources directory, then you'll need to write a custom template resolver (implement the ITemplateResolver interface or extend AbstractTemplateResolver) to read the JAR, and then peek into its contents to extract the necessary template.
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

arrahman
Thank you very much.
you are really very co-operative.

For the ClassLoaderTemplateResolver where exactly I have to put the jar file to be counted as a classpath? I am just asking about it but I am also trying to configure ITemplateResolver.

and one thing more in my controller in return it shows "Cannot resolve MVC view 'vis' and actually this 'vis' is in the jar file and MVC cannot see it directly so what you say maybe because of this the error "template could not be resolved is coming"

Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

Emanuel
Administrator
To have the JAR on the classpath so the ClassLoderTemplateResolver can work, it needs to be included as a dependency of your project.  For example, if you're using Gradle, then dependencies live in a section like this in the build.gradle file:

dependencies {
  implementation 'org.example:some-jar:1.0.0'
}

For Maven, it'll be in the pom.xml file, probably in the <dependencies> section, though I'm not very good with Maven so can't give a good example.

(Didn't you have this going after you followed that video?  Your error page was talking about errors in the vis.html template file?)

As for IntelliJ not being able to resolve "vis", I wouldn't worry about that as you've got a special case trying to load templates from a JAR - IntelliJ I think only scans for template files, not files inside other files.
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

arrahman
This post was updated on .
In the console, the first error is about parsing and the second error is about vis template resolving
I followed the video and I added the jar using the terminal using the following command first time :

mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=src/main/resources/templates/visualizer-0.0.1-SNAPSHOT.jar

Later also tried this one:

mvn install:install-file -Dfile=src/main/resources/templates/visualizer-0.0.1-SNAPSHOT.jar -DgroupId=com.openlap -DartifactId=visualizer -Dversion=0.0.1-SNAPSHOT-Dpackaging=jar -DgeneratePom=true

and I updated the pom file also:

<dependency>
   <groupId>com.openlap</groupId>
   <artifactId>visualizer</artifactId>
   <version>0.0.1-SNAPSHOT</version>
</dependency>

So should I try in another IDE?
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

miyabhai101
This post was updated on .
In reply to this post by Emanuel
Emanuel wrote
Yes, you can load templates from anywhere provided you have the appropriate TemplateResolver for the job.  Thymeleaf comes with 4 template resolvers:
 - ClassLoaderTemplateResolver
 - FileTemplateResolver
 - ServletContextTemplateResolver
 - UrlTemplateResolver

Most of the examples you've seen probably use the ServletContextTemplateResolver, which can load anything from the context root of your web application.  I've used it to load stuff from the main web content directory instead of WEB-INF - just a personal preference.

The other template resolvers should be self-explanatory, and I remember someone asking if they could load a template from a database...  *does a search*  Ah, here it is: http://forum.thymeleaf.org/Loading-thymeleaf-template-from-Database-td4025164.html
hello,

Thank you so much for sharing this helpful information, it helped me alot

Thanks and regards.:) tubemate get-mobdro.com authorityapk.com
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from external source

arrahman
In reply to this post by Emanuel
dear Emanual,

I want to generate runtime controls like dropdown in the thymeleaf, for example, I have a string in the form of HTML and JavaScript in an object in the controller and when I inject that string into the thymeleaf so i want to create runtime control for that string because in the string i use the event  for dropdown. I can inject the select button also from the backend to the thymleaf but I want that thymleaf generate it when the template detect the string coming from the controllers.


Thanks in advance

123