前提・実現したいこと
Spring Bootでヘッダー・フッターを実装したい
→ 共通のパーツを複数ページで使用したい。
(JSPでいうjsp:includeでパーツを表示させるみたいな。。)
共通化したいパーツは、Controllerからビューに値を渡すもので、
html
1<div th:replace="parts/header"></div>
Thymeleaf を使って共通パーツを配置したのですが、コントローラで指定したページ以外ではエラーが出ます… (下記エラー)
恐らくControllerを通っていないためビューに値が渡されていないものだと思うのですが…
方法として、Spring のInterception や AOPを使って Controllerが読み込まれる前に処理を入れる
という事を考えて実践したのですが、Model(オブジェクト?)をビューに値を渡す方法が分かりません…
どちらにせよ良いやり方が思いつかなかったため、方法があればご教示いただきたいです。。
発生している問題・エラーメッセージ
ERROR
1Whitelabel Error Page 2This application has no explicit mapping for /error, so you are seeing this as a fallback. 3 4Thu Oct 22 16:05:02 JST 2020 5There was an unexpected error (type=Internal Server Error, status=500). 6An error happened during template parsing (template: "class path resource [templates/mitulu.html]") 7org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/page.html]") 8 at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241) 9 at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100) 10 at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666) 11 at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) 12 at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) 13 at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) 14 at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189) 15 at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373) 16 at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118) 17 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057) 18 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) 19 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) 20~~~ 21 at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 22 at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) 23 at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) 24 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 25 at java.base/java.lang.Thread.run(Thread.java:832) 26Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "parts/header" - line 21, col 42) 27 at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393) 28 at org.attoparser.MarkupParser.parse(MarkupParser.java:257) 29 at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230) 30 ... 48 more 31Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "parts/header" - line 21, col 42) 32 at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117) 33 at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) 34 at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) 35 at org.thymeleaf.engine.ProcessorTemplateHandler.handleStandaloneElement(ProcessorTemplateHandler.java:918) 36 at org.thymeleaf.engine.StandaloneElementTag.beHandled(StandaloneElementTag.java:228) 37 at org.thymeleaf.engine.Model.process(Model.java:282) 38 at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1587) 39 at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleOpenElementEnd(TemplateHandlerAdapterMarkupHandler.java:304) 40 at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:278) 41 at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleOpenElementEnd(OutputExpressionInlinePreProcessorHandler.java:186) 42 at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:124) 43 at org.attoparser.HtmlElement.handleOpenElementEnd(HtmlElement.java:109) 44 at org.attoparser.HtmlMarkupHandler.handleOpenElementEnd(HtmlMarkupHandler.java:297) 45 at org.attoparser.MarkupEventProcessorHandler.handleOpenElementEnd(MarkupEventProcessorHandler.java:402) 46 at org.attoparser.ParsingElementMarkupUtil.parseOpenElement(ParsingElementMarkupUtil.java:159) 47 at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:710) 48 at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301) 49 ... 50 more 50Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'toppageSearch' available as request attribute 51 at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:153) 52 at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) 53 at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) 54 at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306) 55 at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:253) 56 at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:227) 57 at org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174) 58 at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) 59 ... 66 more
該当のソースコード
parts/header.html
<div class="searchbox"> <form th:method="get" th:action="@{/user/Search}" th:object="${toppageSearch}"> <input type="text" class="searchform" th:field="*{searchword}" placeholder="検索ワード"> <input type="submit" value="検索"> </form> </div>
Toppage.html
<html xmlns:th="http://www.thymeleaf.org"> <body> <div th:replace="parts/header"></div> <div class="container"> </div> </body> </html>
参照元のController
@Controller @SessionAttributes({"toppageSearch", "categorylist"}) //追加 public class ToppageController extends AllController { @ModelAttribute("toppageSearch") ToppageSearch setupForm() { return new ToppageSearch(); } @ModelAttribute("categorylist") List<Category> setuplist() { ~~~~ return categorylist; } @RequestMapping(value = "/Toppage", method = RequestMethod.GET) public String displayTop(Model model) { ...
参照先のController
@Controller @SessionAttributes({"toppageSearch", "categorylist"}) //追加 public class AA { @RequestMapping("/mitulu") public String mitulu() { return "mitulu"; } } ...
(/Toppageでは動作します。)
補足情報(FW/ツールのバージョンなど)
Eclipse IDE
Spring Boot
Gladle
(追記) 解決方法
別ファイルにクラスを用意して継承させました。
AllController.java
public abstract class AllController { @ModelAttribute("toppageSearch") ToppageSearch setupForm() { return new ToppageSearch(); } @ModelAttribute("categorylist") List<Category> setuplist() { ~~~~ return categorylist; }
参照先のController
@Controller public class Mitulu extends AllController {
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/23 01:43 編集
2020/10/23 02:14
2020/10/26 09:00