2016-08-02 92 views
8

我正在使用IntelliJ上的spring启动和thymeleaf编写一个简短的web表单应用程序,但似乎在html文件中模型中的所有字段都无法解析。这里是我的代码:IntelliJ中的春季启动+百里香:无法解决变数

控制器类:

@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"; 
    } 
} 

模型类:

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; 
    } 
} 

然后就是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> 

总是有下红线“帖子“,”标题“和”内容“,但我不知道如何解决它。这是IntelliJ的问题还是只是我的代码问题?

+0

这应该在IntelliJ 2013.3中解决,请参阅我编辑的答案。 –

回答

12

这是IntelliJ问题:IDEA-132738

基本上,当Spring Boot用于自动配置所有内容时,IntelliJ无法找到模型变量。

+1

我正在使用IntelliJ 2017.2,并且此问题在IDE中仍存在。当我访问我的html文件时,这非常占优势。为什么JetBrains的人还没有解决这个Thymeleaf支持? – MAC

+0

@MAC这应该在IntelliJ 2013.3中修复,请参阅我编辑的答案。 –

+0

我们只能期待最好的。不管怎样,谢谢!!! – MAC

8
  1. 如果您的IntelliJ版本< 2017.3,它是作为Andrew wrote,已知错误IDEA-132738。有一个解决方法是如何摆脱IDE中的错误标记。的IntelliJ也支持半自动生成下面提到代码:

您可以使用Alt键 + 输入快捷方式,以便调用意图“声明中评论注解外部变量”摆脱在您的观点中“未解决的模型属性”。从thymeleaf-extras-java8time

如果您使用扩展对象由ThymeLeaf自动构造,如#temporals转换java.time对象:

下面的代码添加到您的html文件

<span th:text="${#temporals.format(person.birthůDate,'yyyy-MM-dd')}"></span> 

并且IntelliJ无法解析它们,请使用类似的代码,并在对象名称前加上#

<[email protected] id="#temporals" type="org.thymeleaf.extras.java8time.expression.Temporals"--> 
  • 如果您的IntelliJ版本为> = 2017.3,这个问题应该是固定的(@FloatOverflow:“我可以证实,在版本2017.3打造25.Oct.2017的问题已经解决“):
  • 状态2017.3

    支持春季启动自动配置的MVC应用程序完成后,所有自动配置捆绑视图类型的支持。

    修复版本:2017。3

    +0

    我确认在版本2017.3 build 25.Oct.2017中问题已解决! – FloatOverflow

    +1

    @FloatOverflow我正在运行2017.3,并且我的对象仍然带有下划线。任何想法我可能做错了什么?例如,在下面的代码中'test'和'name'都有下划线。 '

    ' – justinraczak

    +0

    @justinraczak我建议您创建一个最小化,完整且可验证的示例(https ://stackoverflow.com/help/mcve)并将其报告给Jetbrains。您可以将其添加到上述问题中,或创建一个新问题。 –

    1

    我想补充一点。如上所述,该问题已在IntelliJ 2017.3中得到修复。我也可以证实这一点。

    但是,我注意到,只有在负责的控制器功能中定义了所有属性(例如,这样的:

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

    如果您使用的是你定义模型属性的一个子功能(见例下面),可以的IntelliJ仍没有找到在HTML模板的属性。

    @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"; 
    } 
    

    因此,始终确保你把你的代码中直接查看功能里面!

    +0

    有帮助和/或如果您的RequestMappings位于Kotlin中,则IDE看不到像使用私有方法那样的属性。 –