Spring Boot 篇三: Web Application,Thymeleaf以及JPA(下)

第一篇:Spring Boot 篇一: Web Application,Thymeleaf以及JPA(上) 介绍了项目的初始环境,还有对应的Model,以及对应的Repository。

第二篇:Spring Boot 篇二: Web Application,Thymeleaf以及JPA(中) 介绍了具体的Controller和View,以及Thymeleaf的语法。

本篇,介绍了Thymeleaf语法下的多个View的实现。

项目源码寄存在Github repo

单条信息的显示

在List页面,单击其中的链接,可以显示单条Role的具体信息。
其具体的URL为: localhost:8080/personroles/(roleid)

更新Controller

在之前的PersonRoleController添加下列Method来支持单条信息的显示。

@Controller
public class PersonRoleController {
    // ...
    @GetMapping(value = "/personroles/{roleId}")
    public String getPersonRoleById(Model model, @PathVariable long roleId) 
    { 
        PersonRole role = null;
        try {
            role = personRoleService.findById(roleId);
        } catch (ResourceNotFoundException ex) {
            model.addAttribute("errorMessage", "Person role not found");
        }
        model.addAttribute("role", role);

        return "person-role-display";
    }
}

单条信息的显示View

template目录下,新建文件person-role-display.html

<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8" />
        <title>View Person Role</title>
        <link rel="stylesheet" type="text/css" th:href="@{/css/style.css}"/>
    </head>
    <body>
        <h1>View Person Role</h1>
        <a th:href="@{/personroles}">Back to Person Role List</a>
        <br/><br/>
        <div th:if="${role}">
            <table border="0">
                <tr>
                    <td>ID</td>
                    <td>:</td>
                    <td th:utext="${role.id}">...</td>          
                </tr>
                <tr>
                    <td>Name</td>
                    <td>:</td>
                    <td th:utext="${role.roleName}">...</td>             
                </tr>
                <tr>
                    <td>Value</td>
                    <td>:</td>
                    <td th:utext="${role.roleValue}">...</td>
                </tr>
            </table>
            <br/><br/>
            <div th:if="not ${allowDelete}">
                <a th:href="@{/personroles/{roleId}/edit(roleId=${role.getId()})}">Edit</a> |
                <a th:href="@{/personroles/{roleId}/delete(roleId=${role.getId()})}">Delete</a>
            </div>
        </div>
        <div th:if="${errorMessage}" th:utext="${errorMessage}" class="error">            
        </div>
    </body>
</html>

运行程序:
显示页面

单条信息的新建

单条信息的新建和修改都属于单条信息的编辑。

其具体的URL为: localhost:8080/personroles/(roleid)

更新Controller

在之前的PersonRoleController添加下列Method来支持单条信息的新建。

    @GetMapping(value = {"/personroles/add"})
    public String showAddPersonRole(Model model) 
    { 
        PersonRole role = new PersonRole();
        model.addAttribute("add", true);
        model.addAttribute("role", role);
     
        return "person-role-edit";
    }
 
    @PostMapping(value = "/personroles/add")
    public String addPersonRole(Model model, @ModelAttribute("role") PersonRole personRole) 
    { 
        try {
            PersonRole newRole = personRoleService.save(personRole);
            return "redirect:/personroles/" + String.valueOf(newRole.getId());
        } catch (Exception ex) {
            // log exception first, 
            // then show error
            String errorMessage = ex.getMessage();
            logger.error(errorMessage);
            model.addAttribute("errorMessage", errorMessage);
     
            model.addAttribute("add", true);
            return "person-role-edit";
        }
    }

单条记录的编辑View

这里的View,可以用在新建和修改的情形下。

<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8" />
    <title th:text="${add} ? 'Create a person role' : 'Edit a person role'" />
    <link rel="stylesheet" type="text/css" th:href="@{/css/style.css}" />
</head>

<body>
    <h1 th:text="${add} ? 'Create a person role:' : 'Edit a person role:'" />
    <a th:href="@{/personroles}">Back to Person Role List</a>
    <br /><br />
    <form th:action="${add} ? @{/personroles/add} : @{/personroles/{roleId}/edit(roleId=${role.getId()})}"
        th:object="${role}" method="POST">
        <table border="0">
            <tr th:if="${!add}">
                <td>ID</td>
                <td>:</td>
                <td th:utext="*{id}">...</td>
            </tr>
            <tr>
                <td>Name</td>
                <td>:</td>
                <td><input type="text" th:field="*{roleName}" /></td>
            </tr>
            <tr>
                <td>Value</td>
                <td>:</td>
                <td>
                    <select th:field="*{roleValue}">
                        <option th:each="roleOpt : ${T(com.alvachien.springtutorial.thymeleafjpademo.model.PersonRoleEnum).values()}" 
                            th:value="${roleOpt}" th:text="${roleOpt}">
                        </option>
                    </select>
                </td>
            </tr>
        </table>
        <input type="submit" th:value="${add} ? 'Create' : 'Update'" />
    </form>

    <br />
    <!-- Check if errorMessage is not null and not empty -->
    <div th:if="${errorMessage}" th:utext="${errorMessage}" class="error" />
</body>

</html>

这里,需要注意的是,在Thymeleaf模板下,Select控件与Java Enum之间的绑定语法:

<select th:field="*{roleValue}">
    <option th:each="roleOpt : ${T(com.alvachien.springtutorial.thymeleafjpademo.model.PersonRoleEnum).values()}" 
        th:value="${roleOpt}" th:text="${roleOpt}">
    </option>
</select>

单条信息的更新

单条信息的修改URL为: localhost:8080/personroles/(roleid)/edit

更新Controller

    @GetMapping(value = {"/personroles/{roleId}/edit"})
    public String showEditPersonRole(Model model, @PathVariable long roleId)
    { 
        PersonRole role = null;
        try {
            role = personRoleService.findById(roleId);
        } catch (ResourceNotFoundException ex) {
            model.addAttribute("errorMessage", "Person role not found");
        }
        model.addAttribute("add", false);
        model.addAttribute("role", role);
        return "person-role-edit";
    }
    
    @PostMapping(value = {"/personroles/{roleId}/edit"})
    public String updatePersonRole(Model model,
            @PathVariable long roleId,
            @ModelAttribute("personRole") PersonRole personRole) 
    { 
        try {
            personRole.setId(roleId);
            personRoleService.update(personRole);
            return "redirect:/personroles/" + String.valueOf(personRole.getId());
        } catch (Exception ex) {
            // log exception first, 
            // then show error
            String errorMessage = ex.getMessage();
            logger.error(errorMessage);
            model.addAttribute("errorMessage", errorMessage);
     
             model.addAttribute("add", false);
            return "person-role-edit";
        }
    }

单条记录的编辑View

这里复用上面的person-role-edit.html,所以无需新建额外的View。

运行程序:

编辑页面

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值