MVC設(shè)計模式、JSP開發(fā)模式、三層架構(gòu)&MVC練習(xí)學(xué)生信息管理系統(tǒng)
本文將介紹MVC設(shè)計模式、JSP開發(fā)模式、三層架構(gòu)和MVC練習(xí)的基本概念,并演示如何使用這些技術(shù)來構(gòu)建一個學(xué)生信息管理系統(tǒng)。該系統(tǒng)將包括數(shù)據(jù)庫準(zhǔn)備、查詢、增加、刪除、更新和分頁功能。
MVC設(shè)計模式
MVC是一種軟件架構(gòu)模式,將應(yīng)用程序分為三個部分:模型(Model)、視圖(View)和控制器(Controller)。MVC模式的目標(biāo)是將應(yīng)用程序的數(shù)據(jù)、用戶界面和控制邏輯分離,以實現(xiàn)更好的代碼組織、更好的可維護(hù)性和更好的可重用性。
在MVC模式中,Model表示應(yīng)用程序的數(shù)據(jù)和業(yè)務(wù)邏輯,View表示應(yīng)用程序的用戶界面,Controller表示應(yīng)用程序的控制邏輯。這三個部分相互獨立,彼此之間只通過接口通信。這種分離使得應(yīng)用程序的每個部分都可以獨立變更,而不會影響到其他部分。
JSP開發(fā)模式
JSP(JavaServer Pages)是一種動態(tài)網(wǎng)頁技術(shù),它允許將Java代碼嵌入到HTML頁面中。JSP開發(fā)模式是一種使用JSP的軟件開發(fā)模式,它將應(yīng)用程序分為多個模塊,每個模塊都使用JSP來實現(xiàn)。
在JSP開發(fā)模式中,每個模塊都包含一個JSP頁面,該頁面負(fù)責(zé)顯示數(shù)據(jù)和處理用戶輸入。每個模塊都可以獨立開發(fā)和測試,然后通過一個控制器將它們組合起來形成一個完整的應(yīng)用程序。
三層架構(gòu)
三層架構(gòu)是一種軟件架構(gòu)模式,將應(yīng)用程序分為三個部分:表示層(Presentation Layer)、業(yè)務(wù)邏輯層(Business Logic Layer)和數(shù)據(jù)訪問層(Data Access Layer)。三層架構(gòu)的目標(biāo)是將應(yīng)用程序的不同方面分離,以實現(xiàn)更好的代碼組織和可維護(hù)性。
在三層架構(gòu)中,表示層負(fù)責(zé)顯示數(shù)據(jù)和處理用戶輸入,業(yè)務(wù)邏輯層負(fù)責(zé)應(yīng)用程序的業(yè)務(wù)邏輯,數(shù)據(jù)訪問層負(fù)責(zé)與數(shù)據(jù)庫交互。這三個部分相互獨立,彼此之間只通過接口通信。這種分離使得應(yīng)用程序的每個部分都可以獨立變更,而不會影響到其他部分。
MVC練習(xí)
為了練習(xí)MVC模式和JSP開發(fā)模式,我們將開發(fā)一個簡單的學(xué)生信息管理系統(tǒng)。該系統(tǒng)將包括一個表示層、一個業(yè)務(wù)邏輯層和一個數(shù)據(jù)訪問層。我們將使用MySQL數(shù)據(jù)庫來存儲學(xué)生信息。
數(shù)據(jù)庫準(zhǔn)備
首先,我們需要創(chuàng)建一個名為“student”的數(shù)據(jù)庫,以存儲學(xué)生信息。下面是用于創(chuàng)建數(shù)據(jù)庫和表的SQL語句:
CREATE DATABASE student;
USE student;
CREATE TABLE IF NOT EXISTS `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`gender` varchar(10) NOT NULL,
`age` int(11) NOT NULL,
`major` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
查詢功能
我們將在表示層中實現(xiàn)查詢功能。下面是查詢頁面的JSP代碼:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查詢學(xué)生信息</title>
</head>
<body>
<form action="StudentServlet" method="post">
<input type="hidden" name="action" value="query" />
姓名:<input type="text" name="name" />
<input type="submit" value="查詢" />
</form>
</body>
</html>
在這個頁面中,我們使用一個表單來接收用戶輸入的查詢條件。當(dāng)用戶點擊“查詢”按鈕時,表單將提交到StudentServlet中,并傳遞一個名為“action”的參數(shù),值為“query”。
下面是StudentServlet的代碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-424802.html
package com.example.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.dao.StudentDao;
import com.example.model.Student;
@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if ("query".equals(action)) {
String name = request.getParameter("name");
List<Student> students = StudentDao.query(name);
request.setAttribute("students", students);
request.getRequestDispatcher("query.jsp").forward(request, response);
}
}
}
在這個Servlet中,我們首先從請求中獲取名為“action”的參數(shù),然后根據(jù)參數(shù)值執(zhí)行不同的操作。如果參數(shù)值為“query”,則從請求中獲取名為“name”的參數(shù),然后使用StudentDao查詢學(xué)生信息,并將結(jié)果存儲在請求屬性“students”中。最后,將請求轉(zhuǎn)發(fā)到一個名為“query.jsp”的JSP頁面,該頁面將顯示查詢結(jié)果。
下面是query.jsp的代碼:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查詢結(jié)果</title>
</head>
<body>
<table>
<tr>
<th>ID</th>
<th>姓名</th>
<th>性別</th>
<th>年齡</th>
<th>專業(yè)</th>
</tr>
<c:forEach items="${students}" var="student">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.gender}</td>
<td>${student.age}</td>
<td>${student.major}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
在這個JSP頁面中,我們使用<c:forEach>標(biāo)簽循環(huán)顯示查詢結(jié)果。每個結(jié)果將顯示在一個HTML表格中。
增加功能
我們將在表示層中實現(xiàn)增加功能。下面是增加頁面的JSP代碼:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>增加學(xué)生信息</title>
</head>
<body>
<form action="StudentServlet" method="post">
<input type="hidden" name="action" value="add" />
姓名:<input type="text" name="name" /><br />
性別:<input type="radio" name="gender" value="男" checked="checked" />男
<input type="radio" name="gender" value="女" />女<br />
年齡:<input type="text" name="age" /><br />
專業(yè):<input type="text" name="major" /><br />
<input type="submit" value="提交" />
</form>
</body>
</html>
在這個頁面中,我們使用一個表單來接收用戶輸入的新增信息。當(dāng)用戶點擊“提交”按鈕時,表單將提交到StudentServlet中,并傳遞一個名為“action”的參數(shù),值為“add”。
下面是StudentServlet的代碼:
package com.example.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.dao.StudentDao;
import com.example.model.Student;
@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if ("add".equals(action)) {
String name = request.getParameter("name");
String gender = request.getParameter("gender");
int age = Integer.parseInt(request.getParameter("age"));
String major = request.getParameter("major");
Student student = new Student(name, gender, age, major);
StudentDao.add(student);
response.sendRedirect("add.jsp");
}
}
}
在這個Servlet中,我們首先從請求中獲取名為“action”的參數(shù),然后根據(jù)參數(shù)值執(zhí)行不同的操作。如果參數(shù)值為“add”,則從請求中獲取新增信息,然后使用StudentDao將新增信息插入到數(shù)據(jù)庫中。最后,將請求重定向到一個名為“add.jsp”的JSP頁面,該頁面將顯示新增成功的消息。
刪除功能
我們將在表示層中實現(xiàn)刪除功能。下面是刪除頁面的JSP代碼:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>刪除學(xué)生信息</title>
</head>
<body>
<form action="StudentServlet" method="post">
<input type="hidden" name="action" value="delete" />
ID:<input type="text" name="id" />
<input type="submit" value="刪除" />
</form>
</body>
</html>
在這個頁面中,我們使用一個表單來接收用戶輸入的刪除條件。當(dāng)用戶點擊“刪除”按鈕時,表單將提交到StudentServlet中,并傳遞一個名為“action”的參數(shù),值為“delete”。
下面是StudentServlet的代碼:
package com.example.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.dao.StudentDao;
@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if ("delete".equals(action)) {
int id = Integer.parseInt(request.getParameter("id"));
StudentDao.delete(id);
response.sendRedirect("delete.jsp");
}
}
}
在這個Servlet中,我們首先從請求中獲取名為“action”的參數(shù),然后根據(jù)參數(shù)值執(zhí)行不同的操作。如果參數(shù)值為“delete”,則從請求中獲取刪除條件,然后使用StudentDao將符合條件的學(xué)生信息從數(shù)據(jù)庫中刪除。最后,將請求重定向到一個名為“delete.jsp”的JSP頁面,該頁面將顯示刪除成功的消息。
更新功能
我們將在表示層中實現(xiàn)更新功能。下面是更新頁面的JSP代碼:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>更新學(xué)生信息</title>
</head>
<body>
<form action="StudentServlet" method="post">
<input type="hidden" name="action" value="update" />
ID:<input type="text" name="id" /><br />
姓名:<input type="text" name="name" /><br />
性別:<input type="radio" name="gender" value="男" checked="checked" />男
<input type="radio" name="gender" value="女" />女<br />
年齡:<input type="text" name="age" /><br />
專業(yè):<input type="text" name="major" /><br />
<input type="submit" value="提交" />
</form>
</body>
</html>
在這個頁面中,我們使用一個表單來接收用戶輸入的更新信息。當(dāng)用戶點擊“提交”按鈕時,表單將提交到StudentServlet中,并傳遞一個名為“action”的參數(shù),值為“update”。文章來源:http://www.zghlxwxcb.cn/news/detail-424802.html
下面是StudentServlet的代碼:
package com.example.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation
到了這里,關(guān)于MVC設(shè)計模式、JSP開發(fā)模式、三層架構(gòu)&MVC練習(xí)學(xué)生信息管理系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!