2016-10-27 8 views
0

が動作していない私のコントローラ、Thymeleaf +春ブーツ:webcontextオブジェクトのセッションは、「ここで適切に

@Controller 
public class CategoryController { 
    @GetMapping(value="/categories") 
    public String searchCategory(Map<String, Object> model, HttpSession session) { 
     Category filter = (Category) session.getAttribute("filter"); 
     if(filter == null) { 
      filter = new Category(); 
      session.setAttribute("filter", filter); 
     } 

     ... 

     return "category-list"; 
    } 
} 

である私は

、セッションにオブジェクトを格納し、そしてコードの下でUI上に表示していました
<!DOCTYPE html> 
<html lang="zh" xmlns:th="http://www.thymeleaf.org"> 
<body> 
      ... 
      <input type="text" class="form-control" id="name" th:field="${session.filter.name}" placeholder="name"/> 
      ... 
</body> 

しかし、私は以下のエラーメッセージが表示されてしまった、intead事前に定義されたWebcontextオブジェクトの

、Thymeleafはリクエストに通常のオブジェクトとして「セッション」を扱うように思えます
2016-10-27 10:34:53.655 ERROR 5844 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[.[dispatcherServlet]  : Servlet.service() fo 
r servlet [dispatcherServlet] in context with path [/smartact] threw exception [Request processing failed; nested except 
ion is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path 
resource [templates/datamaster/category-list.html]")] with root cause 

java.lang.IllegalStateException: **Neither BindingResult nor plain target object for bean name 'session' available as requ 
est attribute** 
     at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) ~[spring-webmvc-5.0.0.BUILD-SN 
APSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
     at org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:307) ~[thymeleaf-spri 
ng4-3.0.0.RELEASE.jar:3.0.0.RELEASE] 
     at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:258) ~[thymeleaf-spring4-3.0.0.RELEASE.ja 
r:3.0.0.RELEASE] 
     at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:227) ~[thymeleaf-spring4-3.0.0.RELEASE.ja 
r:3.0.0.RELEASE] 
     at org.thymeleaf.spring4.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.jav 
a:173) ~[thymeleaf-spring4-3.0.0.RELEASE.jar:3.0.0.RELEASE] 
     at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74 
) ~[thymeleaf-3.0.0.RELEASE.jar:3.0.0.RELEASE] 

コメントありがとうございます!

+0

使用 ':テキストを= "$ {session.filter.name}"' '番目のものです –

+0

:field'はありません。指定されたオブジェクト(通常はコマンドオブジェクト)のフィールドを参照します。おそらく、代わりに 'th:text'を使いたいでしょう。 –

+0

ありがとうございます。 Query-by-Exampleのコマンドオブジェクトとして 'filter'を入れたいと思っています。そして、検索基準をセッションに入れて、検索条件がさまざまな要求の間に続くようにします。 th:テキストはユーザー入力を保持できません。私は正しい?再度、感謝します。 –

答えて

0

Prasanna Kumarが既に述べたように、th:text="${filter.name}"(属性名はfilterです)を使用してください。しかし、もう一つ重要なことがあります:セッションを不要にしないでください!最悪の場合、それをSpringのセッション属性として設定します。通常、あなたがリクエストを使用します属性:第

@Controller 
public class CategoryController { 
    @GetMapping(value="/categories") 
    public String searchCategory(@ModelAttribute("filter") Category filter, Model model) { 
     filter = new Category(); 
     model.addAttribute("filter", filter); 

     ... 

     return "category-list"; 
    } 
} 
+0

ありがとうBranislav、上記の私の返信を参照してください。 –

関連する問題