【问题标题】:Spring Boot, Issue using ThymeleafSpring Boot,使用 Thymeleaf 的问题
【发布时间】:2017-06-28 02:11:07
【问题描述】:

我使用的是 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>

它们都在依赖项中正确下载。

该项目有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>{}

src/main/resources -> templates 我有一个带有标签的 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。如果我尝试运行项目并转到 @ 987654321@ 页面我有一个 Whitelabel 错误页面 并且在 netbeans 控制台中我有 org.springframework.expression.spel.SpelEvaluationException: EL1008E: 属性或字段 'createdAsShort' 不能在“com.example.Journal”类型的对象上找到 - 可能不公开?

【问题讨论】:

    标签: java spring-boot thymeleaf


    【解决方案1】:

    它自己说的足够多的错误:在类com.example.Journal 中没有这样的字段createdAsShort。根据您所显示的内容,您的班级肯定没有这样的字段。

    【讨论】:

    • 谢谢,我按照书中的示例进行操作,我删除了该行并且它可以工作,仍然从 .html 文件中我总是有“带有本地名称的属性”xmlns:th 不能序列化为html 标记和每个 th: 中的 XML 1.0",我也无法在 th: 标记中使用建议,就像我在带有 "xmlns:h="xmlns.jcp.org/jsf/html" 的 jsf xhtml 中一样。这应该发生吗?这是 NetBeans 问题吗?因为在没有建议/自动完成的情况下工作对我来说可能是个问题。
    猜你喜欢
    • 2019-03-25
    • 2023-03-24
    • 2014-11-21
    • 2017-10-01
    • 1970-01-01
    • 2017-05-26
    • 2019-06-28
    • 2019-07-22
    • 1970-01-01
    相关资源
    最近更新 更多