詳解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)


在上一篇(詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(上))博文中已經介紹了關於SSM框架的各種基礎配置,(對於SSM配置不熟悉的朋友,可以先看看上一篇博文)那么本篇博文介紹介紹springmvc前后台的交互。

v簡單頁面跳轉

我們首先修改index.jsp文件,實現一個頁面跳轉。

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

在views文件夾下面建一個jsp文件,就是上面所需要跳轉的頁面。

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

 

對應的實現頁面跳轉,完善這個功能,增加映射,可以處理對跳轉中的/message/go作出響應,在controller包里面新建java文件,格式:Controller.java,以示他是用來控制請求的,這里新建文件messageController.java

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

注意在開頭加一個"@Controller",@Controller標識一個Spring類是Spring MVC controller處理器, 也就是加了@Controller,這個文件就會被spring認為是處理請求的 

接着開始寫函數,@RequestMapping() 里面寫鏈接,@RequestMapping() 注解可以在控制器類的級別和/或其中的方法的級別上使用。

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

注意函數名稱可以隨便起,重要的是需要對應的url映射和返回的文件 。點擊運行啟動項目。效果如下圖:

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

點擊"我要跳轉了...", 跳轉至新建的jsp頁面。OK,跳轉頁面的小目標就此實現。

v綜合頁面跳轉

需求:在頁面上輸入一個用戶名,然后根據這個用戶名跳轉到這個用戶的詳情頁。

改造index.jsp, 增加可以輸入用戶名的框框。

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

添加控制的跳轉函數, 

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

@PathVariable可以將 URL 中占位符參數綁定到控制器處理方法的入參中:URL 中的 {xxx} 占位符可以通過,@PathVariable("xxx") 綁定到操作方法的入參中。

新建用戶詳情頁

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

 

點擊運行,實現效果如下:

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

vform表單

1、GET請求會向數據庫發索取數據的請求,從而來獲取信息,該請求就像數據庫的select操作一樣,只是用來查詢一下數據,不會修改、增加數據,不會影響資源的內容,即該請求不會產生副作用。無論進行多少次操作,結果都是一樣的。

2、與GET不同的是,PUT請求是向服務器端發送數據的,從而改變信息,該請求就像數據庫的update操作一樣,用來修改數據的內容,但是不會增加數據的種類等,也就是說無論進行多少次PUT操作,其結果並沒有不同。

3、POST請求同PUT請求類似,都是向服務器端發送數據的,但是該請求會改變數據的種類等資源,就像數據庫的insert操作一樣,會創建新的內容。幾乎目前所有的提交操作都是用POST請求的。

4、DELETE請求顧名思義,就是用來刪除某一個資源的,該請求就像數據庫的delete操作。

在實際操作的時候,就get和post兩種用的比較多。這里主要介紹get和post

GET

1.改造index.jsp,添加get方式的form表單

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

2.添加處理get請求的函數

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

3.根據get請求最終展示數據的報表頁

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

 

4.點擊運行,效果如下:

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

輸入開始和結束時間,點擊查找報表

POST

1.改造index.jsp,添加post方式的form表單

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

2.添加處理post請求的函數

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

 

3.根據post請求最終展示數據的報表頁

報表頁共用GET方式的報表頁。

4.點擊運行,效果如下:

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

輸入開始和結束時間,點擊查找報表

v注冊登錄

大部分網站都繞不開登錄和注冊,那就來講講springmvc登錄注冊的簡單實現。

首先創建一個用戶表

再用mybatis-generator自動生成的實體類,UserInfo

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

點擊查看大圖

添加service層

UserinfoService

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)
package com.springmvc.service;

import com.springmvc.entity.Userinfo;

import java.util.ArrayList;

/**
 * Created by toutou on 2018/5/27.
 */
public interface UserinfoService {
    int insert(Userinfo record);

    ArrayList<Userinfo> selectSelective(Userinfo record);
}
View Code

UserinfoServiceImpl

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

出於用戶賬號安全的考慮,一般用戶密碼都需要進行加密,這樣只有用戶自己知道自己的密碼,采用MD5加密,附上方法。(簡單的加密網上一搜一大堆,我這也是網上搜的)

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)
package com.springmvc.util;

import java.security.MessageDigest;

/**
 * Created by toutou on 2018/5/27.
 */
public class Encryption {
    // MD5加碼。32位
    public static String MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];

        byte[] md5Bytes = md5.digest(byteArray);

        StringBuffer hexValue = new StringBuffer();

        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }

        return hexValue.toString();
    }

    // 可逆的加密算法
    public static String KL(String inStr) {
        // String s = new String(inStr);
        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) (a[i] ^ 't');
        }
        String s = new String(a);
        return s;
    }

    // 加密后解密
    public static String JM(String inStr) {
        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) (a[i] ^ 't');
        }
        String k = new String(a);
        return k;
    }

    /*// 測試主函數
    public static void main(String args[]) {
        String s = new String("123123");
        System.out.println("原始:" + s);
        System.out.println("MD5后:" + MD5(s));
        System.out.println("MD5后再加密:" + KL(MD5(s)));
        System.out.println("解密為MD5后的:" + JM(KL(MD5(s))));
    }*/

    /**
     * 創建指定數量的隨機字符串
     *
     * @param numberFlag
     *            是否是數字
     * @param length
     * @return
     */
    public static String generateQC(boolean numberFlag, int length) {
        String retStr = "";
        String strTable = numberFlag ? "1234567890"
                : "1234567890abcdefghijkmnpqrstuvwxyz";
        int len = strTable.length();
        boolean bDone = true;
        do {
            retStr = "";
            int count = 0;
            for (int i = 0; i < length; i++) {
                double dblR = Math.random() * len;
                int intR = (int) Math.floor(dblR);
                char c = strTable.charAt(intR);
                if (('0' <= c) && (c <= '9')) {
                    count++;
                }
                retStr += strTable.charAt(intR);
            }
            if (count >= 2) {
                bDone = false;
            }
        } while (bDone);

        return retStr;
    }
}
View Code

注冊

添加userController注冊函數

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

添加注冊頁面register.jsp

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

登錄

添加userController登錄函數

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)
package com.springmvc.controller;

import com.springmvc.entity.Userinfo;
import com.springmvc.service.UserinfoService;
import com.springmvc.util.Encryption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by toutou on 2018/5/27.
 */
@Controller
public class userController {
    @Autowired
    private UserinfoService userService;

    @RequestMapping(value="/user/login", method = RequestMethod.GET)
    public String login() {
        return "login";
    }

    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    public String loginValidate(HttpSession session, Model model, @ModelAttribute Userinfo user) {
        List<Userinfo> list = new ArrayList<Userinfo>();
        Userinfo record  = new Userinfo();
        record.setName(user.getName());
        list = userService.selectSelective(record);
        if (list.size() == 0) {
            model.addAttribute("status", 1);
        } else {
            record.setPw(Encryption.MD5(user.getPw()));
            list = userService.selectSelective(record);
            if (list.size() == 0) {
                model.addAttribute("status", 2);
            }
            record = list.get(0);
            session.setAttribute("userinfo", record);
            model.addAttribute("status", 0);
        }

        return "login";
    }
}
View Code

添加登錄頁面login.jsp

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)
<%--
  Created by IntelliJ IDEA.
  User: toutou
  Date: 2018/5/27
  Time: 16:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登錄頁面</title>
</head>
<body>
<div>
    <form id="zc" action="/user/login" method="post">
        用戶名:<input type="text" required id="name" name="name"><br>
        密碼:<input type="password" required id="pw" name="pw"><br>
        <input type="submit" value="登錄">
        <input type="button" value="注冊" onclick="location.href='/user/register'">
    </form>
</div>
</body>
<script>

    //對應后台返回的提示
    if ('${status}' != '') {
        if ('${status}' == 0) {
            alert('登錄成功,即將跳轉至用戶詳情頁!')
            location.href = '/user/userInfo'
        }else if ('${status}' == 1) {
            alert('該賬戶不存在!');
        }
        else if ('${status}' == 2) {
            alert('密碼錯誤!')
        }
    }
</script>
</html>
View Code

添加userControlle個人中心函數

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)
@RequestMapping(value="/user/userInfo", method = RequestMethod.GET)
    public String userInfo(Model model, HttpSession session) {
        Userinfo user = (Userinfo) session.getAttribute("userinfo");
        if(user != null){
            model.addAttribute("user", user);
        }

        return "userInfo";
    }
View Code

添加個人中心頁面userInfo.jsp

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: toutou
  Date: 2018/5/27
  Time: 22:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>個人中心</title>
</head>
<body>
<div>
    <c:if test="${not empty user}">
        <div>歡迎您,${user.name}${user.sex?'女士':'先生'}             <a href="/user/logout">注銷</a></div></div>
    </c:if>
    <c:if test="${ empty user}">
        對不起,請先<a href="/user/login">登錄</a>
    </c:if>
</div>
</body>
</html>
View Code

注銷

詳解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)
@RequestMapping(value = "/user/logout", method = RequestMethod.GET)
    public String logout(HttpSession session) {
        session.invalidate();
        //session.removeAttribute("user");
        return "login";
    }
View Code

v源碼地址

https://github.com/toutouge/javademosecond/tree/master/hellobeijing

v博客總結

關於《詳解intellij idea 搭建SSM框架》共分為上下兩集,大概就介紹這么多了,每個功能(操作)都是盡量介紹到最細節,如有遺漏歡迎補充。


作  者:請叫我頭頭哥
出  處:http://www.cnblogs.com/toutou/
關於作者:專注於基礎平台的項目開發。如有問題或建議,請多多賜教!
版權聲明:本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。
特此聲明:所有評論和私信都會在第一時間回復。也歡迎園子的大大們指正錯誤,共同進步。或者直接私信
聲援博主:如果您覺得文章對您有幫助,可以點擊文章右下角推薦一下。您的鼓勵是作者堅持原創和持續寫作的最大動力!


免責聲明!

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



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