Spring-Boot+thymeleaf3

简介

这个Demo来源于Spring-Boot的GitHub

源码下载地址

http://download.csdn.net/detail/qq_17616169/9720366

项目结构

Spring-Boot+thymeleaf3_第1张图片

代码

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>demogroupId>
    <artifactId>Spring-boot-thymeleaf3artifactId>
    <packaging>warpackaging>
    <version>0.0.1-SNAPSHOTversion>
    <name>Spring-boot-thymeleaf3 Maven Webappname>
    <url>http://maven.apache.orgurl>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.4.1.RELEASEversion>
    parent>
    <properties>
        <main.basedir>${basedir}/../..main.basedir>
        <thymeleaf.version>3.0.2.RELEASEthymeleaf.version>
        <thymeleaf-layout-dialect.version>2.1.1thymeleaf-layout-dialect.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <version>1.4.2.RELEASEversion>
            <scope>testscope>
        dependency>
    dependencies>
    <build>
        <finalName>Spring-boot-thymeleaf3finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                configuration>
            plugin>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

配置文件

# Allow Thymeleaf templates to be reloaded at dev time
spring.thymeleaf.cache: false
spring.thymeleaf.mode: html

java代码

Message.java

/*
 * Copyright 2012-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sample.web.thymeleaf3;

import java.util.Calendar;

import org.hibernate.validator.constraints.NotEmpty;

/**
 * @author Rob Winch
 */
public class Message {

    private Long id;

    @NotEmpty(message = "Message is required.")
    private String text;

    @NotEmpty(message = "Summary is required.")
    private String summary;

    private Calendar created = Calendar.getInstance();

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Calendar getCreated() {
        return this.created;
    }

    public void setCreated(Calendar created) {
        this.created = created;
    }

    public String getText() {
        return this.text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getSummary() {
        return this.summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }
}

MessageRepository.java

/*
 * Copyright 2012-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sample.web.thymeleaf3;

/**
 * @author Rob Winch
 */
public interface MessageRepository {

    Iterable findAll();

    Message save(Message message);

    Message findMessage(Long id);

    void deleteMessage(Long id);

}

InMemoryMessageRepository.java

/*
 * Copyright 2012-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sample.web.thymeleaf3;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author Dave Syer
 */
public class InMemoryMessageRepository implements MessageRepository {

    private static AtomicLong counter = new AtomicLong();

    private final ConcurrentMap messages = new ConcurrentHashMap();

    @Override
    public Iterable findAll() {
        return this.messages.values();
    }

    @Override
    public Message save(Message message) {
        Long id = message.getId();
        if (id == null) {
            id = counter.incrementAndGet();
            message.setId(id);
        }
        this.messages.put(id, message);
        return message;
    }

    @Override
    public Message findMessage(Long id) {
        return this.messages.get(id);
    }

    @Override
    public void deleteMessage(Long id) {
        this.messages.remove(id);
    }

}

SampleWebThymeleaf3Application.java

SpringBoot启动类

/*
 * Copyright 2012-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sample.web.thymeleaf3;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.convert.converter.Converter;

@SpringBootApplication
public class SampleWebThymeleaf3Application {

    @Bean
    public MessageRepository messageRepository() {
        return new InMemoryMessageRepository();
    }

    @Bean
    public Converter messageConverter() {
        return new Converter() {
            @Override
            public Message convert(String id) {
                return messageRepository().findMessage(Long.valueOf(id));
            }
        };
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleWebThymeleaf3Application.class, args);
    }

}

MessageController.java

/*
 * Copyright 2012-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sample.web.thymeleaf3.mvc;

import javax.validation.Valid;

import sample.web.thymeleaf3.Message;
import sample.web.thymeleaf3.MessageRepository;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @author Rob Winch
 * @author Doo-Hwan Kwak
 */
@Controller
@RequestMapping("/")
public class MessageController {

    private final MessageRepository messageRepository;

    public MessageController(MessageRepository messageRepository) {
        this.messageRepository = messageRepository;
    }

    @GetMapping
    public ModelAndView list() {
        Iterable messages = this.messageRepository.findAll();
        return new ModelAndView("messages/list", "messages", messages);
    }

    @GetMapping("{id}")
    public ModelAndView view(@PathVariable("id") Message message) {
        return new ModelAndView("messages/view", "message", message);
    }

    @GetMapping(params = "form")
    public String createForm(@ModelAttribute Message message) {
        return "messages/form";
    }

    @PostMapping
    public ModelAndView create(@Valid Message message, BindingResult result,
            RedirectAttributes redirect) {
        if (result.hasErrors()) {
            return new ModelAndView("messages/form", "formErrors", result.getAllErrors());
        }
        message = this.messageRepository.save(message);
        redirect.addFlashAttribute("globalMessage", "Successfully created a new message");
        return new ModelAndView("redirect:/{message.id}", "message.id", message.getId());
    }

    @RequestMapping("foo")
    public String foo() {
        throw new RuntimeException("Expected exception in controller");
    }

    @GetMapping(value = "delete/{id}")
    public ModelAndView delete(@PathVariable("id") Long id) {
        this.messageRepository.deleteMessage(id);
        Iterable messages = this.messageRepository.findAll();
        return new ModelAndView("messages/list", "messages", messages);
    }

    @GetMapping(value = "modify/{id}")
    public ModelAndView modifyForm(@PathVariable("id") Message message) {
        return new ModelAndView("messages/form", "message", message);
    }

}

前端模板

layout.html

样式框架


<html xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<title>Layouttitle>
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}"
    href="../../css/bootstrap.min.css" />
head>
<body>
    <div class="container">
        <div class="navbar">
            <div class="navbar-inner">
                <a class="brand"
                    href="https://github.com/ultraq/thymeleaf-layout-dialect">
                    Thymeleaf - Layout a>
                <ul class="nav">
                    <li><a th:href="@{/}" href="messages.html"> Messages a>li>
                ul>
            div>
        div>
        <h1 layout:fragment="header">Layouth1>
        <div layout:fragment="content">Fake contentdiv>
    div>
body>
html>

form.htmil

增加、删除页


<html xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
    layout:decorate="layout">
<head>
<title>Messages : Createtitle>
head>
<body>
    <h1 layout:fragment="header">Messages : Createh1>
    <div layout:fragment="content" class="container">
        <form id="messageForm" th:action="@{/(form)}" th:object="${message}" action="#" method="post">
            <div th:if="${#fields.hasErrors('*')}" class="alert alert-error">
                <p th:each="error : ${#fields.errors('*')}" th:text="${error}">Validation errorp>
            div>
            <div class="pull-right">
                <a th:href="@{/}" href="messages.html"> Messages a>
            div>
            <input type="hidden" th:field="*{id}" th:class="${#fields.hasErrors('id')} ? 'field-error'" /> 
            <label for="summary">Summarylabel>
            <input type="text" th:field="*{summary}" th:class="${#fields.hasErrors('summary')} ? 'field-error'" />
            <label for="text">Messagelabel>
            <textarea th:field="*{text}" th:class="${#fields.hasErrors('text')} ? 'field-error'">textarea>
            <div class="form-actions">
                <input type="submit" value="Save" />
            div>
        form>
    div>
body>
html>

list.html

列表页


<html xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
    layout:decorate="layout">
<head>
<title>Messages : View alltitle>
head>
<body>
    <h1 layout:fragment="header">Messages : View allh1>
    <div layout:fragment="content" class="container">
        <div class="pull-right">
            <a href="form.html" th:href="@{/(form)}">Create Messagea>
        div>
        <table class="table table-bordered table-striped">
            <thead>
                <tr>
                    <td>IDtd>
                    <td>Createdtd>
                    <td>Summarytd>
                tr>
            thead>
            <tbody>
                <tr th:if="${messages.empty}">
                    <td colspan="3">No messagestd>
                tr>
                <tr th:each="message : ${messages}">
                    <td th:text="${message.id}">1td>
                    <td th:text="${#calendars.format(message.created)}">July 11,
                        2012 2:17:16 PM CDTtd>
                    <td><a href="view.html" th:href="@{'/' + ${message.id}}"
                        th:text="${message.summary}"> The summary a>td>
                tr>
            tbody>
        table>
    div>
body>
html>

view.html

详情页

<html xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
    layout:decorate="layout">
<head>
<title>Messages : Viewtitle>
head>
<body>
    <h1 layout:fragment="header">Messages : Createh1>
    <div layout:fragment="content" class="container">
        <div class="alert alert-success" th:if="${globalMessage}"
            th:text="${globalMessage}">Some Success messagediv>
        <div class="pull-right">
            <a th:href="@{/}" href="list.html"> Messages a>
        div>
        <dl>
            <dt>IDdt>
            <dd id="id" th:text="${message.id}">123dd>
            <dt>Datedt>
            <dd id="created" th:text="${#calendars.format(message.created)}">
                July 11, 2012 2:17:16 PM CDTdd>
            <dt>Summarydt>
            <dd id="summary" th:text="${message.summary}">A short summary...
            dd>
            <dt>Messagedt>
            <dd id="text" th:text="${message.text}">A detailed message that
                is longer than the summary.dd>
        dl>
        <div class="pull-left">
            <a href="messages" th:href="@{'/delete/' + ${message.id}}">
                delete a> | <a href="form.html"
                th:href="@{'/modify/' + ${message.id}}"> modify a>
        div>
    div>
body>
html>

测试

运行SampleWebThymeleaf3Application.java

Spring-Boot+thymeleaf3_第2张图片

使用浏览器登录http://localhost:8080/ 若出现以下页面,则运行成功

Spring-Boot+thymeleaf3_第3张图片

你可能感兴趣的:(SpringBoot)