Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 240 Vote(s) - 3.58 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spring boot + thymeleaf in IntelliJ: cannot resolve vars

#1
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?
Reply

#2
This is a problem with IntelliJ: [IDEA-132738][1].

Basically IntelliJ is unable to locate the model variables when Spring Boot has been used to autoconfigure everything.

[1]:

[To see links please register here]

Reply

#3
I want to add one more thing. As stated above, the issue has been fixed in **IntelliJ 2017.3**. I can also confirm this.

However, I noticed that this is only true if you define all your attributes **directly** inside the responsible controller function, like e.g. this:

@RequestMapping(value = "/userinput")
public String showUserForm(Model model){
model.addAttribute("method", "post");
model.addAttribute("user", new User());
return "userform";
}

If you are using a sub-function in which you define the model attributes (see **Example** below), IntelliJ can still not find the attributes in the HTML template.

**Example**:

@RequestMapping(value = "/userinput")
public String showUserForm(Model model){
return doIt(model);
}

private String doIt(Model model) {
model.addAttribute("method", "post");
model.addAttribute("user", new User());
return "userform";
}

So, always make sure you put your code directly inside the view function!
Reply

#4
This feature is supported in the Ultimate edition only.

[Click here][1] for more details


[1]:

[To see links please register here]

Reply

#5
In my case the problem was that I had the following in my application.properties:

spring.thymeleaf.prefix=file:src/main/resources/templates/

spring.thymeleaf.cache=false

Once I removed these properties, the spring mvc mappings are detected by Intellij again (in the Ultimate version, I'm using 2018.1). Also the thymeleaf objects are working now.

I used these properties to support fast development where a refresh would reload the thymeleaf template files.

To solve this issue, I use the following -D option in my run configuration of my spring boot application to tell spring boot where my property files are during development:

-Dspring.config.location=/dev/application/conf/application.properties


Reply

#6
There is one more interesting situation ,if you DONT return directly the object from your controller ,Intellij will not recognize the variable ,but she will still work
Reply

#7
Manually enabling it, worked for me in Intellij 2018.1.6 Ultimate Edition. Steps followed

1. Open the Project tool window (e.g. View | Tool Windows | Project).

2. Right-click the project or the module folder and select Add
Framework Support.
3. In the left-hand pane of the Add Frameworks
Support dialog that opens, select the Thymeleaf checkbox.

Official reference :

[To see links please register here]

Reply

#8
This problem comes from thymeleaf web link.

The problem:`xmlns:th="http://www.thymeleaf.org"`

[![This is problem link:][1]][1]

[![This is error area:][2]][2]

The solution: `xmlns:th="http://thymeleaf.org"`

[![Solution link][3]][3]

[![Error is gone][4]][4]


[1]:

[2]:

[3]:

[4]:
Reply

#9
For those who come from ReactJS and want to use Springboot as a backend.

I forgot to add html boilerplate code in the html, that's why I'm seeing this error. don't forget adding boilerplate code.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTML 5 Boilerplate</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<script src="index.js"></script>
</body>
</html>
Reply

#10
This was happening to me on Ultimate 2021.2.1.

In one `@GetMapping` method, I used `@ModelAttribute('transactionForm')`. The Thymeleaf page for that method didn't have errors.

But in another `@GetMapping` method, I didn't use `@ModelAttribute('newCustomerForm')`. The Thymeleaf page for that method had the errors. When I added the `@ModelAttribute` to the method, the errors went away. And when I took it out, the errors came back.

I know Spring MVC doesn't require the `@ModelAttribute` if the name of the model is the same as the class name. So I shouldn't have to do this, and I'm only doing this to appease the IDE.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through