• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Springboot Thymeleaf NOT rendering templates under src/main/resources/templates

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In initial phases of learning Spring,SpringBoot + its respective web framework MVC .. Springboot Thymeleaf NOT rendering templates under src/main/resources/templates

MVC: 1 Controller portion of source code:


1 View Portion of Source Code:



Respective Configuration Class:


BUT when pointing to http://localhost:8090/design: renders String object "design"...... NOT the respective Spring Thymeleaf template..as a check on the GET request handling method it is returning the respective Model objects..
 
Saloon Keeper
Posts: 15554
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!


You don't need any of the code in the Config class. Get rid of it.


Your problem is caused by wrong use of the annotations on your controller class. It should look like this:


The @Controller and @RequestMapping annotations should be on the controller class itself, not on the showDesignForm() method.

Don't put @ResponseBody on your controller action unless you want the return value to be interpreted as the actual HTTP response body. This is why your page displays the text "design", instead of forwarding the request to the design view.

In fact, you need never use @ResponseBody at all. If you want your controllers to return a JSON response body, create a separate controller for your web API and annotate it with @RestController instead of @Controller. This annotation implies that every action will behave as if you annotated it with @ResponseBody.

Finally, don't annotate the design parameter of the processDesign() action with @RequestBody. This will prevent your application from interpreting the submitted form correctly. Use @RequestBody only when your request body is something other than a HTML Form.
 
Partha Ray
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the results when :
1) @ResponseBody is omitted
HTTP Status 500 – Internal Server Error

Type Exception Report

Message Request processing failed: org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/design.html]")

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

jakarta.servlet.ServletException: Request processing failed: org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/design.html]")
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1019)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100


2) AND Config.java file is omitted
Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Mar 26 11:06:01 EDT 2024
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [design], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [design], template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869)
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1103) ...




AND THANKS MUCH!
 
Stephan van Hulst
Saloon Keeper
Posts: 15554
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.

Please show us your pom.xml file.
 
Partha Ray
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i attempt to paste in respective  xml source code with ... says sorry post is spam...also unable to attach xml file... thinking how to share xml file???

 
Marshal
Posts: 4518
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Posting the contents of the pom.xml file on Partha Roy's behalf:
 
Partha Ray
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Root Cause

java.io.FileNotFoundException: class path resource [templates/design.html] cannot be opened because it does not exist
org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:211)
org.thymeleaf.spring6.templateresource.SpringResourceTemplateResource.reader(SpringResourceTemplateResource.java:103)
org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:223)
org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100)
org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1103)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1077)
org.thymeleaf.spring6.view.ThymeleafView.renderFragment(ThymeleafView.java:372)
org.thymeleaf.spring6.view.ThymeleafView.render(ThymeleafView.java:192)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1415)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1159)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1098)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:974)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)


AND this is the bottom of STACK root cause when ONLY @ResponseBody omitted

src/main/resources/templates -> home.html. "design" is logical name for the view rendered with respective thymeleaf.
 
Partha Ray
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Root Cause

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'id' cannot be found on object of type 'com.example.demo.DomainTacoIngredient' - maybe not public or not valid?
org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:222)

so after i added design.html(identical to home.html) in addition to home.html... i get field property error
 
Stephan van Hulst
Saloon Keeper
Posts: 15554
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, the view name that you return from your controller action must match the name of the template file. Otherwise, Spring wouldn't know which template to load.

Duplicating your views doesn't make sense. Either name your template "home" and return "home" from the controller, or name the template "design" and return "design" from the controller.

All the properties that you use in your template must be public. You should make your DomainTacoIngredient and your DomainTacoDesign classes public, and their properties as well.
 
Partha Ray
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why do the data/properties/ fields HAVE to be public ? Is it not good practice to keep data private vs functionality public??
when i do make the data private.. then i get this stack trace error at bottom of stack:
Root Cause

org.springframework.beans.NotReadablePropertyException: Invalid property 'name' of bean class [com.example.demo.DomainTacoDesign]: Bean property 'name' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:626)
 
Stephan van Hulst
Saloon Keeper
Posts: 15554
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right, it IS good practice to keep things as private as possible.

Key phrase being "as possible".

The the entire purpose of your bean is to make the properties available to the template processor. Properties that are private are not available to the template processor.

Try this:

If that doesn't work, make both the class and the getName() method public.

If you're also using your class to get data from the submitted form (which you appear to be), then use this instead:

Again, if it complains about visibility, make the class itself, the parameterless constructor, and the getter and the setter public.
 
Partha Ray
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you ALL for your diagnostic help! Any other conceptual issues  moving forward as i am sure there will be will reach out . Thanks!
reply
    Bookmark Topic Watch Topic
  • New Topic