2016-08-02 94 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看不到像使用私有方法那樣的屬性。 –