


MVC realizes login, data display of addition, deletion, modification and query: EL expression of JSP
The data display here uses the EL expression of jsp, the background is put into the session, and the front-end EL is obtained.
The database design is like this, one teacher corresponds to multiple students, and the foreign key tid is established in the student table student and the teacher table teacher tid corresponds. Now after the teacher successfully logs in, the student information under the teacher can be displayed
Write from back to front;
1 Create the entity class student.java of the table corresponding to the database
public class Student { private int sid; private String sname; private String sage; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSage() { return sage; } public void setSage(String sage) { this.sage = sage; } public Student() { super(); } public Student(int sid, String sname, String sage) { super(); this.sid = sid; this.sname = sname; this.sage = sage; } }
2 StudentDao.java
public interface StudentDao { /** * * @param tid * @return 学生对象 * 根据老师id返回学生对象 */ public List<Student> getStudentBytid(int tid); }
3 StudentDaoImpl.java
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import JDBCUtil.JDBCUtil; import com.zr.dao.StudentDao; import com.zr.model.Student; public class StudentDaoImpl implements StudentDao{ /** * 根据传入的老师id获取学生 */ public List<Student> getStudentBytid(int tid) { //定义学生对象集合students接收数据库返回 List<Student> students = new ArrayList<Student>(); //获取数据库连接 Connection con=JDBCUtil.getConnection(); //编写SQL语句 StringBuffer sql=new StringBuffer("select * from student where tid=?"); try { PreparedStatement pst=con.prepareStatement(sql.toString()); pst.setInt(1, tid); //返回一个结果集 ResultSet rs=pst.executeQuery(); while (rs.next()) { //学生对象接收结果集的结果 Student s=new Student(); s.setSid(rs.getInt("sid")); s.setSname(rs.getString("sname")); s.setSage(rs.getString("sage")); students.add(s); } } catch (SQLException e) { e.printStackTrace(); } return students; }
4 searchService.java
import java.util.List; import com.zr.model.Student; public interface searchService { /** * 查询服务 * @param tid 通过老师ID * @return 学生对象所有信息 */ public List<Student> getStudents(int tid); }
5 searchServiceImpl.java
import java.util.ArrayList; import java.util.List; import com.zr.dao.StudentDao; import com.zr.daoIm.StudentDaoImpl; import com.zr.model.Student; import com.zr.service.searchService; public class searchServiceImpl implements searchService{ public List<Student> getStudents(int tid) { List<Student> students=new ArrayList<Student>(); StudentDao studentDaoImpl =new StudentDaoImpl(); students= studentDaoImpl.getStudentBytid(tid); return students; }
6 SearchController.java
import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.zr.model.Student; import com.zr.model.Teacher; import com.zr.service.searchService; import com.zr.serviceIm.searchServiceImpl; public class SearchController extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List<Student> students=new ArrayList<Student>(); searchService sc=new searchServiceImpl(); //获取存放在session中的teacher对象 Teacher teacher=(Teacher) req.getSession().getAttribute("teacher"); //获取老师ID int td= teacher.getTid(); //根据老师id获取学生对象 students= sc.getStudents(td); //将students对象放入session中,前台用EL表达式获取值 HttpSession session= req.getSession(); session.setAttribute("students", students); req.getRequestDispatcher("main.jsp").forward(req, resp); } }
7 Configure web.xml file
Note: name should come first
<!-- 查询当前下面的学生信息 --> <servlet> <servlet-name>selectStus</servlet-name> <servlet-class>com.zr.controller.SearchController</servlet-class> </servlet> <servlet-mapping> <servlet-name>selectStus</servlet-name> <url-pattern>/selectStus</url-pattern> </servlet-mapping>
8 mian.jsp Front page
<body> <form action="selectStus"> <input type="submit" value="查询"> </form> <!-- 展示学生对象信息 --> <table class="table table-bordered" style="margin-top: 50px"> <tr> <td>学生编号</td> <td>学生名</td> <td>学生年龄</td> </tr> <!-- 利用EL表达式 --> <c:forEach items="${students}" var="student" varStatus="studentIndex"> <tr> <td>${student.sid}</td> <td>${student.sname}</td> <td>${student.sage}</td> </tr> </c:forEach> </table> </body>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Introduction In today's rapidly evolving digital world, it is crucial to build robust, flexible and maintainable WEB applications. The PHPmvc architecture provides an ideal solution to achieve this goal. MVC (Model-View-Controller) is a widely used design pattern that separates various aspects of an application into independent components. The foundation of MVC architecture The core principle of MVC architecture is separation of concerns: Model: encapsulates the data and business logic of the application. View: Responsible for presenting data and handling user interaction. Controller: Coordinates the interaction between models and views, manages user requests and business logic. PHPMVC Architecture The phpMVC architecture follows the traditional MVC pattern, but also introduces language-specific features. The following is PHPMVC

The MVC architecture (Model-View-Controller) is one of the most popular patterns in PHP development because it provides a clear structure for organizing code and simplifying the development of WEB applications. While basic MVC principles are sufficient for most web applications, it has some limitations for applications that need to handle complex data or implement advanced functionality. Separating the model layer Separating the model layer is a common technique in advanced MVC architecture. It involves breaking down a model class into smaller subclasses, each focusing on a specific functionality. For example, for an e-commerce application, you might break down the main model class into an order model, a product model, and a customer model. This separation helps improve code maintainability and reusability. Use dependency injection

SpringMVC framework decrypted: Why is it so popular, specific code examples are needed Introduction: In today's software development field, the SpringMVC framework has become a very popular choice among developers. It is a Web framework based on the MVC architecture pattern, providing a flexible, lightweight, and efficient development method. This article will delve into the charm of the SpringMVC framework and demonstrate its power through specific code examples. 1. Advantages of SpringMVC framework Flexible configuration method Spr

The MVC (Model-View-Controller) pattern is a commonly used software design pattern that can help developers better organize and manage code. The MVC pattern divides the application into three parts: Model, View and Controller, each part has its own role and responsibilities. In this article, we will discuss how to implement the MVC pattern using PHP. Model A model represents an application's data and data processing. usually,

How to implement a scalable MVC architecture in the PHP8 framework Introduction: With the rapid development of the Internet, more and more websites and applications adopt the MVC (Model-View-Controller) architecture pattern. The main goal of MVC architecture is to separate different parts of the application in order to improve the maintainability and scalability of the code. In this article, we will introduce how to implement a scalable MVC architecture in the PHP8 framework. 1. Understand the MVC architecture pattern. The MVC architecture pattern is a software design

In Web development, MVC (Model-View-Controller) is a commonly used architectural pattern for processing and managing an application's data, user interface, and control logic. As a popular web development language, PHP can also use the MVC architecture to design and build web applications. This article will introduce how to use MVC architecture to design projects in PHP, and explain its advantages and precautions. What is MVCMVC is a software architecture pattern commonly used in web applications. MV

Developing MVC with PHP8 framework: Important concepts and techniques that beginners need to know Introduction: With the rapid development of the Internet, Web development plays an important role in today's software development industry. PHP is widely used for web development, and there are many mature frameworks that help developers build applications more efficiently. Among them, the MVC (Model-View-Controller) architecture is one of the most common and widely used patterns. This article will introduce how beginners can use the PHP8 framework to develop MVC applications.

Model-view-controller (mvc) architecture is a powerful design pattern for building maintainable and scalable WEB applications. The PHPMVC architecture decomposes application logic into three distinct components: Model: represents the data and business logic in the application. View: Responsible for presenting data to users. Controller: Acts as a bridge between the model and the view, handling user requests and coordinating other components. Advantages of MVC architecture: Code separation: MVC separates application logic from the presentation layer, improving maintainability and scalability. Reusability: View and model components can be reused across different applications, reducing code duplication. Performance Optimization: MVC architecture allows caching of view and model results, thus increasing website speed. Test Friendly: Detachment
