简体   繁体   中英

How can I solve the problem with my multiple layouts in Thymleaf?

I m getting an error when i want to introduce another fragment from one layout page that contains to fragments. The one is for the header and the other for a post form. When i include just the first one I m getting a problem but if I add the other one im getting two errors.

The first one is:

Error during execution of processor 'org.thymeleaf.spring4.processor.SpringInputGeneralFieldTagProcessor' (template: "fragments/header" - line 38, col 26)

The second one is

` ERROR 13400 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]      
: Servlet.service() for servlet [dispatcherServlet] in context with path 
[/api] threw exception [Request processing failed; nested exception is 
org.thymeleaf.exceptions.TemplateProcessingException: Error during execution 
of processor 
'org.thymeleaf.spring4.processor.SpringInputGeneralFieldTagProcessor' 
(template: "fragments/header" - line 38, col 26)] with root cause

java.lang.IllegalStateException: Neither BindingResult nor plain target 
object for bean name 'tweet' available as request attribute`

The first fragment is a navigation bar and the second is post form. I was using thymleaf and spring boot.

The header.html is:

<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org">
<head>
 ...
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top" th:fragment="header">
<div class="container">
<div class="navbar-header">
  <a class="navbar-brand" href="#"><strong>Twitter</strong></a>
</div>
<div class="navbar-collapse collapse">
  <ul class="nav navbar-nav">
    <li th:classappend="${module == 'home' ? 'active' : ''}">
      <a href="#" th:href="@{/}">My tweets</a>
    </li>
    <li th:classappend="${module == 'tasks' ? 'active' : ''}">
      <a href="#" th:href="@{/new}">Create a User</a>
    </li>
    <li th:classappend="${module == 'tasks' ? 'active' : ''}">
      <a href="#" th:href="@{/tweets}">Tweets</a>
    </li>
    <li th:classappend="${module == 'tasks' ? 'active' : ''}">
      <a href="#" th:href="@{/all}">Display all Users</a>
    </li>
    <li th:classappend="${module == 'tasks' ? 'active' : ''}">
      <a href="#" th:href="@{/overview}">Profile overview</a>
    </li>
  </ul>
</div>
</div>
</div>
<div th:fragment="post">
 <!--/*@thymesVar id="tweet" 
 type="com.javalanguagezone.interviewtwitter.domain.Tweet"*/-->
<form th:action="@{/tweets/}" th:object="${tweet}" method="post">
  <div class="form-group">
  <input type="text" th:field="*{content}" placeholder="What's happening? 
Tell us!">+
  <input type="submit" value="Submit" />
  </div>
</form>
</div>
</body>
</html>

The html file where im calling the layout fragments looks like this:

    <!DOCTYPE html>
   <html lang="en"  xmlns:th="http://www.thymeleaf.org">
   <head><meta charset="UTF-8">
    <title>User overview</title>
   </head>
  <body>
  <div th:replace="fragments/header :: header"></div>
  <div th:replace="fragments/header :: post"></div>

 <div class="container-fluid" style="margin-top: 80px">
 <div class="row">
   <div class="col-md-12">
  <div class="panel panel-primary">

    <div class="panel-heading">
      <h1 class="panel-title">User profile overview</h1>
    </div>
    <div class="panel-body">
      <div class="table-responsive" th:if="${tweets}!=null">
        <table class="table table-hover">
          <thead>
          <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Number of tweets</th>
            <th>Number of followers</th>
            <th>Number of users following</th>
          </tr>
          </thead>
          <tbody>
          <tr>
            <td th:text="${user.id}"></td>
            <td th:text="${user.getUsername()}"></td>
            <td th:text="${tweets}">1</td>
            <td th:text="${followers}">Hamdo</td>
            <td th:text="${following} "><br/>

            </td>
            </span>
          </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
   </div>
  </div>
  </div>
  </body></html>

The controller looks like this:

 @PostMapping("/")
 @ResponseStatus(CREATED)
 public String tweet(@ModelAttribute("tweet") @Valid @RequestBody String 
 tweet, Principal principal, BindingResult result) {
 if(result.hasErrors()){
  return "error";
 }
   tweetService.createTweet(tweet, principal);

  return "index";
  }

Thanks you with the help in advance and im newbie with Thymleaf.

You can try passing down the tweet object as variable to your fragment with the following code.

<div th:replace="fragments/header :: post" th:with="tweetFrag = ${tweet}"></div>

Then, just use the new variable.

<form th:action="@{/tweets/}" th:object="${tweetFrag}" method="post">
  <div class="form-group">
  <input type="text" th:field="*{content}" placeholder="What's happening? Tell us!">+
  <input type="submit" value="Submit" />
  </div>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM