使用Thymeleaf进行Spring Boot问题

时间:2017-02-09 21:36:49

标签: java spring-boot thymeleaf

我正在使用NetBeans 8.1,我有一个具有依赖关系的SpringBoot项目:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

它们都在Dependencies中正确下载。

该项目有3个java类和一个扩展JpaRepository&lt;&gt;

的接口
@Entity
public class Journal implements java.io.Serializable{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String title;
private LocalDate created;
private String summary;

public Journal() {
}

public Journal(String title, LocalDate created, String summary) {
    this.title = title;
    this.created = created;
    this.summary = summary;
}
// getters and setters
}
@Controller
public class JournalController {

@Autowired
JournalRepository repo;

@RequestMapping("/")
public String index(Model model){
    model.addAttribute("journal", repo.findAll());
    return "index";
}

}
@SpringBootApplication
public class SpringBootJournalApplication {

@Bean
InitializingBean saveData(JournalRepository repo) {
    return () -> {
        repo.save(new Journal("text1", LocalDate.now(), "date1"));
        repo.save(new Journal("text2", LocalDate.now(), "date2"));
        repo.save(new Journal("text3", LocalDate.now(), "date3"));
        repo.save(new Journal("text4", LocalDate.now(), "date4"));
    };
}

public static void main(String[] args) {
    SpringApplication.run(SpringBootJournalApplication.class, args);
}
}
public interface JournalRepository extends JpaRepository<Journal, Long>{}

src / main / resources下 - &gt;模板我有一个 index.html 文件,标签为-html lang =“eng-US”xmlns:th =“http://www.thymeleaf.org-:

<html lang="en-US" xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
    <div class="container">
        <h1>Spring Boot Journal</h1>
        <ul class="timeline">
            <div th:each="entry,status : ${journal}" >
                <li th:attr="class=${status.odd}?'timeline-inverted':''" >
                    <div class="tl-circ"></div>
                    <div class="timeline-panel">
                        <div class="tl-heading">
                            <h4> <span th:text="${entry.title}">TITLE</span> </h4>
                            <p><small class="text-muted"><i class="glyphicon glyphicon-time"></i>
                                    <span th:text="${entry.createdAsShort}">CREATED</span> </small></p>
                        </div>
                        <div class="tl-body">
                            <p> <span th:text="${entry.summary}">SUMMARY</span> </p>
                        </div>
                    </div>
                </li>
            </div>
        </ul>
    </div>
</body>

在html标签上我有错误:具有本地名称的属性“xmlns:th不可序列化为XML 1.0 。如果我尝试运行项目并转到 { {3}} 页面我有一个 Whitelabel错误页面,在netbeans控制台中我有 org.springframework.expression.spel.SpelEvaluationException:EL1008E:属性或字段'createdAsShort'在“com.example.Journal”类型的对象上找不到 - 可能不公开?

1 个答案:

答案 0 :(得分:1)

它自己说的错误:类<html> <header> <title>Profiel</title> <link href="css/style.css" rel="stylesheet" type="text/css" media="all"/> </header> <body> <?php require_once 'core/init.php'; echo "<div id='main-block' class='main-block'>"; if(Input::exists()) { $validate = new Validate(); $validation = $validate->check($_POST, array( 'username' => array('required' => true), 'password' => array('required' => true) )); if($validate->passed()) { $user = new User(); $remember = (Input::get('remember') === 'on') ? true : false; $login = $user->login(Input::get('username'), Input::get('password'), $remember); if($login) { echo '<br>RD<br>'; header('Location: index.php', true); } else { echo '<p>Incorrect username or password</p>'; } } else { foreach($validate->errors() as $error) { echo $error, '<br>'; } } } ?> <table> <form method="post"> <tr> <div class="field"> <td><label for='username'>Username</label></td> <td><input type="text" name="username" id="username" autocomplete="off"></td> </div> </tr> <tr> <div class="field"> <td><label for='password'>Password</label></td> <td><input type="password" name="password" id="password"></td> </div> </tr> <tr> <div class="field"> <td><label for="remember"> <input type="checkbox" name="remember" id="remember">Remember me </label></td> </div> </tr> <input type="hidden" name="token" value="<?php echo Token::generate(); ?>"> <tr><td><input type="submit" name='submit' value="Login"></td></tr> </form> </table> </div> </body> </html> 中没有这样的字段createdAsShort。根据你所展示的,你班上没有这样的领域。