简体   繁体   English

Spring Boot,使用Thymeleaf发行

[英]Spring Boot, Issue using Thymeleaf

I am using NetBeans 8.1, i have a SpringBoot project that has as dependencies: 我正在使用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>

They are all properly downloaded in Dependencies. 它们都已在Dependencies中正确下载。

The project has 3 java classes and an interface to extend JpaRepository<> 该项目具有3个Java类和一个扩展JpaRepository <>的接口

@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>{}

Under src/main/resources -> templates i have a index.html file with a tag -html lang="eng-US" xmlns:th="http://www.thymeleaf.org- : src / main / resources->模板下,我有一个带标签-html lang =“ eng-US” xmlns:th =“ http://www.thymeleaf.org-的index.html文件:

<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>

On the html tag i have the error : Attribute with the local name "xmlns:th is not serializable as XML 1.0 . If i try to run the project and go to the http://localhost:8080/ page i have a Whitelabel Error Page and in the netbeans console i have org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'createdAsShort' cannot be found on object of type 'com.example.Journal' - maybe not public? 在html标记上,我出现错误: 本地名称为“ xmlns:th的属性不能序列化为XML 1.0 。如果我尝试运行该项目并转到http:// localhost:8080 /页面,则出现Whitelabel错误页面和netbeans控制台中,我具有org.springframework.expression.spel.SpelEvaluationException:EL1008E:在类型为com.example.Journal的对象上找不到属性或字段“ createdAsShort”-也许不是公共的?

The error it self says enough: there is no such field createdAsShort in class com.example.Journal . 它本身的错误足以说明问题:类com.example.Journal没有此类字段createdAsShort According to what you've shown definitely there is no such field in your class. 根据您所显示的内容,您的课堂上肯定没有这样的领域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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