2017-02-10 34 views
2

thymeleaf 3.0.3とSpring Boot 1.5.1のテンプレートで#requestや#responseなどのWebコンテキストオブジェクトのメソッドを呼び出そうとしています。thymeleafテンプレートのWebコンテキストオブジェクトのメソッドの呼び出しに関する問題

私は継続的にこのようなエラーが出る:

org.springframework.expression.spel.SpelEvaluationException:EL1011E:メソッドの呼び出し:ヌルコンテキストオブジェクトのメソッドメソッド(java.lang.String)を呼び出すようにしようとしました

ここではコントローラーです:

@Controller 
public class Controller { 

@RequestMapping(method = RequestMethod.GET, value = "/endpoint", produces = "text/html") 
public String customerServiceSignin(Model uiModel, HttpServletRequest request) { 
    uiModel.addAttribute("attr1", true); // show proper header 
    uiModel.addAttribute("attr2", false); 
    return "template"; 
} 

とテンプレート:

<html xmlns:th="http://www.thymeleaf.org"> 
<div> 
<div style="..."> 

    <div class="errorblock" th:unless="${#strings.isEmpty(#request.getAttribute('some_attr'))}" th:utext="${#request.getAttribute('some_other_attr')}"></div> 

    <form name='f' action="action" method='POST'> 
     <table> 
      <tr> 
       <td>User:</td> 
       <td><input type='text' name='username' value="" /> 
       </td> 
      </tr> 
      <tr> 
       <td>Password:</td> 
       <td><input type='password' name='password' /> 
       </td> 
      </tr> 
      <tr> 
       <td><input name="submit" type="submit" value="submit" /> 
       </td> 
       <td><input name="reset" type="reset" /> 
       </td> 
      </tr> 
     </table> 
    </form> 
</div> 
</div> 
</html> 

これは一つの例ですが、私はどこでもこれをしようとしますが、これらのエラーが発生します。私は行方不明の作品がありますか?

答えて

3

私は思う#requestが

+1

そうですが、なぜですか? #httpServletRequestではなく#requestを参照しています。 –

+0

thymeleafのバージョンによって異なります。 http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#expression-basic-objectsとバージョン3 #request上:ます:http://www.thymeleafバージョン2では#httpServletRequestのようです。 ORG/DOC /チュートリアル/ 3.0/usingthymeleaf.html#式-基本オブジェクト - しかし、あなたはすでに3 thymeleaf使用主張するので、私はここでの問題についてはよく分かりません。おそらくあなたはあなたが思っているより古いバージョンを使用していますか? – eis

0

ここでの問題は、#stringではなく、#stringsを使用する必要があるということです。 #requestの部分がうまく見えます。

また、あなたのコーディングスタイルにちょうどいくつかのコメントは、あなたはフォーマットすることができます:

<th:block th:if="${not #string.isEmpty(#request.getAttribute('some_attr'))}"> 
    <div class="errorblock" th:utext="${#request.getAttribute('some_other_attr')}"></div> 
</th:block> 

を単に

<div th:unless="${#strings.isEmpty(#request.getAttribute('some_attr'))}" class="errorblock" th:utext="${#request.getAttribute('some_other_attr')}" /> 
+0

ありがとう:だからあなたのメソッドは、次のようになります。残念ながら、私はまだ同じエラーが発生しており、#requestや他のWebコンテキストオブジェクトの問題が原因であることは間違いありません。これは私が#stringsを使用しないWebコンテキストオブジェクトを使用するテンプレートでも発生します。 –

+0

ええと...まあ、ここからどこに行くのか分かりません。上記のコードを再作成し、#stringsまたは#request/#responseを使用しても問題はありません。 (Thymeleaf 3、spring boot 1.4.2) – Metroids

0

#httpServletRequestにする必要が判明します単独で使用するとモデルはうまく動作します。そのスタイルの先端のための

@Controller 
public class Controller { 

@RequestMapping(method = RequestMethod.GET, value = "/endpoint", produces = "text/html") 
public String customerServiceSignin(Model uiModel) { 
    uiModel.addAttribute("attr1", true); // show proper header 
    uiModel.addAttribute("attr2", false); 
    return "template"; 
} 
関連する問題