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

1、登陸功能

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

 

 

 實現這個功能的主要代碼以下所示前端

 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");spring

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

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

實現這個功能須要咱們在resources下面創建國際化包,以下瀏覽器

 

 

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

 

 

除此以外還須要在application.properties裏面配置mybatis

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

配置完以後將能夠實現瀏覽器端的語言切換(你們應該不明白什麼是瀏覽器端吧)接着向下看app

 

 

 咱們在瀏覽器上能夠設置英文仍是中文,上面的操做就能夠實現中英文切換,但這種方法並非咱們想要的,咱們想要的是在登陸頁面下,點擊按鈕設置相應的語言,(彆着急,向下看) 1ide

/國際化
 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 }

這個是咱們中英文切換按鈕post

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"));

3、mybatis-plus實現CRUD

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

在這裏想說的就是我在這個裏面碰見的一些問題,由於是第一次使用mybatis-plus,全部對於這些並非太明白,在本身搗鼓了半天,本身差很少明白了,其實參考內容能夠訪問

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

4、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="提交"/>

>
相關文章
相關標籤/搜索