一個簡單的springboot+mybatis-plus+thymeleaf的學生管理系統


一、登錄功能

1.1登錄所涉及的功能主要包括攔截器,過濾器,用戶在未登錄的時候,訪問頁面會阻止訪問的,如圖所示:

 

 

 實現這個功能的主要代碼如下所示

 1 //攔截器
 2 public class LoginHandlerInterceptor implements HandlerInterceptor {
 3     //執行之前
 4     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
 5         Object username = request.getSession().getAttribute("username");
 6         if(username ==null){
 7             //未登錄,返回登錄頁面
 8             request.setAttribute("msg","沒有權限,請先登錄");
 9             request.getRequestDispatcher("/index.html").forward(request,response);
10             return false;
11         }
12         return true;
13     }

解釋:首先你登錄的時候可以將用戶名之類的信息封裝在session對象里面,在重啟項目后,session的生命周期結束,則Object username = request.getSession().getAttribute("username");

獲取的username為空將執行為空操作,實現對用戶的攔截。 request.getRequestDispatcher("/index.html").forward(request,response);就是重定向到indext頁面。

二、國際化(實現中英文切換)

實現這個功能需要我們在resources下面建立國際化包,如下

 

 

 解釋:login指默認時的,login_en_US.properties指英文,login_zh_CN指中文,在配置文件中,對需要進行轉化的進行書寫,如下

 

 

除此之外還需要在application.properties里面配置

1 #國際化
2 spring.messages.basename=i18n.login

配置完之后將可以實現瀏覽器端的語言切換(大家應該不明白什么是瀏覽器端吧)接着向下看

 

 

 我們在瀏覽器上可以設置英文還是中文,上面的操作就可以實現中英文切換,但這種方法並不是我們想要的,我們想要的是在登錄頁面下,點擊按鈕設置相應的語言,(別着急,向下看) 1

/國際化
 2 public class MyLocaleResolver implements LocaleResolver {
 3     //解析信息
 4     @Override
 5     public Locale resolveLocale(HttpServletRequest request) {
 6         String l = request.getParameter("l");
 7         //默認問英文
 8         Locale locale=Locale.getDefault();
 9         if(!StringUtils.isEmpty(l)){
10             //根據分割線進行分割
11             String[] split = l.split("_");
12             locale = new Locale(split[0], split[1]);
13         }
14         return locale;
15     }
16 
17     @Override
18     public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
19 
20     }
21 }

這個是我們中英文切換按鈕

1 <a class="btn btn-sm" th:href="@{/index.html(l='zh_cn')}">中文</a>
2 <a class="btn btn-sm"th:href="@{/index.html(l='en_US')}">English</a>

大體解釋一下,我們在點擊的時候會攜帶參數的跳轉,英文en_US,中文zh_CN,在MyLocaleResolver方法中我們首先獲取到是en_US還是zh_CN,然后通過spit方法進行分割,英文分割成en US 中文 zh CN

它們是以key value的形式存儲,在springBoot底層可以自動辨別它是什么語言,在springBoot底層默認如下:

 

1  private static Locale initDefault() {
2         String language, region, script, country, variant;
3         language = AccessController.doPrivileged(
4             new GetPropertyAction("user.language", "en"));//表示英文 5         // for compatibility, check for old user.region property
6         region = AccessController.doPrivileged(
7             new GetPropertyAction("user.region"));

三、mybatis-plus實現CRUD

配置過程很簡單詳情請看mybatis-plus官網:https://mp.baomidou.com/guide/

在這里想說的就是我在這個里面遇見的一些問題,因為是第一次使用mybatis-plus,所有對於這些並不是太明白,在自己搗鼓了半天,自己差不多明白了,其實參考內容可以訪問

https://blog.csdn.net/weixin_45616483/article/details/106011637

四、RestFul風格提交

GET請求

后端:

1 public User selectUserById(@PathVariable("id") Integer id){
2         return userService.getUserById(id);
3 }

前端:localhost:8989/xxx/id

post請求

后端:

1 public User insert(User user){
2     userService.insert(user);
3     return user;
4 }

前端:

1 <form action="http://localhost:8989/XXX" method="post">
2     <input type="text" name="username" value="zhansan"/>
3     <input type="text" name="password" value="123"/>
4     <input type="submit" value="提交"/>
5 
6 </form>

PUT請求:

后端

1 public User update(XXX xxxr){
3         return xxxService.update(xxx);
4 }

前端

1 <form action="http://localhost:8989/xxx" method="post">
2 <input type="hidden" name="_method" value="PUT"/>
3     <input type="text" name="username" value="zhangsan"/>
4     <input type="text" name="password" value="123"/>
5     <input type="submit" value="提交"/>
6 
7 </form>

DELETE請求

后端:

1    public String delete(@PathVariable("id") Integer id){
2        xxxService.delete(id);
3     
4     }

前端:

<form action="http://localhost:8989/xxx/x" method="post">
    <input type="text "name="_method" value="DELETE"/>
    <input type="submit" value="提交"/>

>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM