Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot + thymeleaf in IntelliJ: cannot resolve vars

I'm writing a short web form application using spring boot and thymeleaf on IntelliJ, but it seems that in the html file, all fields in the model cannot be resolved. Here is my code:

Controller class:

@Controller public class IndexController{      @RequestMapping(value = "/", method = RequestMethod.GET)     public String index(){         return "index";     }      @RequestMapping(value="/", method = RequestMethod.POST)     public String addNewPost(@Valid Post post, BindingResult bindingResult, Model model){         if(bindingResult.hasErrors()){             return "index";         }         model.addAttribute("title",post.getTitle());         model.addAttribute("content",post.getContent());         return "hello";     } } 

Model Class:

public class Post {      @Size(min=4, max=35)     private String title;      @Size(min=30, max=1000)     private String content;      public String getTitle() {         return title;     }      public void setTitle(String title) {         this.title = title;     }      public String getContent() {         return content;     }      public void setContent(String content) {         this.content = content;     } } 

Then is the index.html:

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head lang="en">      <title>Spring Framework Leo</title>     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> </head> <body>  <h3>Spring Boot and Thymeleaf</h3>       <form action="#" th:action="@{/}"  th:object="${post}" method="post">         <table>             <tr>                 <td>Title:</td>                 <td><input type="text" th:field="*{title}" /></td>                 <td th:if="${#fields.hasErrors('title')}" th:errors="*{title}">Title error message</td>             </tr>             <tr>                 <td>Content:</td>                 <td><input type="text" th:field="*{content}" /></td>                 <td th:if="${#fields.hasErrors('content')}" th:errors="*{content}">Content error message</td>             </tr>             <tr>                 <td><button type="submit">Submit post</button></td>             </tr>         </table>     </form> 

There are always red lines under "post", "title" and "content", but I don't know how to solve it. Is it a problem of IntelliJ or just a problem of my code?

like image 318
Shuhan Liu Avatar asked Aug 02 '16 01:08

Shuhan Liu


People also ask

How do I enable Thymeleaf in Spring Boot?

1.1. Spring Boot will provide auto-configuration for Thymeleaf. Add spring-boot-starter-thymeleaf dependency in pom. xml to enable this auto-configuration. No other configurations required, Spring Boot will inject all required configuration to work with Thymeleaf.

How do I declare a variable in Thymeleaf?

We can use the th:with attribute to declare local variables in Thymeleaf templates. A local variable in Thymeleaf is only available for evaluation on all children inside the bounds of the HTML tag that declares it.

How do I add thymeleaf to IntelliJ?

Right-click the project or the module folder and select Add Framework Support. In the left-hand pane of the Add Frameworks Support dialog that opens, select the Thymeleaf checkbox. Show activity on this post. Once I removed these properties, the spring mvc mappings are detected by Intellij again (in the Ultimate version, I'm using 2018.1).

How thymeleaf works in Spring Boot?

- GeeksforGeeks Spring Boot – How Thymeleaf Works? Thymeleaf is a Java library, template engine used to parse and render the data produced by the application to template files – thus providing transformation. It is just like HTML but is provided with more attributes for working with rendered data.

Why can't IntelliJ find the model variables in Spring Boot?

Basically IntelliJ is unable to locate the model variables when Spring Boot has been used to autoconfigure everything. Show activity on this post. I want to add one more thing.

How to use thymeleaf view for Spring MVC?

When Spring-Boot’s autoconfiguration detects Thymeleaf in the classpath, it creates beans supporting Thymeleaf view for Spring MVC. It can work with request attributes of Servlet. Therefore, Spring copies the model data into request attributes that the Thymeleaf template can work with. To use Thymeleaf, add its dependency in the project build.


1 Answers

I had two different portions of code: the first was showing the error and the second was not doing it. I observed that there is a difference in the xmlns:th attribute.

First Page: Not working!

<html xmlns="http://www.w3.org/1999/xhtml"       xmlns:th="http://www.thymeleaf.org"> 

Second Page: Working!

<html xmlns="http://www.w3.org/1999/xhtml"       xmlns:th="http://thymeleaf.org"> 

I removed the www. and it works for me!

like image 64
Mustapha El Kojji Avatar answered Sep 23 '22 08:09

Mustapha El Kojji