三層架構:
和MVC一樣,都是爲了解耦合,提高代碼的複用,區別是,二者對項目的理解是不同。
三層組成:
表示層,USL: use show layer,視圖層,和用戶的交互,
-- 前臺 界面的顯示 css jsp html ...
-- 控制跳轉,調用業務邏輯層 Servlet (SpringMVC Struts2)
業務邏輯層 BLL: bussness logic layer: Service 層 承上啓下,邏輯操作,如刪除之前,先判斷數據存在,然後再刪除
數據訪問層 DAL, data access layer Dao 層 , 直接訪問數據庫的操作,原子性的操作
如上圖就是三層結構,表示層負責發任務,在業務邏輯層這個任務拆成多個小任務,而這些小任務都是不可在分的,
如一個刪除任務,在業務邏輯層就需要拆爲查和刪小任務。
上面例子中的
- 前臺相當於是MVC中的View層。jsp js html css 等
- 後臺相當於是MVC中的Controller 層 Servlet(Spring mvc Struct2)
- 業務邏輯層相當於是 Service 層
- 數據訪問層相當於是Dao層
再三層中的實體類,是負責三層之間數據的傳遞,也可以歸納到MVC中的模型中去。
亂碼問題:
Html頁面亂碼問題:或者將GB18030換爲 utf-8
Servlet收到的Request的Param(即請求參數)亂碼問題,需設置Request的Charset:
request.setCharacterEncoding("UTF-8");
String snum = request.getParameter("snum");
response 返回到瀏覽器亂碼:
response.setContentType("text/html; charset=UTF-8");
數據庫存儲亂碼:
private final String URL = "jdbc:mysql://localhost:3306/springbootdb?characterEncoding=UTF-8";
IDEA 使用tomcat sout 輸出中文的時候亂碼:查看編碼方式
String encoding=System.getProperty("file.encoding");
System.out.println("系統編碼方式: " + encoding);
tomcat 得Edit Configuration 中配置
get請求當拿到的url 中的參數的時候亂碼
// 下面的方式可以解決post或者get方式的中文亂碼問題
// 這裏接收的name是iso8859-1的字符編碼,tomcat 默認的編碼方式
String name = request.getParameter("username");
// 將name字符串按照原來字符編碼打散
byte[] bytes = name.getBytes("ISO8859-1");
// 將bytes字節數據按照指定字符編碼字符編碼進行組裝,組裝爲String
name = new String(bytes, "UTF-8");
//真正的寫法:name = new String(name.getBytes("ISO8859-1"), "UTF-8");
或者在tomcat的service.xml 配置文件中修改默認編碼方式
post請求體中亂碼:
request.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("utf-8"); // 請求的編碼
response.setCharacterEncoding("utf-8");// 響應的編碼
// 設置響應編碼
// response.setContentType("text/html; charset=UTF-8");
// 需要在響應代碼生成以前進行編碼,如果將編碼寫到out 之後那麼還會亂碼
String snum = request.getParameter("snum");
String sname = request.getParameter("sname");
Integer sage = Integer.parseInt(request.getParameter("sage"));
String saddress = request.getParameter("saddress");
Student student = new Student(Integer.valueOf(snum), sname, sage, saddress);
StudentService studentService = new StudentService();
PrintWriter writer = response.getWriter();
try {
boolean student1 = studentService.addStudent(student);
if (student1){
/*
out response request session application 都可以在這裏拿到
out:response.getWriter();
session:request.getSession()
application: request.getServletContext()
*/
writer.print("增加成功!");
// response.setContentType("text/html; charset=UTF-8");
}else {
writer.print("增加失敗!");
}
分頁:
以上的做法的缺點是,id 值必須是連續的,否則無法滿足10條數據。