springboot thymeleaf模板使用

1.maven依賴:

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

2. 控制器

@RequestMapping(value = "/add")//, method = RequestMethod.POST
    public ModelAndView zsUserAdd(ModelAndView mode) {
        mode.setViewName("zsUser/user_addOrEdit");
        mode.addObject("userinfo", "添加用戶");
        ZsUserInfo zsUserInfo = new ZsUserInfo();
        zsUserInfo.setStatus("adduser");
        mode.addObject("zsUserInfo",zsUserInfo);
        return mode;
    }

3.html頁面 thymeleaf模塊

<form  class="form-horizontal"  th:action="@{/zsUser/save}" th:method="post" th:object="${zsUserInfo}"  id="submit_user_info"   enctype="multipart/form-data" style="display: block">

                <div class="box-body">
                    <h4 class="box-title">基本信息</h4>
                    <!-- 判斷界面是新增仍是編輯界面 start-->
                    <input type="hidden" class="form-control" id="status" name="status" th:field="*{status}" >
                    <!-- 判斷界面是新增仍是編輯界面 end-->
                    <div class="form-group">
                        <label class="col-sm-1 control-label">用戶名</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" name="userName"  th:field="*{userName}" >
                            <input type="hidden" class="form-control" id="userId" name="userId" value="" >
                        </div>
                        <label class="col-sm-1 control-label">暱稱</label>
                        <div class="col-sm-3">
                            <input type="text" name="nickName" id="nickName" class="form-control" value="" th:field="*{nickName}" >

                        </div>
                    </div>
</form>

3.1 列表界面

後續完善html

4.thymeleaf默認配置修改 application.yml

# ================================================
#                   Thymeleaf配置
# ================================================
# 是否啓用thymeleaf模板解析
spring.thymeleaf.enabled=true
# 是否開啓模板緩存(建議:開發環境下設置爲false,生產環境設置爲true)
spring.thymeleaf.cache=false
# Check that the templates location exists.
spring.thymeleaf.check-template-location=true
# 模板的媒體類型設置,默認爲text/html
spring.thymeleaf.content-type=text/html
# 模板的編碼設置,默認UTF-8
spring.thymeleaf.encoding=UTF-8
# 設置能夠被解析的視圖,以逗號,分隔
#spring.thymeleaf.view-names=
# 排除不須要被解析視圖,以逗號,分隔
#spring.thymeleaf.excluded-view-names=
# 模板模式設置,默認爲HTML5
#spring.thymeleaf.mode=HTML5
# 前綴設置,SpringBoot默認模板放置在classpath:/template/目錄下
spring.thymeleaf.prefix=classpath:/templates/
# 後綴設置,默認爲.html
spring.thymeleaf.suffix=.html
# 模板在模板鏈中被解析的順序
#spring.thymeleaf.template-resolver-order=
相關文章
相關標籤/搜索