蒟蒻のspring小窩(員工RestfulCURD)&&日常04

這兩天進展的不是太順利555 導致心情有些低落 所以要多打遊戲啊!
記錄一下thymeleaf+springboot完成的一個curd操作
vue+springboot有待整合
代碼複製到這邊就不整齊了起來 不改了能看就行

默認訪問首頁

//使用WebMvcConfigurerAdapter可以來擴展SpringMVC的功能
 //@EnableWebMvc 不要接管SpringMVC
  @Configuration
 public class MyMvcConfig extends WebMvcConfigurerAdapter {
 
@Override public void addViewControllers(ViewControllerRegistry registry)
 { // super.addViewControllers(registry); //瀏覽器發送
  /atguigu 請求來到 success 
  registry.addViewController("/atguigu").setViewName("success");
   
   }//所有的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"); }
  };return adapter; 
  }
   } 

thymeleaf公共頁面元素抽取


1、抽取公共片段
 <div th:fragment="copy">
  © 2020The Good Thymes Virtual Grocery </div>
   2、引入公共片段 
   <div th:insert="~{footer :: copy}">
   </div> ~{templatename::selector}:模板名::選擇器 
   ~{templatename::fragmentname}:模板名::片段名
    3、默認效果: insert的公共片段在div標籤中 如果使用th:insert等屬性進行引
   入,可以不用寫~{}: 行內寫法可以加上:[[~{}]];[(~{})]

三種引入公共片段的th屬性:
th:insert:將公共片段整個插入到聲明引入的元素中
th:replace:將聲明引入的元素替換爲公共片段
th:include:將被引入的片段的內容包含進這個標籤中


<footer th:fragment="copy">
 © 2020 The Good Thymes Virtual Grocery 
 </footer> 
 引入方式 <div th:insert="footer :: copy">
 </div>
 
<div th:replace="footer :: copy">
</div> 
<div th:include="footer :: copy">
</div> 效果<div><footer> © 2020
 The Good Thymes Virtual Grocery </footer> 
 </div> <footer> © 2020 
 The Good Thymes Virtual Grocery
  </footer>
   <div>
    © 2020 The Good Thymes Virtual Grocery </div>

引入片段的時候傳入參數:


<nav class="col‐md‐2 d‐none d‐md‐block bg‐light sidebar" id="sidebar">

 <div class="sidebar‐sticky"> <ul class="nav flex‐column"> <li 

class="nav‐item">
  <a class="nav‐link active" th:class="${activeUri=='main.html'?'nav‐
  
link active':'nav‐link'}" href="#" th:href="@{/main.html}">

   <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"
    viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke‐
    width="2" stroke‐ linecap="round" stroke‐linejoin="round"
     class="feather feather‐home">
      <path d="M3 9l9‐7 9 7v11a2 2 0 0 1‐2 2H5a2 2 0 0 1‐2‐2z">
     </path> 
     <polyline points="9 22 9 12 15 12 15 22"></polyline>
      
     </svg> Dashboard <span class="sr‐only">(current)
     </span> </a> </li> <!‐‐引入側邊欄;傳入參數‐‐> 
     
     <div th:replace="commons/bar::#sidebar(activeUri='emps')">
     </div> 

太多了。。
明天再補上
update:

攔截器進行登陸檢查


/*** 登陸檢查, */ 
public class LoginHandlerInterceptor implements HandlerInterceptor
 { 
 //目標方法執行之前 @Override 
 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception 
 {
  Object user = request.getSession().getAttribute("loginUser"); if(user == null)
  { //未登陸,返回登陸頁面 
  request.setAttribute("msg","沒有權限請先登陸");
   request.getRequestDispatcher("/index.html").forward(request,response);
    return false;
     }
     else{
     //已登陸,放行請求 
     return true; 
     }
      }
      @Override 
    public void postHandle
   (HttpServletRequest request, HttpServletResponse response, Object
    handler, ModelAndView modelAndView) throws Exception { 
    }
    @Override
     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { 
     }
     
//所有的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.html").setViewName("dashboard"); 
       }//註冊攔截器 
       @Override
        public void addInterceptors
        (InterceptorRegistry registry) { //super.addInterceptors(registry);
         //靜態資源
  // *.css , *.js 
   //SpringBoot已經做好了靜態資源映射 
   registry.addInterceptor(newLoginHandlerInterceptor()).addPathPatterns("/**")
    .excludePathPatterns("/index.html","/","/user/login"); } 
    };
    return adapter;
     }  

CRUD-員工添加

寫了c其他的大概可以自己擴展一下


<form>
<div class="form‐group"> 
<label>LastName
</label>
 <input type="text" class="form‐control" placeholder="zhangsan"> 
 </div> 
 
<div class="form‐group"> 
<label>
Email</label> <input type="email" class="form‐control"

 placeholder="[email protected]"> </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"> <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"> <label class="form‐check‐label"></label>
      </div> 
      </div> 
      <div class="form‐group"> 
      <label>department</label>
       <select class="form‐control">
    <option>1</option>
    <option>2</option> 
<option>3</option>
<option>4</option>
<option>5</option>
   </select>
    </div>
   <div class="form‐group"> <label>Birth</label> <input type="text" class="form‐control" placeholder="zhangsan"> 
   </div> 
  <button type="submit" class="btn btn‐primary">添加</button> </form> 

下面是一位死宅的廢話
快把JOJO補完了 雲了這麼多年
觀看順序果然是35412最好了呢!!
日常逛推與知乎上炒的沸沸揚揚的美帝新冠
美國人民貌似一點都不care這件事啊!!!
再有就是今天完結的我英 season 4
人家官推已經五期製作決定都下來了
前腳剛完結 第五季就來了
果然不關心大陸啊!!?
在這裏插入圖片描述

果然不涉及敏感話題 本身是一部優秀的動漫~~
然而還有些慕洋犬在洗 ε=(´ο`*)))
vue開發者原來也玩知乎 華裔之光呀

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章