一、登陸頁面:
登陸—>首頁
在開發期間,模版引擎頁面修改以後,想要實時生效
1)禁用模版引擎的緩存
#禁用緩存
spring.thymeleaf.cache=false
2)頁面修改完成以後ctrl+F9:重新編譯一下
登陸錯誤消息的顯示:
<!--判斷-->
<!--顯示錯誤信息,如果返回了msg就代表密碼錯誤-->
<!--th:if判斷成功以後 th:text纔會生效-->
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
3)攔截器機制進行登錄檢查
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object loginUser = request.getSession().getAttribute("loginUser");
if(loginUser==null){
//未登錄,轉到登錄頁
request.setAttribute("msg","沒有權限,請先登陸");
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}else {
//登陸,放行
return true;
}
}
@Configuration
public class MymvcController extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/ogj").setViewName("success");
}
@Bean
//所有的WebMvcConfigurerAdapter組件都會一起起作用 ,但是 我們一定要用bean注入到容器中
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter(){
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("/main").setViewName("dashboard");
}
//查看可重寫的方法:ctrl+o
//註冊攔截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
//靜態資源, css js
//springboot已經做好了靜態資源映射 不用處理
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html","/","/user/login");
}
};
return adapter;
}
}
二、CRUD-員工列表
1)、Result CRUD: CRUD滿足Rest風格;
URI:/資源名稱/資源標識 HTTP請求方式區分對資源CRUD操作
普通CRUD | Result CRUD | |
---|---|---|
select | getEmp | emp–GET |
add | addEmp?xxx | emp–POST |
update | updateEmp?id=xxx&xxx=xx | emp/{id}–PUT |
delete | deleteEmp?id=1 | emp/{id}–DELETE |
2)、實現請求的架構:
請求URI | 請求方式 | |
---|---|---|
查詢所有員工 | emps | GET |
查詢某個員工(來到修改頁面) | emp/id | GET |
來到添加頁面 | emp | GET |
添加員工 | emp | POST |
來到修改頁面(查出員工進行信息回顯) | emp/1 | GET |
修改員工 | emp | PUT |
刪除員工 | emp/1 | DELETE |
3)、員工列表
thymeleaf公共片段的抽取
1、抽取公共片段
<div th:fragment="copy">
This is a fragment
</div>
2、引入公共片段
<div th:insert="~{footer :: copy}"></div>
~{templatename::selector}:模版名::選擇器
~{templatename::fragmentname}:模版名::片段名
3、默認效果:
insert的功能片段在div標籤中
如果使用th:insert等屬性進行引入,可以不寫~{};
行內寫法可以加上:[[~{}]];[(~{})];
三種引入片段的th屬性:
th:insert:將公共片段整個插入到元素中
th:replcae:將聲明引入的元素替換爲公共片段
th:include:被引入的片段的內容包含進這個標籤中
<footer th:fragment="copy">
this is a fragment
</footer>
引入方式
<div th:insert="footer::copy"></div>
<div th:replace="footer::copy"></div>
<div th:include="footer::copy"></div>
效果
還用一種引入方式:給
4)、鏈接高亮問題
引入片段的時候,可以添加參數,來解決鏈接高亮問題。
5)、添加員工
<form th:action="@{/emp}" method="post">
<!--發送put請求修改員工數據-->
<!--
1、SpringMVC中配置HiddenHttpMethodFilter;(SpringBoot自動配置好的)
2、頁面創建一個post表單
3、創建一個input項,name="_method";值就是我們指定的請求方式
-->
<input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>
<input type="hidden" name="id" th:if="${emp!=null}" th:value="${emp.id}">
<div class="form-group">
<label>LastName</label>
<input name="lastName" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${emp.lastName}">
</div>
<div class="form-group">
<label>Email</label>
<input name="email" type="email" class="form-control" placeholder="[email protected]" th:value="${emp!=null}?${emp.email}">
</div>
<div class="form-group">
<label>Gender</label><br/>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" value="1" th:checked="${emp!=null}?${emp.gender==1}">
<label class="form-check-label">男</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" value="0" th:checked="${emp!=null}?${emp.gender==0}">
<label class="form-check-label">女</label>
</div>
</div>
<div class="form-group">
<label>department</label>
<!--提交的是部門的id-->
<select class="form-control" name="department.id">
<option th:value="${dept.id}"
th:each="dept:${depts}"
th:text="${dept.departmentName}">1</option>
</select>
</div>
<div class="form-group">
<label>Birth</label>
<input name="birth" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}">
</div>
<button type="submit" class="btn btn-primary" th:text="${emp!=null}?'修改':'添加'">添加</button>
</form>
提交的數據格式不對:生日:日期;
2017-12-12;2017/12/12;2017.12.12;
日期的格式化:SpringMVC將頁面提交的值需要轉換爲指定的類型;
2017-12-12----Date;類型轉換 格式化
默認日期是按照/的方式的;
我們可以在application上配置:
spring.mvc.date-format=yyyy-MM-dd